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

Better SSR support #548

Merged
merged 2 commits into from
Mar 17, 2023
Merged

Better SSR support #548

merged 2 commits into from
Mar 17, 2023

Conversation

gabro
Copy link
Member

@gabro gabro commented Mar 13, 2023

To use Bento in an SSR environment, the consumer needs to wrap the app in SSRProvider from react-aria. However, react-aria is an implementation detail, and we don't document this anywhere.

This PR:

  • re-exports SSRProvider from react-aria
  • adds a ssr prop to BentoProvider to that the provider can be applied automatically (without even knowing about react-aria)
  • adds some docs about this

@kanbanbot kanbanbot added the WIP label Mar 13, 2023
Comment on lines +123 to +124
export { SSRProvider } from "@react-aria/ssr";
export type { SSRProviderProps } from "@react-aria/ssr";
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the same approach as react-spectrum. I don't think we need necessarily need this export, but in case someone composes their provider differently, I think it makes sense to expose it.

@gabro gabro requested a review from veej March 13, 2023 14:41
@kanbanbot kanbanbot added in review and removed WIP labels Mar 13, 2023
@vercel
Copy link

vercel bot commented Mar 13, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
bento-docs ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 17, 2023 at 3:37PM (UTC)
bento-playroom ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 17, 2023 at 3:37PM (UTC)

@bvkimball
Copy link

@gabro bento also needs a check to see if it is SSR so that it knows to skip any reference to document, which can be found anywhere a portal is created.

return createReactPortal(children, document.body);

const menuPortalTarget = useMemo(() => document.createElement("div"), []);

document.activeElement !== props.inputRef.current

(id) => document.getElementById(`header-cell-${id}`)!.clientWidth

I think this is just for the docs but for completeness

const nodes = [...document.head.querySelectorAll("*")].map((x) => x.cloneNode(true));

@gabro
Copy link
Member Author

gabro commented Mar 15, 2023

Thank you @bvkimball for spotting these! I'm not super familiar with SSR myself, but it seems most of them are inside useEffect, which should only execute client-side, so they should be ok, does it sound right to you?

I'll take a look at the ones that are not and figure out a strategy.

@bvkimball
Copy link

@gabro Yes that does sound right, I have only gotten the error when using "SelectField" because of the useMemo. I will confirm, I did not see it anywhere else. I was just looking for the use of document.

@gabro
Copy link
Member Author

gabro commented Mar 16, 2023

@bvkimball ok, thanks for confirming, I've updated the PR to fix the outstanding cases. I'll take a second pass tomorrow and merge it

Copy link
Member

@veej veej left a comment

Choose a reason for hiding this comment

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

Just a minor doubt, but 👍

@@ -48,7 +50,7 @@ export function NestedMenu({
offset,
});

return state.isOpen
return state.isOpen && !isSSR
Copy link
Member

@veej veej Mar 17, 2023

Choose a reason for hiding this comment

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

is there a specific reason not to use useCreatePortal here instead of useIsSSR and manual check?

Copy link
Member Author

Choose a reason for hiding this comment

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

this createPortal is not our createPortal utility but the standard one from react-dom. Not sure exactly why it wasn't implemented like that, @federico-ercoles may know, but that's the reason why I'm using isSRR here

This prevents accessing document during SSR, which wouldn't work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants