Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shared dynamic api #39163

Merged
merged 3 commits into from Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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