Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalid vnode._parent value #339

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>');
});
});
Loading