Skip to content

Commit

Permalink
Add shared dynamic api (#39163)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
huozhi committed Jul 29, 2022
1 parent 48405e6 commit b1a5821
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 24 deletions.
21 changes: 21 additions & 0 deletions packages/next/client/components/shared/dynamic/index.tsx
@@ -0,0 +1,21 @@
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
}
@@ -0,0 +1,7 @@
import dynamic from 'next/dist/client/components/shared/dynamic'

const Dynamic = dynamic(() => import('../text-dynamic.client'))

export function NextDynamicClientComponent() {
return <Dynamic />
}
@@ -0,0 +1,7 @@
import dynamic from 'next/dist/client/components/shared/dynamic'

const Dynamic = dynamic(() => import('../text-dynamic.server'))

export function NextDynamicServerComponent() {
return <Dynamic />
}
@@ -1,13 +1,13 @@
import { useState, lazy } from 'react'

const Lazy = lazy(() => import('./lazy.client.js'))
const Lazy = lazy(() => import('../text-lazy.client.js'))

export function LazyClientComponent() {
let [state] = useState('client')
return (
<>
<Lazy />
<p className="hi">hello from modern the {state}</p>
<p className="hi">hello from {state}</p>
</>
)
}
11 changes: 0 additions & 11 deletions test/e2e/app-dir/app/app/dashboard/index/dynamic.client.js

This file was deleted.

This file was deleted.

6 changes: 4 additions & 2 deletions test/e2e/app-dir/app/app/dashboard/index/page.server.js
@@ -1,10 +1,12 @@
import { LazyClientComponent } from './react-lazy.client'
import { NextDynamicClientComponent } from './next-dynamic.client'
import { LazyClientComponent } from './dynamic-imports/react-lazy.client'
import { NextDynamicServerComponent } from './dynamic-imports/dynamic.server'
import { NextDynamicClientComponent } from './dynamic-imports/dynamic.client'

export default function DashboardIndexPage() {
return (
<>
<p>hello from app/dashboard/index</p>
<NextDynamicServerComponent />
<NextDynamicClientComponent />
<LazyClientComponent />
</>
Expand Down
11 changes: 11 additions & 0 deletions test/e2e/app-dir/app/app/dashboard/index/text-dynamic.client.js
@@ -0,0 +1,11 @@
import { useState } from 'react'
import styles from './dynamic.module.css'

export default function Dynamic() {
let [state] = useState('dynamic on client')
return (
<p id="css-text-dynamic-client" className={styles.dynamic}>
{`hello from ${state}`}
</p>
)
}
@@ -0,0 +1,9 @@
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>
)
}
7 changes: 5 additions & 2 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -67,8 +67,11 @@ describe('app dir', () => {
it('should serve /index as separate page', async () => {
const html = await renderViaHTTP(next.url, '/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')
// 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
Expand All @@ -77,7 +80,7 @@ describe('app dir', () => {

expect(
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)')
expect(
Expand Down

0 comments on commit b1a5821

Please sign in to comment.