Skip to content

Commit 623184b

Browse files
kasperpeulenstorybook-bot
authored andcommitted
Merge pull request #30847 from storybookjs/kasper/ssr-portable-stories
React: Allow portable stories to be used in SSR (cherry picked from commit e289186)
1 parent 6039743 commit 623184b

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

code/renderers/react/src/act-compat.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/src/act-compat.js
33
import * as React from 'react';
44

5-
import * as DeprecatedReactTestUtils from 'react-dom/test-utils';
6-
75
declare const globalThis: {
86
IS_REACT_ACT_ENVIRONMENT: boolean;
97
};
@@ -13,9 +11,6 @@ declare const globalThis: {
1311
// We do check if act exists, but webpack will still throw an error on compile time
1412
const clonedReact = { ...React };
1513

16-
const reactAct =
17-
typeof clonedReact.act === 'function' ? clonedReact.act : DeprecatedReactTestUtils.act;
18-
1914
export function setReactActEnvironment(isReactActEnvironment: boolean) {
2015
globalThis.IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment;
2116
}
@@ -67,7 +62,15 @@ function withGlobalActEnvironment(actImplementation: (callback: () => void) => P
6762
};
6863
}
6964

70-
export const act =
71-
process.env.NODE_ENV === 'production'
65+
export const getAct = async () => {
66+
// Lazy loading this makes sure that @storybook/react can be loaded in SSR contexts
67+
// For example when SSR'ing portable stories
68+
const reactAct =
69+
typeof clonedReact.act === 'function'
70+
? clonedReact.act
71+
: (await import('react-dom/test-utils')).act;
72+
73+
return process.env.NODE_ENV === 'production'
7274
? (cb: (...args: any[]) => any) => cb()
7375
: withGlobalActEnvironment(reactAct);
76+
};

code/renderers/react/src/entry-preview.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22

33
import semver from 'semver';
44

5-
import { act, getReactActEnvironment, setReactActEnvironment } from './act-compat';
5+
import { getAct, getReactActEnvironment, setReactActEnvironment } from './act-compat';
66
import type { Decorator } from './public-types';
77

88
export const parameters = { renderer: 'react' };
@@ -32,6 +32,8 @@ export const beforeAll = async () => {
3232
// https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/src/pure.js
3333
const { configure } = await import('@storybook/test');
3434

35+
const act = await getAct();
36+
3537
configure({
3638
unstable_advanceTimersWrapper: (cb) => {
3739
return act(cb);

code/renderers/react/src/renderToCanvas.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { RenderContext } from 'storybook/internal/types';
55

66
import { global } from '@storybook/global';
77

8-
import { act } from './act-compat';
8+
import { getAct } from './act-compat';
99
import type { ReactRenderer, StoryContext } from './types';
1010

1111
const { FRAMEWORK_OPTIONS } = global;
@@ -98,6 +98,7 @@ export async function renderToCanvas(
9898
unmountElement(canvasElement);
9999
}
100100

101+
const act = await getAct();
101102
await new Promise<void>(async (resolve, reject) => {
102103
actQueue.push(async () => {
103104
try {

0 commit comments

Comments
 (0)