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 instance where renderToStringAsync returns a promise of a promise #378

Merged
merged 14 commits into from
Jul 17, 2024
5 changes: 5 additions & 0 deletions .changeset/famous-experts-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix issue where preactRenderToString returns a promise of a promise
49 changes: 47 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"@babel/register": "^7.12.10",
"@changesets/changelog-github": "^0.4.1",
"@changesets/cli": "^2.18.0",
"@urql/preact": "^4.1.0",
"baseline-rts": "npm:preact-render-to-string@latest",
"benchmarkjs-pretty": "^2.0.1",
"chai": "^4.2.0",
Expand Down Expand Up @@ -170,4 +171,4 @@
"publishConfig": {
"provenance": true
}
}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 42 additions & 0 deletions test/compat/async.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,46 @@ describe('Async renderToString', () => {
const rendered = await promise;
expect(rendered).to.equal('<p>ok</p>');
});

it('should render JSX after a urql component', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also want to try a test where it's triple-y nested as it might warrant a loop similar to the Array.isArray one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think this would look like?

If I do something like:

		const Context = createContext();

		let c = 0;

		const Fetcher = ({ children }) => {
			c++;
			if (c === 1) {
				throw Promise.resolve();
			}
			return <Fragment>{children}</Fragment>;
		};

		const LazyComponent = lazy(
			async () =>
				function ImportedComponent() {
					return <div>2</div>;
				}
		);

		const NestedLazyComponent = lazy(
			async () =>
				function ImportedComponent() {
					return (
						<Context.Provider>
							<Fetcher>
								<LoadableComponent />
							</Fetcher>
						</Context.Provider>
					);
				}
		);

		const LoadableComponent = ({}) => (
			<Suspense fallback={'...loading'}>
				<LazyComponent />
			</Suspense>
		);

		const NestedLoadableComponent = ({}) => (
			<Suspense fallback={'...loading'}>
				<NestedLazyComponent />
			</Suspense>
		);

		const rendered = await renderToStringAsync(
			<Context.Provider>
				<Fetcher>
					<NestedLoadableComponent />
				</Fetcher>
			</Context.Provider>
		);

		expect(rendered).to.equal(`<div>2</div>`);

Then the current solution continues to work

const client = urql.createClient({
url: 'http://localhost:1234',
exchanges: [urql.cacheExchange, urql.fetchExchange],
suspense: true,
fetch: () =>
Promise.resolve(
new Response('{ "data": { "foo": 4 } }', {
headers: { 'Content-Type': 'application/json' }
})
)
});

const Fetcher = ({ children }) => {
urql.useQuery({ query: 'query{ foo }' });
return <Fragment>{children}</Fragment>;
};

const LazyComponent = lazy(
async () =>
function ImportedComponent() {
return <div>2</div>;
}
);

const LoadableComponent = ({}) => (
<Suspense fallback={'...loading'}>
<LazyComponent />
</Suspense>
);

const rendered = await renderToStringAsync(
<urql.Provider value={client}>
<Fetcher>
<LoadableComponent />
</Fetcher>
</urql.Provider>
);

expect(rendered).to.equal(`<div>2</div>`);
});
});