Skip to content

Commit

Permalink
fix: suspended error not re-thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 17, 2024
1 parent 797c82f commit cc40cfd
Show file tree
Hide file tree
Showing 4 changed files with 39 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
29 changes: 29 additions & 0 deletions test/compat/async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { h } from 'preact';
import { Suspense } from 'preact/compat';
import { expect } from 'chai';
import { createSuspender } from '../utils.js';
import { Deferred } from '../../src/util.js';

Check failure on line 6 in test/compat/async.test.js

View workflow job for this annotation

GitHub Actions / Build & Test

'Deferred' is defined but never used. Allowed unused vars must match /^h$/u

describe('Async renderToString', () => {
it('should render JSX after a suspense boundary', async () => {
Expand Down Expand Up @@ -137,4 +138,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 cc40cfd

Please sign in to comment.