Skip to content

Commit

Permalink
Merge pull request #336 from preactjs/suspense-rethrow
Browse files Browse the repository at this point in the history
fix: suspended error not re-thrown
  • Loading branch information
marvinhagemeister authored Mar 17, 2024
2 parents 797c82f + c46fb59 commit a50c93d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-spoons-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix error thrown after suspending not being rethrown.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ function _renderToString(
try {
return renderChildren();
} catch (e) {
if (!e || typeof e.then !== 'function') throw e;

return e.then(
() => renderChildren(),
() => renderNestedChildren()
Expand Down
28 changes: 28 additions & 0 deletions test/compat/async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,32 @@ describe('Async renderToString', () => {

expect(rendered).to.equal(expected);
});

it('should rethrow error thrown after suspending', async () => {
const { suspended, getResolved } = createSuspender();

function Suspender() {
if (!getResolved()) {
throw suspended.promise;
}

throw new Error('fail');
}

const promise = renderToStringAsync(
<Suspense fallback={<div>loading...</div>}>
<Suspender />
</Suspense>
);

let msg = '';
try {
suspended.resolve();
await promise;
} catch (err) {
msg = err.message;
}

expect(msg).to.equal('fail');
});
});
3 changes: 3 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export function createSuspender() {
}

return {
getResolved() {
return resolved;
},
suspended: deferred,
Suspender
};
Expand Down

0 comments on commit a50c93d

Please sign in to comment.