Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

feat(getServerState): allow users to inject renderToString #3658

Merged
merged 5 commits into from
Oct 20, 2022

Conversation

Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Oct 19, 2022

Summary

There are some cases where the combination of trying to make sure renderToString doesn't end up in a browser bundle, being runnable on esm + cjs, react 17 and 18, .js extension etc. blows up.

One of those is pnpm/vercel removing "unused" packages.

Result

This PR introduces a new argument renderToString to getServerState so you can inject the dependency yourself, meaning the import is within your own code and won't be purged.

import { renderToString } from 'react-dom/server';
await getServerState(<App/>, renderToString);
await getServerState(<App/>, import('react-dom/server').then(mod => mod.renderToString));

fixes #3633
closes #3618
see vercel/next.js#40067
FX-1869

There are some cases where the combination of trying to make sure renderToString doesn't end up in a browser bundle, being runnable on esm + cjs, react 17 and 18, .js extension etc. blows up. One of those is pnpm caching removing "unused" packages.

This PR introduces a new argument `renderToString` to `getServerState` so you can inject the dependency yourself, meaning the import is within your own code and won't be purged.

```js
import { renderToString } from 'react-dom/server';
await getServerState(<App/>, renderToString);
await getServerState(<App/>, import('react-dom/server').then(mod => mod.renderToString));
```

fixes #3633
closes #3618
see vercel/next.js#40067
@Haroenv Haroenv requested review from a team, dhayab and aymeric-giraudet and removed request for a team October 19, 2022 13:28
@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 19, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 8a21a6f:

Sandbox Source
react-instantsearch-app Configuration
hooks-example Configuration
hooks-next-example Issue #3633

@netlify
Copy link

netlify bot commented Oct 19, 2022

Deploy Preview for react-instantsearch ready!

Name Link
🔨 Latest commit 8a21a6f
🔍 Latest deploy log https://app.netlify.com/sites/react-instantsearch/deploys/63510a599181f20007de96c6
😎 Deploy Preview https://deploy-preview-3658--react-instantsearch.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@francoischalifour
Copy link
Member

Don't we want to be more cautious and have it in a param object?

await getServerState(<App/>, { renderToString });

Copy link
Member

@dhayab dhayab left a comment

Choose a reason for hiding this comment

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

Neat idea!

import type {
InstantSearchServerContextApi,
InstantSearchServerState,
} from 'react-instantsearch-hooks';

type SearchRef = { current: InstantSearch | undefined };

export type RenderToString = (element: JSX.Element) => unknown;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
export type RenderToString = (element: JSX.Element) => unknown;
export type RenderToString = (element: JSX.Element) => string;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left unknown as you should be able to use this argument with renderToStream or something as well, because we don't use the string

Copy link
Member

Choose a reason for hiding this comment

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

renderToReadableStream() will probably create issues for example. But as long as its typed as a synchronous method it should be fine indeed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was more thinking if there's in theory another method that traverses the tree, but doesn't render, it is also sufficient for this method

…s-none.test.tsx

Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
@Haroenv Haroenv enabled auto-merge (squash) October 20, 2022 08:44
@Haroenv Haroenv merged commit 9c10449 into master Oct 20, 2022
@Haroenv Haroenv deleted the feat/get-server-state-allow-inject branch October 20, 2022 08:52
Haroenv added a commit that referenced this pull request Oct 20, 2022
Followup on #3658, to be merged close to the release
Haroenv added a commit to algolia/doc-code-samples that referenced this pull request Oct 20, 2022
follow up on algolia/react-instantsearch#3658

to be merged once this is released
Haroenv added a commit that referenced this pull request Oct 20, 2022
Followup on #3658, to be merged close to the release

EDIT: actually this can be merged before the release already as
previously the unused argument wasn't used
Haroenv added a commit to algolia/doc-code-samples that referenced this pull request Oct 25, 2022
follow up on algolia/react-instantsearch#3658

to be merged once this is released
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Could not import ReactDOMServer
4 participants