Unverified Commit 46761a32 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Throw when `unstable_instant` is used in Client Component (#90396)

The `unstable_instant` route segment config is not allowed in Client
Components because the API mainly depends on prefetches, which Client
Components aren't part of.

Closes NAR-774

---------
Co-authored-by: 's avatarvercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
parent 14ac25b9
...@@ -118,6 +118,9 @@ pub struct NextSegmentConfig { ...@@ -118,6 +118,9 @@ pub struct NextSegmentConfig {
#[turbo_tasks(trace_ignore)] #[turbo_tasks(trace_ignore)]
#[bincode(with_serde)] #[bincode(with_serde)]
pub generate_static_params: Option<Span>, pub generate_static_params: Option<Span>,
#[turbo_tasks(trace_ignore)]
#[bincode(with_serde)]
pub unstable_instant: Option<Span>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -505,36 +508,53 @@ pub async fn parse_segment_config_from_source( ...@@ -505,36 +508,53 @@ pub async fn parse_segment_config_from_source(
) )
.await?; .await?;
if mode == ParseSegmentMode::App let is_client_entry = module_ast
&& let Some(span) = config.generate_static_params .body
&& module_ast .iter()
.body .take_while(|i| match i {
.iter() ModuleItem::Stmt(stmt) => stmt.directive_continue(),
.take_while(|i| match i { ModuleItem::ModuleDecl(_) => false,
ModuleItem::Stmt(stmt) => stmt.directive_continue(), })
ModuleItem::ModuleDecl(_) => false, .filter_map(|i| i.as_stmt())
}) .any(|f| match f {
.filter_map(|i| i.as_stmt()) Stmt::Expr(ExprStmt { expr, .. }) => match &**expr {
.any(|f| match f { Expr::Lit(Lit::Str(Str { value, .. })) => value == "use client",
Stmt::Expr(ExprStmt { expr, .. }) => match &**expr {
Expr::Lit(Lit::Str(Str { value, .. })) => value == "use client",
_ => false,
},
_ => false, _ => false,
}) },
{ _ => false,
invalid_config( });
source,
"generateStaticParams", if mode == ParseSegmentMode::App && is_client_entry {
span, if let Some(span) = config.generate_static_params {
rcstr!( invalid_config(
"App pages cannot use both \"use client\" and export function \ source,
\"generateStaticParams()\"." "generateStaticParams",
), span,
None, rcstr!(
IssueSeverity::Error, "App pages cannot use both \"use client\" and export function \
) \"generateStaticParams()\"."
.await?; ),
None,
IssueSeverity::Error,
)
.await?;
}
if let Some(span) = config.unstable_instant {
invalid_config(
source,
"unstable_instant",
span,
rcstr!(
"App pages cannot export \"unstable_instant\" from a Client Component module. \
To use this API, convert this module to a Server Component by removing the \
\"use client\" directive."
),
None,
IssueSeverity::Error,
)
.await?;
}
} }
Ok(config.cell()) Ok(config.cell())
...@@ -952,6 +972,9 @@ async fn parse_config_value( ...@@ -952,6 +972,9 @@ async fn parse_config_value(
"generateStaticParams" => { "generateStaticParams" => {
config.generate_static_params = Some(span); config.generate_static_params = Some(span);
} }
"unstable_instant" => {
config.unstable_instant = Some(span);
}
_ => {} _ => {}
} }
......
...@@ -1071,5 +1071,7 @@ ...@@ -1071,5 +1071,7 @@
"1070": "image of size %s could not be tracked by lru cache", "1070": "image of size %s could not be tracked by lru cache",
"1071": "toNodeDebugChannel cannot be used in edge/web runtime, this is a bug in the Next.js codebase", "1071": "toNodeDebugChannel cannot be used in edge/web runtime, this is a bug in the Next.js codebase",
"1072": "Missing segment data: <head>", "1072": "Missing segment data: <head>",
"1073": "Expected static stage body to be available" "1073": "Expected static stage body to be available",
"1074": "Page \"%s\" cannot use both \"use client\" and export const \"unstable_instant\".",
"1075": "Page \"%s\" cannot export \"unstable_instant\" from a Client Component module. To use this API, convert this module to a Server Component by removing the \"use client\" directive."
} }
...@@ -684,6 +684,13 @@ export async function getAppPageStaticInfo({ ...@@ -684,6 +684,13 @@ export async function getAppPageStaticInfo({
) )
} }
// Prevent use client and unstable_instant in the same file.
if (directives?.has('client') && 'unstable_instant' in config) {
throw new Error(
`Page "${page}" cannot export "unstable_instant" from a Client Component module. To use this API, convert this module to a Server Component by removing the "use client" directive.`
)
}
if ('unstable_instant' in config && !nextConfig.cacheComponents) { if ('unstable_instant' in config && !nextConfig.cacheComponents) {
throw new Error( throw new Error(
`Page "${page}" cannot use \`export const unstable_instant = ...\` without enabling \`cacheComponents\`.` `Page "${page}" cannot use \`export const unstable_instant = ...\` without enabling \`cacheComponents\`.`
......
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
'use client'
export default function Page() {
return <p>hello world</p>
}
export const unstable_instant = {
prefetch: 'static',
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('app dir - instant-validation-client', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
skipStart: true,
})
if (skipped) {
return
}
it('should error when a client component exports unstable_instant', async () => {
const expectedErrMsg = process.env.IS_TURBOPACK_TEST
? `Next.js can't recognize the exported \`unstable_instant\` field in route. App pages cannot export "unstable_instant" from a Client Component module. To use this API, convert this module to a Server Component by removing the "use client" directive.`
: `Page "/page" cannot export "unstable_instant" from a Client Component module. To use this API, convert this module to a Server Component by removing the "use client" directive.`
if (isNextDev) {
await next.start().catch(() => {})
await next.browser('/').catch(() => {})
await retry(async () => {
expect(next.cliOutput).toContain(expectedErrMsg)
})
} else {
const { cliOutput } = await next.build()
expect(cliOutput).toContain(expectedErrMsg)
}
})
})
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
}
export default nextConfig
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