-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DX improvements for
"use cache"
functions (#71518)
With this PR, we're assigning names to the transformed `"use cache"` functions, using the same rules as in #71478. Since we can't use named function expressions here, we're using `Object.defineProperty` instead, which we already used in #71478 for server actions with default export expressions. In addition, this PR fixes source mapping of `"use cache"` functions, which was slightly off before, due to the wrong spans being assigned to the new expressions. **Before:** <img width="943" alt="before" src="https://github.com/user-attachments/assets/f7cf674c-32d4-4f0d-a304-d5de5637a6a1"> **After:** <img width="943" alt="after" src="https://github.com/user-attachments/assets/8edda482-a666-4431-bfad-7e21987331eb">
- Loading branch information
1 parent
dfc5331
commit f2ed75a
Showing
17 changed files
with
222 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 21 additions & 5 deletions
26
crates/next-custom-transforms/tests/fixture/server-actions/server/34/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,42 @@ | ||
/* __next_internal_action_entry_do_not_use__ {"12a8d21b6362b4cc8f5b15560525095bc48dba80":"$$RSC_SERVER_CACHE_3","3128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0","4acc55633206134002149ce873fe498be64a6fef":"$$RSC_SERVER_CACHE_4","951c375b4a6a6e89d67b743ec5808127cfde405d":"$$RSC_SERVER_CACHE_1"} */ import { registerServerReference } from "private-next-rsc-server-reference"; | ||
/* __next_internal_action_entry_do_not_use__ {"12a8d21b6362b4cc8f5b15560525095bc48dba80":"$$RSC_SERVER_CACHE_3","3128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0","69348c79fce073bae2f70f139565a2fda1c74c74":"$$RSC_SERVER_CACHE_2","951c375b4a6a6e89d67b743ec5808127cfde405d":"$$RSC_SERVER_CACHE_1"} */ import { registerServerReference } from "private-next-rsc-server-reference"; | ||
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; | ||
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper"; | ||
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "3128060c414d59f8552e4788b846c0d2b7f74743", async function() { | ||
return 'foo'; | ||
}); | ||
Object.defineProperty($$RSC_SERVER_CACHE_0, "name", { | ||
"value": "foo", | ||
"writable": false | ||
}); | ||
const foo = registerServerReference($$RSC_SERVER_CACHE_0, "3128060c414d59f8552e4788b846c0d2b7f74743", null); | ||
export { bar }; | ||
export var $$RSC_SERVER_CACHE_1 = $$cache__("default", "951c375b4a6a6e89d67b743ec5808127cfde405d", async function bar() { | ||
return 'bar'; | ||
}); | ||
Object.defineProperty($$RSC_SERVER_CACHE_1, "name", { | ||
"value": "bar", | ||
"writable": false | ||
}); | ||
var bar = registerServerReference($$RSC_SERVER_CACHE_1, "951c375b4a6a6e89d67b743ec5808127cfde405d", null); | ||
// Should not be wrapped in $$cache__. | ||
const qux = async function qux() { | ||
return 'qux'; | ||
}; | ||
export var $$RSC_SERVER_CACHE_3 = $$cache__("default", "12a8d21b6362b4cc8f5b15560525095bc48dba80", async function $$RSC_SERVER_CACHE_2() { | ||
export var $$RSC_SERVER_CACHE_2 = $$cache__("default", "69348c79fce073bae2f70f139565a2fda1c74c74", async function baz() { | ||
return qux() + 'baz'; | ||
}); | ||
const baz = registerServerReference($$RSC_SERVER_CACHE_3, "12a8d21b6362b4cc8f5b15560525095bc48dba80", null); | ||
export var $$RSC_SERVER_CACHE_4 = $$cache__("default", "4acc55633206134002149ce873fe498be64a6fef", async function() { | ||
Object.defineProperty($$RSC_SERVER_CACHE_2, "name", { | ||
"value": "baz", | ||
"writable": false | ||
}); | ||
const baz = registerServerReference($$RSC_SERVER_CACHE_2, "69348c79fce073bae2f70f139565a2fda1c74c74", null); | ||
export var $$RSC_SERVER_CACHE_3 = $$cache__("default", "12a8d21b6362b4cc8f5b15560525095bc48dba80", async function() { | ||
return 'quux'; | ||
}); | ||
const quux = registerServerReference($$RSC_SERVER_CACHE_4, "4acc55633206134002149ce873fe498be64a6fef", null); | ||
Object.defineProperty($$RSC_SERVER_CACHE_3, "name", { | ||
"value": "quux", | ||
"writable": false | ||
}); | ||
const quux = registerServerReference($$RSC_SERVER_CACHE_3, "12a8d21b6362b4cc8f5b15560525095bc48dba80", null); | ||
export { foo, baz }; | ||
export default quux; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 19 additions & 3 deletions
22
crates/next-custom-transforms/tests/fixture/server-actions/server/36/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,35 @@ | ||
/* __next_internal_action_entry_do_not_use__ {"3128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0","4acc55633206134002149ce873fe498be64a6fef":"$$RSC_SERVER_CACHE_4","69348c79fce073bae2f70f139565a2fda1c74c74":"$$RSC_SERVER_CACHE_2","951c375b4a6a6e89d67b743ec5808127cfde405d":"$$RSC_SERVER_CACHE_1"} */ import { registerServerReference } from "private-next-rsc-server-reference"; | ||
/* __next_internal_action_entry_do_not_use__ {"12a8d21b6362b4cc8f5b15560525095bc48dba80":"$$RSC_SERVER_CACHE_3","3128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0","69348c79fce073bae2f70f139565a2fda1c74c74":"$$RSC_SERVER_CACHE_2","951c375b4a6a6e89d67b743ec5808127cfde405d":"$$RSC_SERVER_CACHE_1"} */ import { registerServerReference } from "private-next-rsc-server-reference"; | ||
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; | ||
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper"; | ||
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "3128060c414d59f8552e4788b846c0d2b7f74743", async function foo() { | ||
return 'data A'; | ||
}); | ||
Object.defineProperty($$RSC_SERVER_CACHE_0, "name", { | ||
"value": "foo", | ||
"writable": false | ||
}); | ||
export var foo = registerServerReference($$RSC_SERVER_CACHE_0, "3128060c414d59f8552e4788b846c0d2b7f74743", null); | ||
export var $$RSC_SERVER_CACHE_1 = $$cache__("default", "951c375b4a6a6e89d67b743ec5808127cfde405d", async function bar() { | ||
return 'data B'; | ||
}); | ||
Object.defineProperty($$RSC_SERVER_CACHE_1, "name", { | ||
"value": "bar", | ||
"writable": false | ||
}); | ||
export var bar = registerServerReference($$RSC_SERVER_CACHE_1, "951c375b4a6a6e89d67b743ec5808127cfde405d", null); | ||
export var $$RSC_SERVER_CACHE_2 = $$cache__("default", "69348c79fce073bae2f70f139565a2fda1c74c74", async function Cached({ children }) { | ||
return children; | ||
}); | ||
Object.defineProperty($$RSC_SERVER_CACHE_2, "name", { | ||
"value": "Cached", | ||
"writable": false | ||
}); | ||
export default registerServerReference($$RSC_SERVER_CACHE_2, "69348c79fce073bae2f70f139565a2fda1c74c74", null); | ||
export var $$RSC_SERVER_CACHE_4 = $$cache__("default", "4acc55633206134002149ce873fe498be64a6fef", async function $$RSC_SERVER_CACHE_3() { | ||
export var $$RSC_SERVER_CACHE_3 = $$cache__("default", "12a8d21b6362b4cc8f5b15560525095bc48dba80", async function baz() { | ||
return 'data C'; | ||
}); | ||
export const baz = registerServerReference($$RSC_SERVER_CACHE_4, "4acc55633206134002149ce873fe498be64a6fef", null); | ||
Object.defineProperty($$RSC_SERVER_CACHE_3, "name", { | ||
"value": "baz", | ||
"writable": false | ||
}); | ||
export const baz = registerServerReference($$RSC_SERVER_CACHE_3, "12a8d21b6362b4cc8f5b15560525095bc48dba80", null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use client' | ||
|
||
import { useActionState } from 'react' | ||
|
||
export function Form({ | ||
foo, | ||
bar, | ||
baz, | ||
}: { | ||
foo: () => Promise<number> | ||
bar: () => Promise<number> | ||
baz: () => Promise<number> | ||
}) { | ||
const [result, dispatch] = useActionState< | ||
[number, number, number], | ||
'submit' | 'reset' | ||
>( | ||
async (_state, event) => { | ||
if (event === 'reset') { | ||
return [0, 0, 0] | ||
} | ||
|
||
return [await foo(), await bar(), await baz()] | ||
}, | ||
[0, 0, 0] | ||
) | ||
|
||
return ( | ||
<form action={() => dispatch('submit')}> | ||
<button id="submit-button">Submit</button>{' '} | ||
<button id="reset-button" formAction={() => dispatch('reset')}> | ||
Reset | ||
</button> | ||
<p> | ||
{result[0]} {result[1]} {result[2]} | ||
</p> | ||
</form> | ||
) | ||
} |
Oops, something went wrong.