Unverified Commit b1a5821e authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Add shared dynamic api (#39163)

Add `next/dist/client/components/shared/dynamic` as shared api which server and client components can both support. 
Dynamic imports will be part of flight on server side.
This pr doesn't contain preloading part for client components.
parent 48405e6b
import React from 'react'
export type LoaderComponent<P = {}> = Promise<{
default: React.ComponentType<P>
}>
export type Loader<P = {}> = () => LoaderComponent<P>
export type DynamicOptions<P = {}> = {
loader?: Loader<P>
}
export type LoadableComponent<P = {}> = React.ComponentType<P>
export default function dynamic<P = {}>(
loader: Loader<P>
): React.ComponentType<P> {
const LazyLoadable = React.lazy(loader)
return LazyLoadable
}
import dynamic from 'next/dynamic' import dynamic from 'next/dist/client/components/shared/dynamic'
const Dynamic = dynamic(() => import('./dynamic.client')) const Dynamic = dynamic(() => import('../text-dynamic.client'))
export function NextDynamicClientComponent() { export function NextDynamicClientComponent() {
return <Dynamic /> return <Dynamic />
......
import dynamic from 'next/dist/client/components/shared/dynamic'
const Dynamic = dynamic(() => import('../text-dynamic.server'))
export function NextDynamicServerComponent() {
return <Dynamic />
}
import { useState, lazy } from 'react' import { useState, lazy } from 'react'
const Lazy = lazy(() => import('./lazy.client.js')) const Lazy = lazy(() => import('../text-lazy.client.js'))
export function LazyClientComponent() { export function LazyClientComponent() {
let [state] = useState('client') let [state] = useState('client')
return ( return (
<> <>
<Lazy /> <Lazy />
<p className="hi">hello from modern the {state}</p> <p className="hi">hello from {state}</p>
</> </>
) )
} }
import { LazyClientComponent } from './react-lazy.client' import { LazyClientComponent } from './dynamic-imports/react-lazy.client'
import { NextDynamicClientComponent } from './next-dynamic.client' import { NextDynamicServerComponent } from './dynamic-imports/dynamic.server'
import { NextDynamicClientComponent } from './dynamic-imports/dynamic.client'
export default function DashboardIndexPage() { export default function DashboardIndexPage() {
return ( return (
<> <>
<p>hello from app/dashboard/index</p> <p>hello from app/dashboard/index</p>
<NextDynamicServerComponent />
<NextDynamicClientComponent /> <NextDynamicClientComponent />
<LazyClientComponent /> <LazyClientComponent />
</> </>
......
...@@ -2,10 +2,10 @@ import { useState } from 'react' ...@@ -2,10 +2,10 @@ import { useState } from 'react'
import styles from './dynamic.module.css' import styles from './dynamic.module.css'
export default function Dynamic() { export default function Dynamic() {
let [state] = useState('next dynamic') let [state] = useState('dynamic on client')
return ( return (
<p id="css-text-dynamic" className={styles.dynamic}> <p id="css-text-dynamic-client" className={styles.dynamic}>
hello from modern the {state} {`hello from ${state}`}
</p> </p>
) )
} }
import styles from './dynamic.module.css'
export default function Dynamic() {
return (
<p id="css-text-dynamic-server" className={styles.dynamic}>
hello from dynamic on server
</p>
)
}
...@@ -67,8 +67,11 @@ describe('app dir', () => { ...@@ -67,8 +67,11 @@ describe('app dir', () => {
it('should serve /index as separate page', async () => { it('should serve /index as separate page', async () => {
const html = await renderViaHTTP(next.url, '/dashboard/index') const html = await renderViaHTTP(next.url, '/dashboard/index')
expect(html).toContain('hello from app/dashboard/index') expect(html).toContain('hello from app/dashboard/index')
// should load chunks generated via async import correctly // should load chunks generated via async import correctly with React.lazy
expect(html).toContain('hello from lazy') expect(html).toContain('hello from lazy')
// should support `dynamic` in both server and client components
expect(html).toContain('hello from dynamic on server')
expect(html).toContain('hello from dynamic on client')
}) })
// TODO-APP: handle css modules fouc in dev // TODO-APP: handle css modules fouc in dev
...@@ -77,7 +80,7 @@ describe('app dir', () => { ...@@ -77,7 +80,7 @@ describe('app dir', () => {
expect( expect(
await browser.eval( await browser.eval(
`window.getComputedStyle(document.querySelector('#css-text-dynamic')).color` `window.getComputedStyle(document.querySelector('#css-text-dynamic-server')).color`
) )
).toBe('rgb(0, 0, 255)') ).toBe('rgb(0, 0, 255)')
expect( expect(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment