-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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 SSR in external docs via node
exports
#20083
Conversation
Our ESM compilation currently assumes a browser and makes certain optimizations accordingly. That means when SSR, we need to make sure the process uses the CJS imports.
(So it works in SSR)
@@ -2,7 +2,6 @@ import React from 'react'; | |||
import ReactDOM from 'react-dom'; | |||
import type { Renderer, Parameters, DocsContextProps, DocsRenderFunction } from '@storybook/types'; | |||
import { Docs, CodeOrSourceMdx, AnchorMdx, HeadersMdx } from '@storybook/blocks'; | |||
import { MDXProvider } from '@mdx-js/react'; |
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.
This is an ESM only package so must be imported async from CJS
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.
Is this a big package? would it be possible to have be bundled in by tsup?
You can still have react & react-dom be external if you do that. FYI
But there's probably some singleton module pattern in this for hooking up context? Or would that be coming from react? Probably.
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.
I'm open to it if you want to discussion @ndelangen. Still it is not a big deal to do it the way I am doing it here.
Issue: #19846
What I did
external-docs
projectMutationObserver
intouseEffect
node
exports to 3 crucial packages (addon-docs
,theming
,blocks
[1]), to ensure node (SSR) code uses the CJS exports.This is due to the fact that currently our ESM files assume they will run in a browser:
storybook/scripts/prepare/bundle.ts
Line 90 in ab4855e
For this reason it is better to force all node-side code to run through CJS.
[1] Note that strictly speaking
addon-docs
and maybetheming
isn't necessary, but people may still import blocks from there. We may also want to alter other packages that are imported directly from MDX files. Or we might consider just doing it for all packages. WDYT @ndelangen?What I didn't do
Resolve the
lodash
issue (1. in #19846) as it doesn't show up in Nextra (as next uses webpack still I guess with lax ESM settings). We can definitely fix this though, in this branch I got it working in a "pure" SSR situation, although without thinking too hard about how to update the lodash imports: https://github.com/storybookjs/storybook/tree/tom/sb-466-external-docs-exports-from@yannbf I believe you already looked at changing the lodash imports, where did you end up with that? (In any case I don't consider this a difficult problem to solve, it seems we can solve it without doing anything drastic).
How to test
Run
yarn dev
in theexternal-docs
package. Check each of the pages SSRs.