Skip to content

Commit 364b81c

Browse files
authored
[V4] Make sure that the server side rendering works again (@W-19159606@) (#2938)
* Fix server side rendering Brought back the useInsertionEffect no-op from PR #2785. * Bring back this comment * Update CHANGELOG.md
1 parent d1f9aa1 commit 364b81c

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

packages/pwa-kit-react-sdk/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
## v4.0.0-extensibility-preview.5 (Jul 02, 2025)
2+
- SSR rendering: implement workaround for react-ssr-prepass to ignore React's useInsertionEffect [#2785](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2785)
3+
24
## v4.0.0-extensibility-preview.4 (Feb 12, 2025)
35
- Replace `event-emitter` in favor of the native `EventTarget` [#2289](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2289)
46
- Call app extension's new methods `getRoutes` and `getRoutesAsync` [#2308](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2308)

packages/pwa-kit-react-sdk/src/ssr/server/react-rendering.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ import * as errors from '../universal/errors'
3535
import logger from '../../utils/logger-instance'
3636
import PerformanceTimer, {PERFORMANCE_MARKS} from '../../utils/performance'
3737

38+
// Workaround for react-ssr-prepass lack of no-op for the React hook `useInsertionEffect`.
39+
// (see https://github.com/FormidableLabs/react-ssr-prepass/issues/84)
40+
// - Why useInsertionEffect? Chakra v3 hooks (like useDisclosure, useBreakpointValue, useMediaQuery, useCallbackRef) rely on it for rendering optimization.
41+
// * see https://github.com/chakra-ui/chakra-ui/blob/c83e7f5acff2751ee3722bf22d89a5fe581fd72c/packages/react/src/hooks/use-callback-ref.ts#L17-L19
42+
// - Why no-op? useInsertionEffect is meant for client side only. So on the server, we wanted prepass to ignore it.
43+
// - Is it safe? Yes, we're not breaking the original behaviour. As React doc explains, the hook is for client side only: https://react.dev/reference/react/useInsertionEffect
44+
if (React.useInsertionEffect) {
45+
React.useInsertionEffect = () => {} // no-op
46+
}
47+
3848
const CWD = process.cwd()
3949
const BUNDLES_PATH = path.resolve(CWD, 'build/loadable-stats.json')
4050

packages/pwa-kit-react-sdk/src/ssr/universal/components/with-react-query/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ export const withReactQuery = (Wrapped, options = {}) => {
7070

7171
res.__performanceTimer.mark(PERFORMANCE_MARKS.reactQueryPrerender, 'start')
7272
// Use `ssrPrepass` to collect all uses of `useQuery`.
73+
// NOTE: See a workaround in 'ssr/server/react-rendering.js' file that we had to implement,
74+
// so that prepass would ignore React's useInsertionEffect hook.
7375
await ssrPrepass(appJSX)
7476
res.__performanceTimer.mark(PERFORMANCE_MARKS.reactQueryPrerender, 'end')
77+
7578
const queryCache = queryClient.getQueryCache()
7679
const queries = queryCache.getAll().filter((q) => q.options.enabled !== false)
7780
await Promise.all(

0 commit comments

Comments
 (0)