Skip to content

Commit 120b0f9

Browse files
committed
Allow exporting object and array literals in 'use cache' files again
In #86014 we added a validation for exporting object and array literals in `'use cache'` files. This was a bit too restrictive, and short-sighted, as it would prevent exporting common things like metadata or viewport objects from page and layout files, if the file uses a `'use cache'` top-level directive.
1 parent a51a2e4 commit 120b0f9

File tree

9 files changed

+16
-39
lines changed

9 files changed

+16
-39
lines changed

crates/next-custom-transforms/src/transforms/server_actions.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,18 +1895,17 @@ impl<C: Comments> VisitMut for ServerActions<C> {
18951895
Decl::Var(var) => {
18961896
let mut has_export_needing_wrapper = false;
18971897

1898-
// Validate exports and check for cache runtime wrappers. Disallow
1899-
// exporting literals, objects, and arrays directly (destructuring
1900-
// patterns are allowed since we can't statically know if the
1901-
// destructured value is a function).
19021898
for decl in &var.decls {
19031899
if let Pat::Ident(_) = &decl.name {
19041900
if let Some(init) = &decl.init {
1905-
match &**init {
1906-
Expr::Lit(_) | Expr::Object(_) | Expr::Array(_) => {
1907-
disallowed_export_span = *span;
1908-
}
1909-
_ => {}
1901+
// Disallow exporting literals. Admittedly, this is
1902+
// pretty arbitrary. We don't disallow exporting object
1903+
// and array literals, as that would be too restrictive,
1904+
// especially for page and layout files with
1905+
// 'use cache', that may want to export metadata or
1906+
// viewport objects.
1907+
if let Expr::Lit(_) = &**init {
1908+
disallowed_export_span = *span;
19101909
}
19111910
}
19121911
}

crates/next-custom-transforms/tests/errors/server-actions/client-graph/3/output.stderr

Lines changed: 0 additions & 7 deletions
This file was deleted.

crates/next-custom-transforms/tests/errors/server-actions/server-graph/23/input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async function a() {
1616
}
1717
}
1818

19-
export const { foo } = {
19+
export const obj = {
2020
foo() {
2121
return 42
2222
},
Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* __next_internal_action_entry_do_not_use__ {"ffab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ import { registerServerReference } from "private-next-rsc-server-reference";
2-
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
3-
import { cache as $$reactCache__ } from "react";
41
// not exported!
52
async function a() {
63
// this is allowed here
@@ -14,7 +11,7 @@ async function a() {
1411
console.log(arguments);
1512
};
1613
}
17-
const { foo } = {
14+
export const obj = {
1815
foo () {
1916
return 42;
2017
},
@@ -25,14 +22,3 @@ const { foo } = {
2522
console.log(arguments);
2623
}
2724
};
28-
let $$RSC_SERVER_CACHE_foo = foo;
29-
if (typeof foo === "function") {
30-
$$RSC_SERVER_CACHE_foo = $$reactCache__(function() {
31-
return $$cache__("default", "ffab21efdafbe611287bc25c0462b1e0510d13e48b", 0, foo, arguments);
32-
});
33-
registerServerReference($$RSC_SERVER_CACHE_foo, "ffab21efdafbe611287bc25c0462b1e0510d13e48b", null);
34-
Object["defineProperty"]($$RSC_SERVER_CACHE_foo, "name", {
35-
value: "foo"
36-
});
37-
}
38-
export { $$RSC_SERVER_CACHE_foo as foo };

crates/next-custom-transforms/tests/errors/server-actions/server-graph/31/output.stderr

Lines changed: 0 additions & 7 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use cache'
22

3+
// Should not get cache runtime wrappers.
34
export const foo = {},
45
bar = [1]

crates/next-custom-transforms/tests/fixture/server-actions/client-graph/17/output.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use cache'
22

3+
// Should not get cache runtime wrappers.
34
export const foo = {},
45
bar = [1]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Should not get cache runtime wrappers.
2+
export const foo = {}, bar = [
3+
1
4+
];

0 commit comments

Comments
 (0)