-
Notifications
You must be signed in to change notification settings - Fork 1
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: allow experiences-sdk-react components/hooks to be imported in NextJS server components #667
Conversation
…extJS server components
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clean work 👏
Were you able to verify this fix with page router SSR? Does it render on the server correctly? I am a bit worried that adding |
Ya, I just ran through and looked at it again. It renders just as it did before. I think pages router simply ignores the 'use client' directive as thats a RSC construct, much like SPA frameworks do. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sharing this on the call yesterday. I just read a bit more about this kind of pragmas/ directives and your changes sound reasonable to me. Will approve this for sparks :)
Purpose
Allows importing code direclty from
@contentful/experiences-sdk-react
from NextJS server components.Right now we have a workaround to import certain pieces from
@contentful/experiences-core
as importing them in a NextJS server component will throw an error. This fix allows those components to now be successfully imported from the main sdk.Ticket: https://contentful.atlassian.net/browse/ALT-1010
Approach
I added
use client
directives to components that use anything from React that requires client side (useState, useLayoutEffect, etc..). This instructs Next that these are client components, and it will import them as such. This change should have no effect on other frameworks as the 'use client' directive should simply be ignored by them.To get this to work, I had to change the rollup build to preserve the modules (and not bundle them), and to not exclude the directive.
Test Dev Build
You can test this out locally with a dev build by installing
@contentful/experiences-sdk-react@1.8.1-prerelease-20240624T1502-6dcdc44.0