diff --git a/.changeset/cuddly-spoons-impress.md b/.changeset/cuddly-spoons-impress.md new file mode 100644 index 00000000..74c69822 --- /dev/null +++ b/.changeset/cuddly-spoons-impress.md @@ -0,0 +1,5 @@ +--- +'preact-render-to-string': patch +--- + +Fix error thrown after suspending not being rethrown. diff --git a/src/index.js b/src/index.js index c52a067f..90531b64 100644 --- a/src/index.js +++ b/src/index.js @@ -467,6 +467,8 @@ function _renderToString( try { return renderChildren(); } catch (e) { + if (!e || typeof e.then !== 'function') throw e; + return e.then( () => renderChildren(), () => renderNestedChildren() diff --git a/test/compat/async.test.js b/test/compat/async.test.js index 476ebb99..c1b4bfee 100644 --- a/test/compat/async.test.js +++ b/test/compat/async.test.js @@ -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( + loading...}> + + + ); + + let msg = ''; + try { + suspended.resolve(); + await promise; + } catch (err) { + msg = err.message; + } + + expect(msg).to.equal('fail'); + }); }); diff --git a/test/utils.js b/test/utils.js index ae19dad8..6a2c10db 100644 --- a/test/utils.js +++ b/test/utils.js @@ -27,6 +27,9 @@ export function createSuspender() { } return { + getResolved() { + return resolved; + }, suspended: deferred, Suspender };