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 SSR in external docs via node exports #20083

Merged
merged 7 commits into from
Dec 5, 2022

Conversation

tmeasday
Copy link
Member

@tmeasday tmeasday commented Dec 4, 2022

Issue: #19846

What I did

  • Some small fixes to external-docs project
  • Moved MutationObserver into useEffect
  • Add 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:

c.platform = platform || 'browser';

For this reason it is better to force all node-side code to run through CJS.

[1] Note that strictly speaking addon-docs and maybe theming 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 the external-docs package. Check each of the pages SSRs.

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.
@@ -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';
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 an ESM only package so must be imported async from CJS

Copy link
Member

@ndelangen ndelangen Dec 5, 2022

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.

Copy link
Member Author

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.

@ndelangen ndelangen merged commit b2e0a8a into next Dec 5, 2022
@ndelangen ndelangen deleted the tom/sb-466-external-docs-exports-from-2 branch December 5, 2022 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants