Unverified Commit 50bf604f authored by Jimmy Miller's avatar Jimmy Miller Committed by GitHub

Extend lazy server imports to route handlers and Pages (#99541)

Enable development-only lazy imports for Node.js route handlers, Pages
API routes, and Pages SSR while keeping startup entries eager.
parent 417f8c0d
...@@ -1267,7 +1267,10 @@ impl AppEndpoint { ...@@ -1267,7 +1267,10 @@ impl AppEndpoint {
let ssr_chunking_context = if process_ssr { let ssr_chunking_context = if process_ssr {
Some( Some(
match runtime { match runtime {
NextRuntime::NodeJs => Vc::upcast(project.server_chunking_context(true)), NextRuntime::NodeJs => Vc::upcast(project.server_chunking_context(
true, // client_assets
true, // supports_lazy_dynamic_imports
)),
NextRuntime::Edge => this NextRuntime::Edge => this
.app_project .app_project
.project() .project()
...@@ -1378,7 +1381,10 @@ impl AppEndpoint { ...@@ -1378,7 +1381,10 @@ impl AppEndpoint {
original_name = app_entry.original_name original_name = app_entry.original_name
))?; ))?;
let ssr_hmr_chunks = project let ssr_hmr_chunks = project
.server_chunking_context(process_client_assets) .server_chunking_context(
process_client_assets,
true, // supports_lazy_dynamic_imports
)
.server_hmr_chunk_list( .server_hmr_chunk_list(
ssr_hmr_chunk_list_path, ssr_hmr_chunk_list_path,
Vc::cell(ssr_client_reference_chunks), Vc::cell(ssr_client_reference_chunks),
...@@ -1558,14 +1564,13 @@ impl AppEndpoint { ...@@ -1558,14 +1564,13 @@ impl AppEndpoint {
let app_entry_chunks = app_entry_chunk_group_ref.assets; let app_entry_chunks = app_entry_chunk_group_ref.assets;
let app_entry_chunks_ref = app_entry_chunks.await?; let app_entry_chunks_ref = app_entry_chunks.await?;
if is_app_page if runtime == NextRuntime::NodeJs
&& runtime == NextRuntime::NodeJs
&& *project && *project
.next_config() .next_config()
.turbopack_lazy_dynamic_imports_ssr(*project.next_mode().await?) .turbopack_lazy_dynamic_imports_ssr(*project.next_mode().await?)
.await? .await?
{ {
let rsc_chunks = expanded_app_entry_chunks let dynamic_import_chunks = expanded_app_entry_chunks
.await? .await?
.iter() .iter()
.copied() .copied()
...@@ -1583,18 +1588,21 @@ impl AppEndpoint { ...@@ -1583,18 +1588,21 @@ impl AppEndpoint {
.into_iter() .into_iter()
.flatten() .flatten()
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let rsc_hmr_chunks = project let dynamic_import_hmr_chunks = project
.server_chunking_context(true) .server_chunking_context(
process_client_assets,
true, // supports_lazy_dynamic_imports
)
.server_hmr_chunk_list( .server_hmr_chunk_list(
server_path.join(&format!( server_path.join(&format!(
"app{}/rsc-dynamic-imports.js", "app{}/dynamic-imports.js",
app_entry.original_name app_entry.original_name
))?, ))?,
Vc::cell(rsc_chunks), Vc::cell(dynamic_import_chunks),
) )
.to_resolved() .to_resolved()
.await?; .await?;
server_assets.insert(rsc_hmr_chunks); server_assets.insert(dynamic_import_hmr_chunks);
} }
// these references are important for turbotrace // these references are important for turbotrace
...@@ -2221,14 +2229,13 @@ impl Endpoint for AppEndpoint { ...@@ -2221,14 +2229,13 @@ impl Endpoint for AppEndpoint {
format!("{hmr_entry_path}.js").into(), format!("{hmr_entry_path}.js").into(),
format!("{hmr_entry_path}/client-components-ssr.js").into(), format!("{hmr_entry_path}/client-components-ssr.js").into(),
]; ];
if matches!(this.ty, AppEndpointType::Page { .. }) if *project
&& *project .next_config()
.next_config() .turbopack_lazy_dynamic_imports_ssr(*project.next_mode().await?)
.turbopack_lazy_dynamic_imports_ssr(*project.next_mode().await?) .await?
.await?
{ {
server_hmr_entry_paths server_hmr_entry_paths
.push(format!("{hmr_entry_path}/rsc-dynamic-imports.js").into()); .push(format!("{hmr_entry_path}/dynamic-imports.js").into());
} }
EndpointOutputPaths::NodeJs { EndpointOutputPaths::NodeJs {
server_hmr_entry_paths, server_hmr_entry_paths,
......
...@@ -118,7 +118,10 @@ impl InstrumentationEndpoint { ...@@ -118,7 +118,10 @@ impl InstrumentationEndpoint {
async fn node_chunk(self: Vc<Self>) -> Result<Vc<Box<dyn OutputAsset>>> { async fn node_chunk(self: Vc<Self>) -> Result<Vc<Box<dyn OutputAsset>>> {
let this = self.await?; let this = self.await?;
let chunking_context = this.project.server_chunking_context(false); let chunking_context = this.project.server_chunking_context(
false, // client_assets
false, // supports_lazy_dynamic_imports
);
let userland_module = self.entry_module().to_resolved().await?; let userland_module = self.entry_module().to_resolved().await?;
let module_graph = this.project.module_graph(*userland_module); let module_graph = this.project.module_graph(*userland_module);
......
...@@ -130,7 +130,10 @@ impl MiddlewareEndpoint { ...@@ -130,7 +130,10 @@ impl MiddlewareEndpoint {
async fn node_chunk(self: Vc<Self>) -> Result<Vc<Box<dyn OutputAsset>>> { async fn node_chunk(self: Vc<Self>) -> Result<Vc<Box<dyn OutputAsset>>> {
let this = self.await?; let this = self.await?;
let chunking_context = this.project.server_chunking_context(false); let chunking_context = this.project.server_chunking_context(
false, // client_assets
false, // supports_lazy_dynamic_imports
);
let userland_module = self.entry_module().to_resolved().await?; let userland_module = self.entry_module().to_resolved().await?;
let module_graph = this.project.module_graph(*userland_module); let module_graph = this.project.module_graph(*userland_module);
......
...@@ -1280,7 +1280,10 @@ impl PageEndpoint { ...@@ -1280,7 +1280,10 @@ impl PageEndpoint {
.node_root() .node_root()
.await? .await?
.join("server")?, .join("server")?,
project.server_chunking_context(true), project.server_chunking_context(
true, // client_assets
true, // supports_lazy_dynamic_imports
),
project.edge_chunking_context(true), project.edge_chunking_context(true),
)) ))
} }
...@@ -1296,7 +1299,10 @@ impl PageEndpoint { ...@@ -1296,7 +1299,10 @@ impl PageEndpoint {
.node_root() .node_root()
.await? .await?
.join("server/data")?, .join("server/data")?,
this.pages_project.project().server_chunking_context(true), this.pages_project.project().server_chunking_context(
true, // client_assets
true, // supports_lazy_dynamic_imports
),
this.pages_project.project().edge_chunking_context(true), this.pages_project.project().edge_chunking_context(true),
)) ))
} }
...@@ -1312,7 +1318,10 @@ impl PageEndpoint { ...@@ -1312,7 +1318,10 @@ impl PageEndpoint {
.node_root() .node_root()
.await? .await?
.join("server")?, .join("server")?,
this.pages_project.project().server_chunking_context(false), this.pages_project.project().server_chunking_context(
false, // client_assets
true, // supports_lazy_dynamic_imports
),
this.pages_project.project().edge_chunking_context(false), this.pages_project.project().edge_chunking_context(false),
)) ))
} }
......
...@@ -1795,10 +1795,13 @@ impl Project { ...@@ -1795,10 +1795,13 @@ impl Project {
))) )))
} }
// Proxy and instrumentation stay eager: the lazy activation hook rebuilds route entries,
// not these startup entries.
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) async fn server_chunking_context( pub(super) async fn server_chunking_context(
self: Vc<Self>, self: Vc<Self>,
client_assets: bool, client_assets: bool,
supports_lazy_dynamic_imports: bool,
) -> Result<Vc<NodeJsChunkingContext>> { ) -> Result<Vc<NodeJsChunkingContext>> {
let css_url_suffix = self.next_config().asset_suffix_path(); let css_url_suffix = self.next_config().asset_suffix_path();
let options = ServerChunkingContextOptions { let options = ServerChunkingContextOptions {
...@@ -1829,9 +1832,11 @@ impl Project { ...@@ -1829,9 +1832,11 @@ impl Project {
hash_salt: self.next_config().output_hash_salt().to_resolved().await?, hash_salt: self.next_config().output_hash_salt().to_resolved().await?,
style_groups_algorithm: self.next_config().css_chunking().owned().await?, style_groups_algorithm: self.next_config().css_chunking().owned().await?,
per_page_module_graph: self.per_page_module_graph(), per_page_module_graph: self.per_page_module_graph(),
lazy_dynamic_imports: self lazy_dynamic_imports: supports_lazy_dynamic_imports
.next_config() && *self
.turbopack_lazy_dynamic_imports_ssr(*self.next_mode().await?), .next_config()
.turbopack_lazy_dynamic_imports_ssr(*self.next_mode().await?)
.await?,
}; };
Ok(if client_assets { Ok(if client_assets {
get_server_chunking_context_with_client_assets(options) get_server_chunking_context_with_client_assets(options)
...@@ -1889,7 +1894,10 @@ impl Project { ...@@ -1889,7 +1894,10 @@ impl Project {
) -> Vc<Box<dyn ChunkingContext>> { ) -> Vc<Box<dyn ChunkingContext>> {
match runtime { match runtime {
NextRuntime::Edge => self.edge_chunking_context(client_assets), NextRuntime::Edge => self.edge_chunking_context(client_assets),
NextRuntime::NodeJs => Vc::upcast(self.server_chunking_context(client_assets)), NextRuntime::NodeJs => Vc::upcast(self.server_chunking_context(
client_assets,
true, // supports_lazy_dynamic_imports
)),
} }
} }
......
...@@ -591,6 +591,10 @@ pub async fn get_server_module_options_context( ...@@ -591,6 +591,10 @@ pub async fn get_server_module_options_context(
..Default::default() ..Default::default()
}; };
let lazy_compilation = matches!(next_runtime, NextRuntime::NodeJs)
&& *next_config
.turbopack_lazy_dynamic_imports_ssr(*next_mode)
.await?;
let module_options_context = match ty { let module_options_context = match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesApi { .. } => { ServerContextType::Pages { .. } | ServerContextType::PagesApi { .. } => {
next_server_rules.extend(source_transform_rules); next_server_rules.extend(source_transform_rules);
...@@ -650,6 +654,7 @@ pub async fn get_server_module_options_context( ...@@ -650,6 +654,7 @@ pub async fn get_server_module_options_context(
enable_typescript_transform: Some(tsconfig), enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options.to_resolved().await?), enable_decorators: Some(decorators_options.to_resolved().await?),
enable_rust_react_compiler: None, enable_rust_react_compiler: None,
lazy_compilation,
..module_options_context.ecmascript ..module_options_context.ecmascript
}, },
enable_webpack_loaders, enable_webpack_loaders,
...@@ -670,10 +675,6 @@ pub async fn get_server_module_options_context( ...@@ -670,10 +675,6 @@ pub async fn get_server_module_options_context(
} }
} }
ServerContextType::AppSSR { app_dir, .. } => { 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); foreign_next_server_rules.extend(internal_custom_rules);
next_server_rules.extend(source_transform_rules); next_server_rules.extend(source_transform_rules);
...@@ -744,10 +745,6 @@ pub async fn get_server_module_options_context( ...@@ -744,10 +745,6 @@ pub async fn get_server_module_options_context(
ecmascript_client_reference_transition_name, 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 = let client_directive_transformer =
if let Some(name) = ecmascript_client_reference_transition_name { if let Some(name) = ecmascript_client_reference_transition_name {
Some(get_ecma_transform_rule( Some(get_ecma_transform_rule(
...@@ -885,6 +882,7 @@ pub async fn get_server_module_options_context( ...@@ -885,6 +882,7 @@ pub async fn get_server_module_options_context(
enable_typescript_transform: Some(tsconfig), enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options.to_resolved().await?), enable_decorators: Some(decorators_options.to_resolved().await?),
enable_rust_react_compiler: None, enable_rust_react_compiler: None,
lazy_compilation,
..module_options_context.ecmascript ..module_options_context.ecmascript
}, },
enable_webpack_loaders, enable_webpack_loaders,
...@@ -1036,7 +1034,7 @@ pub struct ServerChunkingContextOptions { ...@@ -1036,7 +1034,7 @@ pub struct ServerChunkingContextOptions {
pub hash_salt: ResolvedVc<RcStr>, pub hash_salt: ResolvedVc<RcStr>,
pub style_groups_algorithm: StyleGroupsAlgorithm, pub style_groups_algorithm: StyleGroupsAlgorithm,
pub per_page_module_graph: Vc<bool>, pub per_page_module_graph: Vc<bool>,
pub lazy_dynamic_imports: Vc<bool>, pub lazy_dynamic_imports: bool,
} }
/// Like `get_server_chunking_context` but all assets are emitted as client assets (so `/_next`) /// Like `get_server_chunking_context` but all assets are emitted as client assets (so `/_next`)
...@@ -1117,7 +1115,7 @@ pub async fn get_server_chunking_context_with_client_assets( ...@@ -1117,7 +1115,7 @@ pub async fn get_server_chunking_context_with_client_assets(
// chunk may leave out. // chunk may leave out.
.shared_runtime_chunk(*per_page_module_graph.await?) .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?); .manifest_chunks(lazy_dynamic_imports);
builder = builder.source_map_source_type(if next_mode.is_development() { builder = builder.source_map_source_type(if next_mode.is_development() {
SourceMapSourceType::AbsoluteFileUri SourceMapSourceType::AbsoluteFileUri
...@@ -1178,8 +1176,7 @@ pub async fn get_server_chunking_context( ...@@ -1178,8 +1176,7 @@ pub async fn get_server_chunking_context(
hash_salt, hash_salt,
style_groups_algorithm, style_groups_algorithm,
per_page_module_graph, per_page_module_graph,
// TODO: Revisit this if we want to make more things lazy. lazy_dynamic_imports,
lazy_dynamic_imports: _,
} = options; } = options;
let css_url_suffix = css_url_suffix.to_resolved().await?; let css_url_suffix = css_url_suffix.to_resolved().await?;
let next_mode = mode.await?; let next_mode = mode.await?;
...@@ -1232,7 +1229,8 @@ pub async fn get_server_chunking_context( ...@@ -1232,7 +1229,8 @@ pub async fn get_server_chunking_context(
// Per-page graphs each see only one page, so none of them can decide what the shared runtime // Per-page graphs each see only one page, so none of them can decide what the shared runtime
// chunk may leave out. // chunk may leave out.
.shared_runtime_chunk(*per_page_module_graph.await?) .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);
if next_mode.is_development() { if next_mode.is_development() {
builder = builder.source_map_source_type(SourceMapSourceType::AbsoluteFileUri); builder = builder.source_map_source_type(SourceMapSourceType::AbsoluteFileUri);
......
export async function GET(request: Request) {
if (!new URL(request.url).searchParams.has('load')) {
return Response.json({ value: 'idle' })
}
const { value } = await import('./target')
return Response.json({ value })
}
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { value } = await import('./lib/instrumentation-target')
console.log(value)
}
}
...@@ -7,7 +7,6 @@ import { getRedboxSource, retry, waitForRedbox } from 'next-test-utils' ...@@ -7,7 +7,6 @@ import { getRedboxSource, retry, waitForRedbox } from 'next-test-utils'
() => { () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
patchFileDelay: 500,
}) })
async function assetsContaining( async function assetsContaining(
...@@ -37,6 +36,146 @@ import { getRedboxSource, retry, waitForRedbox } from 'next-test-utils' ...@@ -37,6 +36,146 @@ import { getRedboxSource, retry, waitForRedbox } from 'next-test-utils'
return matches return matches
} }
it('loads instrumentation dynamic imports during startup', async () => {
expect(
await next.fetch('/api/lazy-import').then((res) => res.status)
).toBe(200)
await retry(async () => {
expect(next.cliOutput).toContain(
'instrumentation-startup-target-marker'
)
})
})
it.each([
{
name: 'route handler',
entryPath: 'app/api/lazy-import/route.ts',
targetPath: 'app/api/lazy-import/target.ts',
route: '/api/lazy-import',
marker: 'route-lazy-target-marker',
},
{
name: 'Pages API',
entryPath: 'pages/api/lazy-import-pages.ts',
targetPath: 'lib/lazy-pages-api-target.ts',
route: '/api/lazy-import-pages',
marker: 'pages-api-lazy-target-marker',
},
])(
'compiles $name imports when reached',
async ({ entryPath, targetPath, route, marker }) => {
await next.patchFile(
targetPath,
(content) => `${content}\nexport const invalid = ;`,
async () => {
expect(await next.fetch(route).then((res) => res.json())).toEqual({
value: 'idle',
})
expect(await assetsContaining(marker, 'server')).toHaveLength(0)
}
)
// Rebuild the owning entry after restoration without activating the import.
await next.patchFile(
entryPath,
(content) => content.replace("'idle'", "'ready'"),
async () => {
await retry(async () => {
expect(await next.fetch(route).then((res) => res.json())).toEqual(
{
value: 'ready',
}
)
})
expect(await assetsContaining(marker, 'server')).toHaveLength(0)
const responses = await Promise.all([
next.fetch(`${route}?load=1`).then((res) => res.json()),
next.fetch(`${route}?load=1`).then((res) => res.json()),
])
expect(responses).toEqual([{ value: marker }, { value: marker }])
}
)
await next.patchFile(
targetPath,
(content) => content.replace(marker, `updated-${marker}`),
async () => {
await retry(async () => {
expect(
await next.fetch(`${route}?load=1`).then((res) => res.json())
).toEqual({ value: `updated-${marker}` })
}, 10000)
}
)
}
)
it('compiles Pages SSR imports when reached', async () => {
const targetPath = path.join('lib', 'lazy-pages-ssr-target.ts')
await next.patchFile(
targetPath,
(content) => `${content}\nexport const invalid = ;`,
async () => {
expect(await next.render('/lazy-ssr')).toContain('idle')
expect(
await assetsContaining('pages-ssr-lazy-target-marker', 'server')
).toHaveLength(0)
}
)
await next.patchFile(
'pages/lazy-ssr.tsx',
(content) => content.replace("'idle'", "'ready'"),
async () => {
await retry(async () => {
const $ = await next.render$('/lazy-ssr')
expect($('#pages-ssr-lazy-value').text()).toBe('ready')
})
expect(
await assetsContaining('pages-ssr-lazy-target-marker', 'server')
).toHaveLength(0)
const renders = await Promise.all([
next.render('/lazy-ssr?load=1'),
next.render('/lazy-ssr?load=1'),
])
for (const html of renders) {
expect(html).toContain('pages-ssr-lazy-target-marker')
}
}
)
await next.patchFile(
targetPath,
(content) =>
content.replace(
'pages-ssr-lazy-target-marker',
'updated-pages-ssr-lazy-target-marker'
),
async () => {
await retry(async () => {
expect(await next.render('/lazy-ssr?load=1')).toContain(
'updated-pages-ssr-lazy-target-marker'
)
}, 10000)
}
)
})
it('keeps proxy imports eager', async () => {
expect(
await next.fetch('/lazy-proxy-probe').then((res) => res.json())
).toEqual({ value: 'idle' })
expect(
await assetsContaining('proxy-lazy-target-marker', 'server')
).not.toHaveLength(0)
expect(
await next.fetch('/lazy-proxy-probe?load=1').then((res) => res.json())
).toEqual({ value: 'proxy-lazy-target-marker' })
})
it('activates a dynamic import over HTTP and keeps Fast Refresh working', async () => { it('activates a dynamic import over HTTP and keeps Fast Refresh working', async () => {
const targetPath = path.join('app', 'target.tsx') const targetPath = path.join('app', 'target.tsx')
const originalTarget = await next.readFile(targetPath) const originalTarget = await next.readFile(targetPath)
...@@ -368,6 +507,59 @@ export const invalid = ;` ...@@ -368,6 +507,59 @@ export const invalid = ;`
).toContain('next-dynamic-css-marker') ).toContain('next-dynamic-css-marker')
}) })
it('preloads and applies CSS for a Pages next/dynamic component', async () => {
const $ = await next.render$('/pages-next-dynamic-css')
expect($('#pages-dynamic-target').text()).toBe(
'pages-next-dynamic-marker'
)
const stylesheetHrefs = $('link[rel="stylesheet"]')
.map((_, link) => $(link).attr('href'))
.get()
const stylesheets = await Promise.all(
stylesheetHrefs.map((href) =>
next.fetch(href).then((response) => response.text())
)
)
const targetStylesheet = stylesheets.findIndex((css) =>
css.includes('pages-dynamic-target')
)
expect(targetStylesheet).not.toBe(-1)
const preloadHrefs = $('link[rel="preload"][as="style"]')
.map((_, link) => $(link).attr('href'))
.get()
expect(preloadHrefs).toContain(stylesheetHrefs[targetStylesheet])
const scriptPreloadHrefs = $('link[rel="preload"][as="script"]')
.map((_, link) => $(link).attr('href'))
.get()
const preloadedScripts = await Promise.all(
scriptPreloadHrefs.map((href) =>
next.fetch(href).then((response) => response.text())
)
)
const targetScript = preloadedScripts.findIndex((script) =>
script.includes('pages-next-dynamic-marker')
)
expect(targetScript).not.toBe(-1)
const scriptSources = $('script[src]')
.map((_, script) => $(script).attr('src'))
.get()
expect(scriptSources).toContain(scriptPreloadHrefs[targetScript])
const browser = await next.browser('/pages-next-dynamic-css')
await retry(async () => {
expect(await browser.elementByCss('#pages-dynamic-target').text()).toBe(
'pages-next-dynamic-marker'
)
expect(
await browser
.elementByCss('#pages-dynamic-target')
.getComputedCss('color')
).toBe('rgb(23, 45, 67)')
})
})
it('activates a pattern import without colliding with its target', async () => { it('activates a pattern import without colliding with its target', async () => {
const browser = await next.browser('/pattern') const browser = await next.browser('/pattern')
const getActivationKeys = async (): Promise<string[]> => const getActivationKeys = async (): Promise<string[]> =>
......
export const value = 'instrumentation-startup-target-marker'
export const value = 'pages-api-lazy-target-marker'
export const value = 'pages-ssr-lazy-target-marker'
import styles from './pages-dynamic-target.module.css'
export default function Target() {
return (
<p id="pages-dynamic-target" className={styles.target}>
pages-next-dynamic-marker
</p>
)
}
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
*/ */
const nextConfig = { const nextConfig = {
experimental: { experimental: {
disableOptimizedLoading: true,
turbopackLazyDynamicImports: true, turbopackLazyDynamicImports: true,
turbopackLazyDynamicImportsSSR: true, turbopackLazyDynamicImportsSSR: true,
}, },
......
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (!req.query.load) {
res.json({ value: 'idle' })
return
}
const { value } = await import('../../lib/lazy-pages-api-target')
res.json({ value })
}
import type { GetServerSideProps } from 'next'
export const getServerSideProps: GetServerSideProps<{ value: string }> = async (
context
) => {
if (!context.query.load) return { props: { value: 'idle' } }
const { value } = await import('../lib/lazy-pages-ssr-target')
return { props: { value } }
}
export default function Page({ value }: { value: string }) {
return <p id="pages-ssr-lazy-value">{value}</p>
}
import dynamic from 'next/dynamic'
const DynamicTarget = dynamic(() => import('../lib/pages-dynamic-target'))
export default function Page() {
return <DynamicTarget />
}
import { NextResponse, type NextRequest } from 'next/server'
export async function proxy(request: NextRequest) {
if (!request.nextUrl.searchParams.has('load')) {
return NextResponse.json({ value: 'idle' })
}
const { value } = await import('./lib/lazy-proxy-target')
return NextResponse.json({ value })
}
export const config = { matcher: '/lazy-proxy-probe' }
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