rsnext/packages/next/client/components
Jiachi Liu cd0ebd8e8c
Implement loadable with lazy and suspense for next dynamic (#42589)
### Summary

Migrate `next/dynamic` to implementation based on `React.lazy` and
`Suspense`. Then it becomes easier to migrate the existing code in pages
to layouts. Then we can support both `ssr` and `loading` option for
`next/dynamic`.

For `loading` option, it will work like `Suspense`'s `fallback` property

```js
<Suspense fallback={loading}>
  <DynamicComponent />
 </Suspense>
```

For `ssr` option, by default `React.lazy` supports SSR, but we'll
disable the `ssr: false` case for dynamic import in server components
since there's no client side involved.

Then we don't need `suspense` option anymore as react >= 18 is always
required. Mark it as deprecated.

It also supports to load client component dynamically in server
components now.

#### Code code changes
* switch loadable component to `lazy` + `Suspense`
* will make sure it's retuning a module from `loader()` to
`loader().then(mod => ({ default: mod.default || mod }))` since `lazy()`
only accepts loader returning a module
* Inside suspense boundary, throwing an error for ssr: false, catch the
error on server and client side and ignore it.
* Ignore options like ssr: false for server components since they're on
server, doesn't make sense
* Remove legacy dynamic related transform
#### Feature changes
* `next/dynamic` will work in the same way across the board (appDir and
pages)
* For the throwing error, will make it become a API that throws error
later in the future, so users can customize more with `Suspense`
* You can load client components now in server components with dynamic.
Resolves #43147

#### Tests
* existing dynamic tests all work
* add case: import client component and load through next/dynamic in
server components

### Issues
2022-12-07 19:42:10 +01:00
..
react-dev-overlay Revert "Adding head element checking for root layout" (#43760) 2022-12-06 15:41:29 +01:00
app-router-headers.ts Update cache handling for app (#43659) 2022-12-02 23:32:49 -08:00
app-router.tsx Fix typo in comment (#43685) 2022-12-04 13:34:15 -08:00
error-boundary.tsx Display error digest if presented (#43742) 2022-12-07 17:34:25 +01:00
error.tsx Add default not found template (#41750) 2022-10-24 15:46:59 -07:00
headers.ts fix: Dynamic Usage Error when using previewData with generateStaticParams and appDir (#43395) 2022-11-30 20:28:05 -08:00
hooks-server-context.ts Upgrade react@experimental (#40885) 2022-09-25 21:35:05 +02:00
infinite-promise.ts Handle redirects in new router (#40396) 2022-09-20 15:28:07 +02:00
layout-router.tsx Implement loadable with lazy and suspense for next dynamic (#42589) 2022-12-07 19:42:10 +01:00
match-segments.ts Flush styles effects (#39268) 2022-08-03 16:21:20 +00:00
navigation.ts Remove resolved app directory todos (#43672) 2022-12-03 20:34:53 +00:00
not-found.ts Add never return type for redirect() and notFound() (#42009) 2022-10-27 15:07:59 -07:00
redirect.ts Add never return type for redirect() and notFound() (#42009) 2022-10-27 15:07:59 -07:00
reducer.ts Remove resolved app directory todos (#43672) 2022-12-03 20:34:53 +00:00
render-from-template-context.tsx Client component directive: use client (#41333) 2022-10-11 10:26:45 -07:00
request-async-storage.ts Replace global with globalThis (#42627) 2022-11-08 14:45:30 +01:00
static-generation-async-storage.ts Update cache handling for app (#43659) 2022-12-02 23:32:49 -08:00
static-generation-bailout.ts Add force-static handling for app dir (#43061) 2022-11-18 09:47:44 -08:00
use-reducer-with-devtools.ts misc: make useReducerWithDevtools noop on server (#41019) 2022-09-29 10:08:44 +00:00