From 44628228dec6b10c59023c21e17a140f205ed0c9 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Sun, 17 Mar 2024 16:39:40 +0100 Subject: [PATCH] fix: invalid `vnode._parent` value We have lots of code in hooks that checks for this being `null` when empty. This broke with `preact-render-to-string` because we did set it to `undefined` on reset here. --- .changeset/proud-eyes-cheer.md | 5 +++++ src/index.js | 6 +++--- test/compat/async.test.js | 26 +++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 .changeset/proud-eyes-cheer.md diff --git a/.changeset/proud-eyes-cheer.md b/.changeset/proud-eyes-cheer.md new file mode 100644 index 00000000..388e5486 --- /dev/null +++ b/.changeset/proud-eyes-cheer.md @@ -0,0 +1,5 @@ +--- +'preact-render-to-string': patch +--- + +Fix invalid parent pointer empty value when rendering a suspended vnode diff --git a/src/index.js b/src/index.js index 90531b64..6be17849 100644 --- a/src/index.js +++ b/src/index.js @@ -425,7 +425,7 @@ function _renderToString( return str; } finally { if (afterDiff) afterDiff(vnode); - vnode[PARENT] = undefined; + vnode[PARENT] = null; if (ummountHook) ummountHook(vnode); } @@ -453,7 +453,7 @@ function _renderToString( const str = renderChildren(); if (afterDiff) afterDiff(vnode); - vnode[PARENT] = undefined; + vnode[PARENT] = null; if (ummountHook) ummountHook(vnode); @@ -618,7 +618,7 @@ function _renderToString( } if (afterDiff) afterDiff(vnode); - vnode[PARENT] = undefined; + vnode[PARENT] = null; if (ummountHook) ummountHook(vnode); // Emit self-closing tag for empty void elements: diff --git a/test/compat/async.test.js b/test/compat/async.test.js index c1b4bfee..fe513e11 100644 --- a/test/compat/async.test.js +++ b/test/compat/async.test.js @@ -1,6 +1,6 @@ import { renderToStringAsync } from '../../src/index.js'; import { h } from 'preact'; -import { Suspense } from 'preact/compat'; +import { Suspense, useId } from 'preact/compat'; import { expect } from 'chai'; import { createSuspender } from '../utils.js'; @@ -165,4 +165,28 @@ describe('Async renderToString', () => { expect(msg).to.equal('fail'); }); + + it('should support hooks', async () => { + const { suspended, getResolved } = createSuspender(); + + function Suspender() { + const id = useId(); + + if (!getResolved()) { + throw suspended.promise; + } + + return

{typeof id === 'string' ? 'ok' : 'fail'}

; + } + + const promise = renderToStringAsync( + loading...}> + + + ); + + suspended.resolve(); + const rendered = await promise; + expect(rendered).to.equal('

ok

'); + }); });