Unverified Commit 2dd7f528 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[backport] Allow literal exports in `'use cache'` files (#97312)

Backports #97181 ("Allow literal exports in `'use cache'` files") to
`next-16-3`.

A `'use cache'` page or layout that exports a route segment config
literal, such as `export const instant = false`, `export const
dynamicParams = true`, `export const prefetch = 'partial'`, or `export
const maxDuration = 5`, fails the build with "Only async functions are
allowed to be exported in a "use cache" file".

Rejecting exported literals is meaningful only in a `'use server'` file,
where every export becomes a server reference and a runtime check
asserts that each one is a function. A `'use cache'` file wraps only
exports that are, or might be, functions, so known non-function values
can pass through. The check is now scoped to `in_action_file`.

### Verification on this branch

Cherry-picked from `c0c64a0a` with no conflicts. All source files are
byte-identical to canary. This touches `crates/next-custom-transforms`,
so the release build needs the native binary rebuilt. Behavioral
verification is left to CI on this branch; the change carries transform
fixtures on both the server and client graph.

<!-- NEXT_JS_LLM -->
parent c19a64dc
...@@ -1908,15 +1908,20 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1908,15 +1908,20 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let mut has_export_needing_wrapper = false; let mut has_export_needing_wrapper = false;
for decl in &var.decls { for decl in &var.decls {
if let Pat::Ident(_) = &decl.name if in_action_file
&& let Pat::Ident(_) = &decl.name
&& let Some(init) = &decl.init && let Some(init) = &decl.init
{ {
// Disallow exporting literals. Admittedly, this is // In a "use server" file every export becomes a server
// pretty arbitrary. We don't disallow exporting object // reference, and a runtime check asserts that each one is a
// and array literals, as that would be too restrictive, // function. Reject exported literals at build time instead.
// especially for page and layout files with // Object and array literals stay allowed, as rejecting them
// 'use cache', that may want to export metadata or // would be too restrictive.
// viewport objects. //
// A "use cache" file wraps only exports that are, or might
// be, functions. Known non-function values pass through.
// Page and layout files need this for route segment
// configs, metadata, and viewport.
if let Expr::Lit(_) = &**init { if let Expr::Lit(_) = &**init {
disallowed_export_span = *span; disallowed_export_span = *span;
} }
......
export default function() {}
export function foo() {}
export const bar = ()=>{};
export const baz = 42;
...@@ -22,10 +22,3 @@ ...@@ -22,10 +22,3 @@
: ^^^ : ^^^
6 | export const baz = 42 6 | export const baz = 42
`---- `----
x Only async functions are allowed to be exported in a "use cache" file.
|
,-[input.js:6:1]
5 | export const bar = () => {}
6 | export const baz = 42
: ^^^^^^^^^^^^^^^^^^^^^
`----
'use cache'
// Only the exports that got a reference ID in the server graph become server
// references here. The statically known non-function values are dropped.
export const instant = false
export const dynamicParams = true
export const prefetch = 'partial'
export const maxDuration = 5
export const metadata = { title: 'Hello' }
export default async function Page() {
return null
}
/* __next_internal_action_entry_do_not_use__ {"80c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":{"name":"default"}} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
export default /*#__PURE__*/ createServerReference("80c18c215a6b7cdc64bf709f3a714ffdef1bf9651d", callServer, void 0, findSourceMapURL, "default");
'use cache'
// Route segment configs, metadata, and viewport are statically known
// non-function values. They should be exported as-is, without cache runtime
// wrappers.
export const instant = false
export const dynamicParams = true
export const prefetch = 'partial'
export const maxDuration = 5
export const metadata = { title: 'Hello' }
export default async function Page() {
return null
}
/* __next_internal_action_entry_do_not_use__ {"803128060c414d59f8552e4788b846c0d2b7f74743":{"name":"$$RSC_SERVER_CACHE_0"}} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
import { cache as $$reactCache__ } from "react";
// Route segment configs, metadata, and viewport are statically known
// non-function values. They should be exported as-is, without cache runtime
// wrappers.
export const instant = false;
export const dynamicParams = true;
export const prefetch = 'partial';
export const maxDuration = 5;
export const metadata = {
title: 'Hello'
};
const $$RSC_SERVER_CACHE_0_INNER = async function Page() {
return null;
};
export var $$RSC_SERVER_CACHE_0 = $$reactCache__(function Page() {
return $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 0, $$RSC_SERVER_CACHE_0_INNER, []);
});
registerServerReference($$RSC_SERVER_CACHE_0, "803128060c414d59f8552e4788b846c0d2b7f74743", null);
Object["defineProperty"]($$RSC_SERVER_CACHE_0, "name", {
value: "Page"
});
export default $$RSC_SERVER_CACHE_0;
...@@ -23,6 +23,9 @@ export default async function Page() { ...@@ -23,6 +23,9 @@ export default async function Page() {
<li> <li>
<DebugLinks href="/shells/valid-static-with-gsp/123" /> <DebugLinks href="/shells/valid-static-with-gsp/123" />
</li> </li>
<li>
<DebugLinks href="/shells/valid-use-cache-instant-false" />
</li>
<li> <li>
<DebugLinks href="/shells/invalid-runtime-params/123" /> <DebugLinks href="/shells/invalid-runtime-params/123" />
</li> </li>
......
'use cache'
// A file-level `"use cache"` directive wraps every exported function in a cache
// boundary. `instant` is a statically known non-function value, so it stays a
// plain export and is read as route segment config.
export const instant = false
export default async function IgnoreStaticShellValidationLayout({ children }) {
return children
}
import { Instant } from 'next'
import { cookies } from 'next/headers'
export const instant: Instant = {
level: 'experimental-error',
unstable_samples: [{ cookies: [] }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
<main>
<p>
This page has an unguarded session data access, which is allowed in a
shell but leaves the static shell empty. The parent layout opts out of
static shell validation with <code>instant = false</code>, exported from
a file with a top-level <code>"use cache"</code> directive.
</p>
<SessionData />
</main>
)
}
async function SessionData() {
await cookies()
return <div>Session data (cookies)</div>
}
...@@ -605,6 +605,20 @@ export function registerHeadAndReportingTests( ...@@ -605,6 +605,20 @@ export function registerHeadAndReportingTests(
} }
}) })
it('valid - instant = false in a "use cache" layout opts out of static shell validation', async () => {
if (isNextDev) {
const browser = await navigateTo(
'/shells/valid-use-cache-instant-false'
)
await expectNoDevValidationErrors(browser, await browser.url())
} else {
const result = await prerender(
'/shells/(default)/valid-use-cache-instant-false'
)
expectNoBuildValidationErrors(result)
}
})
it('invalid - unguarded params in a runtime-prefetchable shell', async () => { it('invalid - unguarded params in a runtime-prefetchable shell', async () => {
if (isNextDev) { if (isNextDev) {
const browser = await navigateTo('/shells/invalid-runtime-params/123') const browser = await navigateTo('/shells/invalid-runtime-params/123')
......
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