rsnext/packages/next/src
Jiachi Liu 142050ddc0
Fix css FOUC in dynamic component (#64294)
### What

CSS imports in components that loaded by `next/dynamic` in client
components will cause the css are missing initial in
SSR, and loaded later on client side which will lead to FOUC. This PR
fixes the issue and get CSS preloaded in the SSR for dynamic components.

### Why

The CSS from client components that created through `next/dynamic` are
not collected in the SSR, unlike RSC rendering we already collect the
CSS resources for each entry so we included them in the server rendering
so the styles are availble at that time. But for client components, we
didn't traverse all the client components and collect the CSS resources.

In pages router we kinda collect all the dynamic imports and preload
them during SSR, but this approach is not able to be applied to app
router due to different architecture. Since we already have all the
dynamic imports info and their related chunks in
react-loadable-manifest, so we can do the similar "preloading" thing in
app router. We use the current dynamic module key (`app/page.js ->
../components/foo.js`) which created by SWC transform and match it in
the react loadable manifest that accessed from `AsyncLocalStorage`, to
get the css files created by webpack then render them as preload
styleshee links. In this way we can SSR all the related CSS resources
for dynamic client components.

The reason we pass down the react loadable manifest through
`AsyncLocalStorage` is that it's sort of exclude the manifest from RSC
payload as it's not required for hydration, but only required for SSR.

Note: this issue only occurred in dynamic rendering case for client
components.

### Other Changes Overview

- Change the react loadable manifest key from pages dir based relative
path to a source dir based relative path, to support cases with both
directory or only one of them

Closes NEXT-2578
Fixes #61212
Fixes #61111
Fixes #62940

Replacement for #64021 but only with production test
2024-04-10 16:16:20 +02:00
..
api Use SWC to valid client next/navigation hooks usage in server components (#63160) 2024-03-13 16:37:36 +01:00
bin chore(cli): fix the order --experimental-debug-memory-usage so it's alphabetical (#64264) 2024-04-09 17:28:12 +00:00
build Fix css FOUC in dynamic component (#64294) 2024-04-10 16:16:20 +02:00
bundles chore: upgrade webpack to 5.90.0 (#61109) 2024-01-25 20:18:12 +01:00
cli Add a mode to next build to make it easier to debug memory issues (#63869) 2024-04-09 06:53:34 -07:00
client Fix css FOUC in dynamic component (#64294) 2024-04-10 16:16:20 +02:00
compiled chore: upgrade @edge-runtime/cookies (#63602) 2024-03-22 10:58:49 +00:00
experimental/testmode Enable all pages under the browser context to proxy to the test proxy (#63610) 2024-03-22 22:16:17 +01:00
export Fix status code for /_not-found route (#64058) 2024-04-04 17:35:27 +02:00
lib Ensure configuration is checked for Turbopack build (#64247) 2024-04-10 10:09:03 +02:00
pages fix encoding for filenames containing ? or # (#58293) 2024-04-05 10:25:30 -07:00
server Fix css FOUC in dynamic component (#64294) 2024-04-10 16:16:20 +02:00
shared/lib Fix css FOUC in dynamic component (#64294) 2024-04-10 16:16:20 +02:00
telemetry add telemetry events for ppr & staleTimes experimental flags (#63981) 2024-04-02 21:11:47 +00:00
trace fixes to next.js tracing (#63673) 2024-03-27 16:23:56 +00:00