Unverified Commit 72003a79 authored by Jimmy Miller's avatar Jimmy Miller Committed by GitHub

Lazy dynamic imports for ssr (#98836)

Enables lazy imports for ssr.
https://github.com/vercel/next.js/pull/97305 This was the old Pr for
this. Tried to incorporate the feedback there. The changes here were
from testing on v0/chat
parent ed2c6b9b
......@@ -1574,11 +1574,52 @@ impl AppEndpoint {
)
.to_resolved()
.await?;
server_assets.extend(app_entry_chunks.all_assets().await?);
let expanded_app_entry_chunks = app_entry_chunks.expand_all_assets();
let all_app_entry_chunks = app_entry_chunks.all_assets().await?;
server_assets.extend(all_app_entry_chunks.iter().copied());
let app_entry_chunk_group_ref = app_entry_chunks.await?;
let app_entry_chunks = app_entry_chunk_group_ref.assets;
let app_entry_chunks_ref = app_entry_chunks.await?;
if is_app_page
&& runtime == NextRuntime::NodeJs
&& *project
.next_config()
.turbopack_lazy_dynamic_imports_ssr(*project.next_mode().await?)
.await?
{
let rsc_chunks = expanded_app_entry_chunks
.await?
.iter()
.copied()
.map(async |asset| {
let path = asset.path().owned().await?;
Ok(node_root
.get_path_to(&path)
.is_some_and(|path| {
path.starts_with("server/chunks/") && path.ends_with(".js")
})
.then_some(asset))
})
.try_join()
.await?
.into_iter()
.flatten()
.collect::<Vec<_>>();
let rsc_hmr_chunks = project
.server_chunking_context(true)
.server_hmr_chunk_list(
server_path.join(&format!(
"app{}/rsc-dynamic-imports.js",
app_entry.original_name
))?,
Vc::cell(rsc_chunks),
)
.to_resolved()
.await?;
server_assets.insert(rsc_hmr_chunks);
}
// these references are important for turbotrace
let mut client_reference_manifest = None;
......@@ -2195,11 +2236,21 @@ impl Endpoint for AppEndpoint {
.unwrap_or(&server_entry_path)
.strip_suffix(".js")
.unwrap_or(&server_entry_path);
let mut server_hmr_entry_paths = vec![
format!("{hmr_entry_path}.js").into(),
format!("{hmr_entry_path}/client-components-ssr.js").into(),
];
if matches!(this.ty, AppEndpointType::Page { .. })
&& *project
.next_config()
.turbopack_lazy_dynamic_imports_ssr(*project.next_mode().await?)
.await?
{
server_hmr_entry_paths
.push(format!("{hmr_entry_path}/rsc-dynamic-imports.js").into());
}
EndpointOutputPaths::NodeJs {
server_hmr_entry_paths: vec![
format!("{hmr_entry_path}.js").into(),
format!("{hmr_entry_path}/client-components-ssr.js").into(),
],
server_hmr_entry_paths,
server_entry_path,
server_paths,
client_paths,
......
......@@ -1824,6 +1824,9 @@ impl Project {
hash_salt: self.next_config().output_hash_salt().to_resolved().await?,
style_groups_algorithm: self.next_config().css_chunking().owned().await?,
per_page_module_graph: self.per_page_module_graph(),
lazy_dynamic_imports: self
.next_config()
.turbopack_lazy_dynamic_imports_ssr(*self.next_mode().await?),
};
Ok(if client_assets {
get_server_chunking_context_with_client_assets(options)
......
......@@ -1190,6 +1190,10 @@ pub struct ExperimentalConfig {
/// Compile client dynamic import targets when their runtime proxy is first activated.
/// Development only.
turbopack_lazy_dynamic_imports: Option<bool>,
/// Compile dynamic import targets when they are first reached during SSR.
/// Development only.
#[serde(rename = "turbopackLazyDynamicImportsSSR")]
turbopack_lazy_dynamic_imports_ssr: Option<bool>,
turbopack_import_type_bytes: Option<bool>,
/// Disable automatic configuration of the sass loader.
#[serde(default)]
......@@ -2478,6 +2482,17 @@ impl NextConfig {
)
}
#[turbo_tasks::function]
pub async fn turbopack_lazy_dynamic_imports_ssr(&self, next_mode: NextMode) -> Vc<bool> {
Vc::cell(
next_mode.is_development()
&& self
.experimental
.turbopack_lazy_dynamic_imports_ssr
.unwrap_or(false),
)
}
#[turbo_tasks::function]
pub async fn turbopack_import_type_bytes(&self) -> Vc<bool> {
Vc::cell(
......
......@@ -672,6 +672,10 @@ pub async fn get_server_module_options_context(
}
}
ServerContextType::AppSSR { app_dir, .. } => {
let lazy_compilation = matches!(next_runtime, NextRuntime::NodeJs)
&& *next_config
.turbopack_lazy_dynamic_imports_ssr(*next_mode)
.await?;
foreign_next_server_rules.extend(internal_custom_rules);
next_server_rules.extend(source_transform_rules);
......@@ -717,6 +721,7 @@ pub async fn get_server_module_options_context(
enable_decorators: Some(decorators_options.to_resolved().await?),
// React Compiler only optimizes the React client runtime, so skip it.
enable_rust_react_compiler: None,
lazy_compilation,
..module_options_context.ecmascript
},
enable_webpack_loaders,
......@@ -741,6 +746,10 @@ pub async fn get_server_module_options_context(
ecmascript_client_reference_transition_name,
..
} => {
let lazy_compilation = matches!(next_runtime, NextRuntime::NodeJs)
&& *next_config
.turbopack_lazy_dynamic_imports_ssr(*next_mode)
.await?;
let client_directive_transformer =
if let Some(name) = ecmascript_client_reference_transition_name {
Some(get_ecma_transform_rule(
......@@ -799,6 +808,7 @@ pub async fn get_server_module_options_context(
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options.to_resolved().await?),
enable_rust_react_compiler: None,
lazy_compilation,
..module_options_context.ecmascript
},
enable_webpack_loaders,
......@@ -1028,6 +1038,7 @@ pub struct ServerChunkingContextOptions {
pub hash_salt: ResolvedVc<RcStr>,
pub style_groups_algorithm: StyleGroupsAlgorithm,
pub per_page_module_graph: Vc<bool>,
pub lazy_dynamic_imports: Vc<bool>,
}
/// Like `get_server_chunking_context` but all assets are emitted as client assets (so `/_next`)
......@@ -1057,6 +1068,7 @@ pub async fn get_server_chunking_context_with_client_assets(
hash_salt,
style_groups_algorithm,
per_page_module_graph,
lazy_dynamic_imports,
} = options;
let css_url_suffix = css_url_suffix.to_resolved().await?;
......@@ -1106,7 +1118,8 @@ pub async fn get_server_chunking_context_with_client_assets(
// Per-page graphs each see only one page, so none of them can decide what the shared runtime
// chunk may leave out.
.shared_runtime_chunk(*per_page_module_graph.await?)
.worker_forwarded_globals(worker_forwarded_globals());
.worker_forwarded_globals(worker_forwarded_globals())
.manifest_chunks(*lazy_dynamic_imports.await?);
builder = builder.source_map_source_type(if next_mode.is_development() {
SourceMapSourceType::AbsoluteFileUri
......@@ -1167,6 +1180,8 @@ pub async fn get_server_chunking_context(
hash_salt,
style_groups_algorithm,
per_page_module_graph,
// TODO: Revisit this if we want to make more things lazy.
lazy_dynamic_imports: _,
} = options;
let css_url_suffix = css_url_suffix.to_resolved().await?;
let next_mode = mode.await?;
......
......@@ -446,6 +446,7 @@ export const experimentalSchema = {
turbopackClientSideNestedAsyncChunking: z.boolean().optional(),
turbopackServerSideNestedAsyncChunking: z.boolean().optional(),
turbopackLazyDynamicImports: z.boolean().optional(),
turbopackLazyDynamicImportsSSR: z.boolean().optional(),
turbopackImportTypeBytes: z.boolean().optional(),
turbopackUseBuiltinBabel: z.boolean().optional(),
turbopackUseBuiltinSass: z.boolean().optional(),
......
......@@ -1005,6 +1005,13 @@ export interface ExperimentalConfig {
*/
turbopackLazyDynamicImports?: boolean
/**
* Compile SSR dynamic import targets when they are first reached during server rendering in development.
*
* Defaults to `false`.
*/
turbopackLazyDynamicImportsSSR?: boolean
/**
* Enable filesystem cache for the turbopack dev server.
*
......
......@@ -8,6 +8,7 @@ import { fileURLToPath, pathToFileURL } from 'url'
import ws from 'next/dist/compiled/ws'
import type { OutputState } from '../../build/output/store'
import type { ActionManifest } from '../../build/webpack/plugins/flight-client-entry-plugin'
import { store as consoleStore } from '../../build/output/store'
import type {
CompilationError,
......@@ -35,7 +36,11 @@ import type {
} from '../../build/swc/types'
import { createDefineEnv, getBindingsSync } from '../../build/swc'
import * as Log from '../../build/output/log'
import { BLOCKED_PAGES } from '../../shared/lib/constants'
import {
BLOCKED_PAGES,
CLIENT_REFERENCE_MANIFEST,
SERVER_REFERENCE_MANIFEST,
} from '../../shared/lib/constants'
import {
getOverlayMiddleware,
getSourceMapMiddleware,
......@@ -43,7 +48,9 @@ import {
} from './middleware-turbopack'
import { PageNotFoundError } from '../../shared/lib/utils'
import { debounce } from '../utils'
import { clearManifestCache } from '../load-manifest.external'
import { clearManifestCache, loadManifest } from '../load-manifest.external'
import { loadClientReferenceManifestForPage } from '../load-components'
import { setManifestsSingleton } from '../app-render/manifests-singleton'
import { deleteCache } from './require-cache'
import {
dropDevValidationWorker,
......@@ -199,6 +206,9 @@ declare global {
* Sync with `turbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs/runtime/nodejs-globals.d.ts`.
*/
var __turbopack_server_hmr_handlers__: Map<string, unknown> | undefined
var __turbopack_ensure_chunk__:
| ((chunkPath: string) => void | Promise<void>)
| undefined
}
/**
......@@ -411,6 +421,8 @@ export async function createHotReloaderTurbopack(
const buildId = 'development'
const { nextConfig, dir: projectPath } = opts
const lazyDynamicImports = nextConfig.experimental.turbopackLazyDynamicImports
const lazyDynamicImportsSSR =
nextConfig.experimental.turbopackLazyDynamicImportsSSR
const bindings = getBindingsSync()
......@@ -621,6 +633,7 @@ export async function createHotReloaderTurbopack(
)
const assetMapper = new AssetMapper()
const serverAssetMapper = new AssetMapper()
// Deferred entries state management
const deferredEntriesConfig = nextConfig.experimental.deferredEntries
......@@ -2067,6 +2080,10 @@ export async function createHotReloaderTurbopack(
handleWrittenEndpoint: (id, result, forceDeleteCache) => {
currentWrittenEntrypoints.set(id, result.value)
assetMapper.setPathsForKey(id, result.value.clientPaths)
serverAssetMapper.setPathsForKey(
id,
result.value.serverPaths.map(({ path }) => path)
)
return clearRequireCache(id, result.value, {
force: forceDeleteCache,
})
......@@ -2145,6 +2162,10 @@ export async function createHotReloaderTurbopack(
handleWrittenEndpoint: (id, result, forceDeleteCache) => {
currentWrittenEntrypoints.set(id, result.value)
assetMapper.setPathsForKey(id, result.value.clientPaths)
serverAssetMapper.setPathsForKey(
id,
result.value.serverPaths.map(({ path }) => path)
)
shouldPullServerHmr ||= participatesInServerHmr(
id,
result.value
......@@ -2312,6 +2333,60 @@ export async function createHotReloaderTurbopack(
process.exit(1)
})
if (lazyDynamicImportsSSR) {
const previousEnsureChunk = globalThis.__turbopack_ensure_chunk__
const ensureChunk = async (chunkPath: string) => {
if (!chunkPath.includes('lazy-compilation-')) return
const keys = serverAssetMapper.getKeysByAsset(chunkPath)
if (keys.length === 0 || !(await project.activateLazyChunk(chunkPath))) {
return
}
for (const key of keys) {
const { page, type } = splitEntryKey(key)
await hotReloader.ensurePage({
page,
clientOnly: false,
definition: undefined,
})
if (type === 'app') {
clearManifestCache(
join(
distDir,
'server',
'app',
page.replace(/%5F/g, '_') +
'_' +
CLIENT_REFERENCE_MANIFEST +
'.js'
)
)
const clientReferenceManifest =
await loadClientReferenceManifestForPage(distDir, page, 1)
if (clientReferenceManifest) {
setManifestsSingleton({
page,
clientReferenceManifest,
serverActionsManifest: loadManifest<ActionManifest>(
join(distDir, 'server', SERVER_REFERENCE_MANIFEST + '.json'),
false
),
})
}
}
}
deleteCache([join(distDir, chunkPath)])
}
globalThis.__turbopack_ensure_chunk__ = ensureChunk
opts.onDevServerCleanup?.(async () => {
if (globalThis.__turbopack_ensure_chunk__ === ensureChunk) {
globalThis.__turbopack_ensure_chunk__ = previousEnsureChunk
}
})
}
let serverHmr: ReturnType<typeof setupServerHmr> | undefined
if (serverFastRefresh) {
serverHmr = setupServerHmr(project, {
......
export default function ConcurrentTarget() {
return <p id="concurrent-target">concurrent-lazy-marker-6c20</p>
}
'use client'
import { lazy } from 'react'
const SsrTarget = lazy(() => import('./ssr-target'))
const ConcurrentTarget = lazy(() => import('./concurrent-target'))
export function Host({ target }: { target?: string }) {
if (target === 'ssr') return <SsrTarget />
if (target === 'concurrent') return <ConcurrentTarget />
return <p id="ssr-idle">SSR target not rendered</p>
}
import { Host } from './host'
export default async function Page({
searchParams,
}: {
searchParams: Promise<{ target?: string }>
}) {
return <Host target={(await searchParams).target} />
}
export default function SsrTarget() {
return <p id="ssr-target">ssr-lazy-marker-4f31</p>
}
......@@ -10,8 +10,11 @@ import { getRedboxSource, retry, waitForRedbox } from 'next-test-utils'
patchFileDelay: 500,
})
async function assetsContaining(marker: string): Promise<string[]> {
const root = path.join(next.testDir, next.distDir, 'static')
async function assetsContaining(
marker: string,
outputDir: 'static' | 'server' = 'static'
): Promise<string[]> {
const root = path.join(next.testDir, next.distDir, outputDir)
const matches: string[] = []
async function walk(dir: string) {
......@@ -188,7 +191,40 @@ import { getRedboxSource, retry, waitForRedbox } from 'next-test-utils'
}
})
it('preserves client and server references behind a server dynamic import', async () => {
it('does not compile a server component dynamic import before it is reached', async () => {
const targetPath = path.join(
'app',
'server-graph',
'lazy-server-content.tsx'
)
const originalTarget = await next.readFile(targetPath)
try {
await next.patchFile(
targetPath,
`${originalTarget}\nexport const invalid = ;`
)
expect(await next.render('/server-graph')).toContain(
'reveal server graph'
)
expect(
await assetsContaining('server-graph-marker', 'server')
).toHaveLength(0)
} finally {
await next.patchFile(targetPath, originalTarget)
}
const renders = await Promise.all([
next.render('/server-graph?show=1'),
next.render('/server-graph?show=1'),
])
for (const html of renders) {
expect(html).toContain('server-graph-marker')
expect(html).toContain('hydrate-client')
expect(html).not.toContain('Could not find the module')
}
})
it('preserves client and server references behind a lazy server component import', async () => {
const browser = await next.browser('/server-graph')
expect(
......@@ -214,6 +250,29 @@ import { getRedboxSource, retry, waitForRedbox } from 'next-test-utils'
'hidden-action-result'
)
})
const targetPath = path.join(
'app',
'server-graph',
'lazy-server-content.tsx'
)
const originalTarget = await next.readFile(targetPath)
try {
await next.patchFile(
targetPath,
originalTarget.replace(
'server-graph-marker',
'updated-server-graph-marker'
)
)
await retry(async () => {
expect(await next.render('/server-graph?show=1')).toContain(
'updated-server-graph-marker'
)
}, 10000)
} finally {
await next.patchFile(targetPath, originalTarget)
}
})
it('does not parse a dynamic import target before activation', async () => {
......@@ -492,5 +551,59 @@ export const invalid = ;`
await next.patchFile(cssPath, originalCss)
}
})
it('activates dynamic imports reached during server rendering', async () => {
const hostPath = path.join('app', 'ssr-dynamic', 'host.tsx')
const targetPath = path.join('app', 'ssr-dynamic', 'ssr-target.tsx')
const originalHost = await next.readFile(hostPath)
const originalTarget = await next.readFile(targetPath)
try {
await next.patchFile(
targetPath,
`${originalTarget}\nexport const invalid = ;`
)
expect(await next.render('/ssr-dynamic')).toContain(
'SSR target not rendered'
)
expect(
await assetsContaining('ssr-lazy-marker-4f31', 'server')
).toHaveLength(0)
} finally {
await next.patchFile(targetPath, originalTarget)
}
await retry(async () => {
expect(await next.render('/ssr-dynamic?target=ssr')).toContain(
'ssr-lazy-marker-4f31'
)
})
expect(
await assetsContaining('ssr-lazy-marker-4f31', 'server')
).not.toHaveLength(0)
expect(await assetsContaining('ssr-lazy-marker-4f31')).toHaveLength(0)
try {
await next.patchFile(
hostPath,
originalHost.replace('<SsrTarget />', '<SsrTarget key="edited" />')
)
await retry(async () => {
expect(await next.render('/ssr-dynamic?target=ssr')).toContain(
'ssr-lazy-marker-4f31'
)
})
} finally {
await next.patchFile(hostPath, originalHost)
}
const renders = await Promise.all([
next.render('/ssr-dynamic?target=concurrent'),
next.render('/ssr-dynamic?target=concurrent'),
])
for (const html of renders) {
expect(html).toContain('concurrent-lazy-marker-6c20')
}
})
}
)
......@@ -4,6 +4,7 @@
const nextConfig = {
experimental: {
turbopackLazyDynamicImports: true,
turbopackLazyDynamicImportsSSR: true,
},
}
......
......@@ -38,6 +38,31 @@ nodeDevContextPrototype.c = devModuleCache
nodeDevContextPrototype.R = resolvePathFromModule
nodeDevContextPrototype.C = clearChunkCache
if (globalThis.__turbopack_ensure_chunk__ !== undefined) {
const chunksBeingEnsured = new Map<ChunkPath, Promise<void>>()
function loadChunkAsyncOnDemand<TModule extends Module>(
this: TurbopackBaseContext<TModule>,
chunkData: ChunkData
): Promise<void> {
const chunkPath = typeof chunkData === 'string' ? chunkData : chunkData.path
const ensureChunk = globalThis.__turbopack_ensure_chunk__
if (ensureChunk === undefined || chunkCache.has(chunkPath)) {
return loadChunkAsync.call(this, chunkData)
}
const ensured =
chunksBeingEnsured.get(chunkPath) ??
Promise.resolve()
.then(() => ensureChunk(chunkPath))
.finally(() => chunksBeingEnsured.delete(chunkPath))
chunksBeingEnsured.set(chunkPath, ensured)
return ensured.then(() => loadChunkAsync.call(this, chunkData))
}
nodeDevContextPrototype.l = loadChunkAsyncOnDemand
}
/**
* Instantiates a module in development mode using shared HMR logic.
*/
......
......@@ -21,6 +21,13 @@ declare global {
var __turbopack_server_hmr_handlers__:
| Map<string, { handler: (update: any) => void; chunkPrefix: string }>
| undefined
/**
* Compiles an on-demand chunk before the runtime requires it.
* Used together with EcmascriptOptionsContext.lazy_compilation
*/
var __turbopack_ensure_chunk__:
| ((chunkPath: ChunkPath) => void | Promise<void>)
| undefined
}
export {}
......@@ -1546,6 +1546,20 @@ nodeDevContextPrototype.M = moduleFactories;
nodeDevContextPrototype.c = devModuleCache;
nodeDevContextPrototype.R = resolvePathFromModule;
nodeDevContextPrototype.C = clearChunkCache;
if (globalThis.__turbopack_ensure_chunk__ !== undefined) {
const chunksBeingEnsured = new Map();
function loadChunkAsyncOnDemand(chunkData) {
const chunkPath = typeof chunkData === 'string' ? chunkData : chunkData.path;
const ensureChunk = globalThis.__turbopack_ensure_chunk__;
if (ensureChunk === undefined || chunkCache.has(chunkPath)) {
return loadChunkAsync.call(this, chunkData);
}
const ensured = chunksBeingEnsured.get(chunkPath) ?? Promise.resolve().then(()=>ensureChunk(chunkPath)).finally(()=>chunksBeingEnsured.delete(chunkPath));
chunksBeingEnsured.set(chunkPath, ensured);
return ensured.then(()=>loadChunkAsync.call(this, chunkData));
}
nodeDevContextPrototype.l = loadChunkAsyncOnDemand;
}
/**
* Instantiates a module in development mode using shared HMR logic.
*/ function instantiateModule(id, sourceType, sourceData) {
......
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