From 054dae0ae9dc0ab5b86c1c7fd9712b640a09111d Mon Sep 17 00:00:00 2001 From: Chris Couzens Date: Wed, 17 Jul 2024 18:03:24 +0100 Subject: [PATCH] Fix instance where renderToStringAsync returns a promise of a promise (#378) * Fix issue where rendered is a promise I want to test this in my project to see if this change fixes it. All being well, I'll then add a test here and submit it for review * Different syntax, but more success I'm unclear why the previous didn't seem to work for me * Attempt to write a test for the case based on my app So far no luck actually reproducing the issue. I will take a different tact and remove code from my app until the issue goes away, to figure out what causes the behaviour. * Add test that tests behaviour * Further simplify test case * Further simplify test case * Create the test I want * Revert unrelated change * Add changeset * Add missing imports * Reproduce without useClient But I still need the provider * Avoid using urql * Better description * Update test/compat/async.test.jsx --------- Co-authored-by: Jovi De Croock --- .changeset/famous-experts-decide.md | 5 ++ src/index.js | 2 +- test/compat/async.test.jsx | 83 +++++++++++++++++++++++------ 3 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 .changeset/famous-experts-decide.md diff --git a/.changeset/famous-experts-decide.md b/.changeset/famous-experts-decide.md new file mode 100644 index 00000000..476006ab --- /dev/null +++ b/.changeset/famous-experts-decide.md @@ -0,0 +1,5 @@ +--- +'preact-render-to-string': patch +--- + +Fix issue where preactRenderToString returns a promise of a promise diff --git a/src/index.js b/src/index.js index 9dec64ab..743f25aa 100644 --- a/src/index.js +++ b/src/index.js @@ -109,7 +109,7 @@ export async function renderToStringAsync(vnode, context) { parent[CHILDREN] = [vnode]; try { - const rendered = _renderToString( + const rendered = await _renderToString( vnode, context || EMPTY_OBJ, false, diff --git a/test/compat/async.test.jsx b/test/compat/async.test.jsx index 8b50c001..968c12b1 100644 --- a/test/compat/async.test.jsx +++ b/test/compat/async.test.jsx @@ -1,6 +1,6 @@ import { renderToStringAsync } from '../../src/index.js'; -import { h } from 'preact'; -import { Suspense, useId } from 'preact/compat'; +import { h, Fragment } from 'preact'; +import { Suspense, useId, lazy, createContext } from 'preact/compat'; import { expect } from 'chai'; import { createSuspender } from '../utils.jsx'; @@ -26,10 +26,14 @@ describe('Async renderToString', () => { }); it('should render JSX with nested suspended components', async () => { - const { Suspender: SuspenderOne, suspended: suspendedOne } = - createSuspender(); - const { Suspender: SuspenderTwo, suspended: suspendedTwo } = - createSuspender(); + const { + Suspender: SuspenderOne, + suspended: suspendedOne + } = createSuspender(); + const { + Suspender: SuspenderTwo, + suspended: suspendedTwo + } = createSuspender(); const promise = renderToStringAsync(