Skip to content

Commit

Permalink
Merge pull request #339 from preactjs/parent-fix
Browse files Browse the repository at this point in the history
fix: invalid `vnode._parent` value
  • Loading branch information
marvinhagemeister authored Mar 17, 2024
2 parents 5441664 + 4462822 commit 6a4e725
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-eyes-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix invalid parent pointer empty value when rendering a suspended vnode
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ function _renderToString(
return str;
} finally {
if (afterDiff) afterDiff(vnode);
vnode[PARENT] = undefined;
vnode[PARENT] = null;

if (ummountHook) ummountHook(vnode);
}
Expand Down Expand Up @@ -453,7 +453,7 @@ function _renderToString(
const str = renderChildren();

if (afterDiff) afterDiff(vnode);
vnode[PARENT] = undefined;
vnode[PARENT] = null;

if (ummountHook) ummountHook(vnode);

Expand Down Expand Up @@ -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:
Expand Down
26 changes: 25 additions & 1 deletion test/compat/async.test.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 <p>{typeof id === 'string' ? 'ok' : 'fail'}</p>;
}

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

suspended.resolve();
const rendered = await promise;
expect(rendered).to.equal('<p>ok</p>');
});
});

0 comments on commit 6a4e725

Please sign in to comment.