Unverified Commit ba20a5e8 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Fix false-positive build error for `cacheLife` & `cacheTag` (#85875)

When functions exported from a module with a top-level `'use cache'`
directive are imported into client modules, the compiler should allow
`cacheLife` and `cacheTag` usage within those functions.

This PR updates the `react_server_components` transform to properly
track module directives (`'use client'`, `'use server'`, `'use cache'`)
and skip client graph validation for modules with `'use cache'`,
identical to how modules with `'use server'` are already handled. Those
modules are transformed anyway for the client graph by the
`server_actions` transform, which is applied after the
`react_server_components` transform, in which their implementation is
replaced by server function references.

closes NAR-498
parent 5bd7c6fe
......@@ -158,6 +158,8 @@ fn react_server_actions_errors(input: PathBuf) {
syntax(),
&|tr| {
(
// The transforms are intentionally declared in the same order as in
// crates/next-custom-transforms/src/chain_transforms.rs
resolver(Mark::new(), Mark::new(), false),
server_components(
FileName::Real(PathBuf::from("/app/item.js")).into(),
......@@ -222,6 +224,8 @@ fn use_cache_not_allowed(input: PathBuf) {
syntax(),
&|tr| {
(
// The transforms are intentionally declared in the same order as in
// crates/next-custom-transforms/src/chain_transforms.rs
resolver(Mark::new(), Mark::new(), false),
server_components(
FileName::Real(PathBuf::from("/app/item.js")).into(),
......
x It's not possible to have both `use client` and `use server` directives in the same file.
x It's not possible to have both "use client" and "use server" directives in the same file.
,-[input.js:2:1]
1 | 'use server'
2 | 'use client'
......
x It's not possible to have both `use client` and `use server` directives in the same file.
x It's not possible to have both "use client" and "use server" directives in the same file.
,-[input.js:6:1]
5 |
6 | 'use server'
......
/* __next_internal_client_entry_do_not_use__ foo auto */ const { createProxy } = require("private-next-rsc-mod-ref-proxy");
module.exports = createProxy("/app/item.js");
x It's not possible to have both "use client" and "use cache" directives in the same file.
,-[input.js:2:1]
1 | 'use cache'
2 | 'use client'
: ^^^^^^^^^^^^
`----
/* __next_internal_client_entry_do_not_use__ foo auto */ const { createProxy } = require("private-next-rsc-mod-ref-proxy");
module.exports = createProxy("/app/item.js");
x It's not possible to have both "use client" and "use cache" directives in the same file.
,-[input.js:2:1]
1 | 'use client'
2 | 'use cache'
: ^^^^^^^^^^^
`----
......@@ -471,19 +471,39 @@ fn react_server_components_typescript(input: PathBuf) {
fn react_server_components_fixture(input: PathBuf) {
use next_custom_transforms::transforms::react_server_components::{Config, Options};
let is_react_server_layer = input.iter().any(|s| s.to_str() == Some("server-graph"));
let filename = FileName::Real(PathBuf::from("/some-project/src/some-file.js"));
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|tr| {
server_components(
FileName::Real(PathBuf::from("/some-project/src/some-file.js")).into(),
Config::WithOptions(Options {
is_react_server_layer,
cache_components_enabled: false,
use_cache_enabled: false,
}),
tr.comments.as_ref().clone(),
None,
(
// The transforms are intentionally declared in the same order as in
// crates/next-custom-transforms/src/chain_transforms.rs
server_components(
filename.clone().into(),
Config::WithOptions(Options {
is_react_server_layer,
cache_components_enabled: false,
use_cache_enabled: false,
}),
tr.comments.as_ref().clone(),
None,
),
server_actions(
&filename,
None,
server_actions::Config {
is_react_server_layer,
is_development: true,
use_cache_enabled: true,
hash_salt: "".into(),
cache_kinds: FxHashSet::default(),
},
tr.comments.as_ref().clone(),
tr.cm.clone(),
Default::default(),
ServerActionsMode::Webpack,
),
)
},
&input,
......
......@@ -3,3 +3,7 @@
// It should be allowed to import server APIs here.
import 'server-only'
import { cookies } from 'next/headers'
export async function test() {
await cookies()
}
'use server';
// It should be allowed to import server APIs here.
import 'server-only';
import { cookies } from 'next/headers';
/* __next_internal_action_entry_do_not_use__ {"00a275d5b08d0553a04df65d28075d3cad57073dca":"test"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
export var test = /*#__PURE__*/ createServerReference("00a275d5b08d0553a04df65d28075d3cad57073dca", callServer, void 0, findSourceMapURL, "test");
'use cache'
import { cacheLife } from 'next/cache'
export async function test() {
cacheLife('days')
return null
}
/* __next_internal_action_entry_do_not_use__ {"80a275d5b08d0553a04df65d28075d3cad57073dca":"test"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
export var test = /*#__PURE__*/ createServerReference("80a275d5b08d0553a04df65d28075d3cad57073dca", callServer, void 0, findSourceMapURL, "test");
'use cache'
import { cacheTag } from 'next/cache'
export async function test() {
cacheTag('test')
return null
}
/* __next_internal_action_entry_do_not_use__ {"80a275d5b08d0553a04df65d28075d3cad57073dca":"test"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
export var test = /*#__PURE__*/ createServerReference("80a275d5b08d0553a04df65d28075d3cad57073dca", callServer, void 0, findSourceMapURL, "test");
'use cache'
import { cacheLife, cacheTag } from 'next/cache'
function getRandomValue() {
const v = Math.random()
console.log(v)
......@@ -7,10 +9,12 @@ function getRandomValue() {
}
export async function foo() {
cacheLife('days')
return getRandomValue()
}
export const bar = async function () {
cacheTag('bar')
return getRandomValue()
}
......
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