Skip to content

Commit c4738f7

Browse files
authored
Turbopack: don't execute action loader eagerly (#83049)
The server-reference-manifest.json contains the chunk item id of the action loader. The loader itself doesn't "register" itself in any way. So no need to execute it eagerly.
1 parent 69af581 commit c4738f7

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

crates/next-api/src/app.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ impl AppEndpoint {
17481748
assets,
17491749
availability_info,
17501750
} = *chunking_context
1751-
.evaluated_chunk_group(
1751+
.chunk_group(
17521752
server_action_manifest_loader.ident(),
17531753
ChunkGroup::Entry(
17541754
[ResolvedVc::upcast(server_action_manifest_loader)]
@@ -1785,7 +1785,6 @@ impl AppEndpoint {
17851785
bail!("rsc_entry must be evaluatable");
17861786
};
17871787

1788-
evaluatable_assets.push(server_action_manifest_loader);
17891788
evaluatable_assets.push(rsc_entry);
17901789

17911790
async {
@@ -1864,6 +1863,25 @@ impl AppEndpoint {
18641863
.await?;
18651864
}
18661865

1866+
{
1867+
let chunk_group = chunking_context
1868+
.chunk_group(
1869+
server_action_manifest_loader.ident(),
1870+
ChunkGroup::Entry(vec![ResolvedVc::upcast(
1871+
server_action_manifest_loader,
1872+
)]),
1873+
module_graph,
1874+
current_availability_info,
1875+
)
1876+
.await?;
1877+
1878+
current_chunks = current_chunks
1879+
.concatenate(*chunk_group.assets)
1880+
.resolve()
1881+
.await?;
1882+
current_availability_info = chunk_group.availability_info;
1883+
}
1884+
18671885
anyhow::Ok(Vc::cell(vec![
18681886
chunking_context
18691887
.entry_chunk_group_asset(

crates/next-api/src/server_actions.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ use swc_core::{
1919
},
2020
};
2121
use turbo_rcstr::{RcStr, rcstr};
22-
use turbo_tasks::{FxIndexMap, ResolvedVc, TryFlatJoinIterExt, ValueToString, Vc};
22+
use turbo_tasks::{FxIndexMap, ResolvedVc, TryFlatJoinIterExt, Vc};
2323
use turbo_tasks_fs::{self, File, FileSystemPath, rope::RopeBuilder};
2424
use turbopack_core::{
2525
asset::AssetContent,
26-
chunk::{ChunkItem, ChunkItemExt, ChunkableModule, ChunkingContext, EvaluatableAsset},
26+
chunk::{
27+
ChunkItem, ChunkItemExt, ChunkableModule, ChunkingContext, EvaluatableAsset, ModuleId,
28+
},
2729
context::AssetContext,
2830
file_source::FileSource,
2931
ident::AssetIdent,
@@ -168,7 +170,11 @@ async fn build_manifest(
168170
let key = format!("app{page_name}");
169171

170172
let actions_value = actions.await?;
171-
let loader_id = chunk_item.id().to_string().await?;
173+
let loader_id = chunk_item.id().await?;
174+
let loader_id = match &*loader_id {
175+
ModuleId::Number(id) => ActionManifestModuleId::Number(*id),
176+
ModuleId::String(id) => ActionManifestModuleId::String(id),
177+
};
172178
let mapping = match runtime {
173179
NextRuntime::Edge => &mut manifest.edge,
174180
NextRuntime::NodeJs => &mut manifest.node,
@@ -190,7 +196,7 @@ async fn build_manifest(
190196
entry.workers.insert(
191197
&key,
192198
ActionManifestWorkerEntry {
193-
module_id: ActionManifestModuleId::String(loader_id.as_str()),
199+
module_id: loader_id.clone(),
194200
is_async: *async_module_info.is_async(chunk_item.module()).await?,
195201
exported_name: name.as_str(),
196202
filename: filename.as_str(),

crates/next-core/src/next_manifests/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ pub struct ActionManifestWorkerEntry<'a> {
329329
pub filename: &'a str,
330330
}
331331

332-
#[derive(Serialize, Debug)]
332+
#[derive(Serialize, Debug, Clone)]
333333
#[serde(untagged)]
334334
pub enum ActionManifestModuleId<'a> {
335335
String(&'a str),
336-
Number(f64),
336+
Number(u64),
337337
}
338338

339339
#[derive(

test/e2e/app-dir/actions/app-action.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('app-dir action handling', () => {
4444

4545
expect(itemInfo.filename).toBeString()
4646
// can be outside app dir but this test suite has them all in app
47-
expect(itemInfo.filename.startsWith('app/')).toBe(true)
47+
expect(itemInfo.filename).toStartWith('app/')
4848
expect(itemInfo.exportedName).toBeString()
4949
} catch (err) {
5050
require('console').error(`Invalid action entry ${item}`, err)

test/e2e/app-dir/cache-components-errors/cache-components-errors.test.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,8 +2697,7 @@ describe('Cache Components Errors', () => {
26972697
| ^",
26982698
"stack": [
26992699
"{module evaluation} app/use-cache-private-in-unstable-cache/page.tsx (21:38)",
2700-
"{module evaluation} .next-internal/server/app/use-cache-private-in-unstable-cache/page/actions.js (server actions loader) (1:1)",
2701-
"<FIXME-file-protocol>",
2700+
"<FIXME-next-dist-dir>",
27022701
"<FIXME-next-dist-dir>",
27032702
],
27042703
}
@@ -2754,8 +2753,8 @@ describe('Cache Components Errors', () => {
27542753
expect(output).toMatchInlineSnapshot(`
27552754
"Error: "use cache: private" must not be used within \`unstable_cache()\`.
27562755
at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-unstable-cache/page.tsx:21:38)
2757-
at __TURBOPACK__module__evaluation__ (bundler:///.next-internal/server/app/use-cache-private-in-unstable-cache/page/actions.js (server actions loader):1:1)
27582756
at a (<next-dist-dir>)
2757+
at b (<next-dist-dir>)
27592758
19 | }
27602759
20 |
27612760
> 21 | const getCachedData = unstable_cache(async () => {
@@ -2773,7 +2772,6 @@ describe('Cache Components Errors', () => {
27732772
expect(output).toMatchInlineSnapshot(`
27742773
"Error: "use cache: private" must not be used within \`unstable_cache()\`.
27752774
at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-unstable-cache/page.tsx:21:38)
2776-
at __TURBOPACK__module__evaluation__ (bundler:///.next-internal/server/app/use-cache-private-in-unstable-cache/page/actions.js%20(server%20actions%20loader):1:1)
27772775
at a (<next-dist-dir>)
27782776
19 | }
27792777
20 |
@@ -2842,8 +2840,7 @@ describe('Cache Components Errors', () => {
28422840
| ^",
28432841
"stack": [
28442842
"{module evaluation} app/use-cache-private-in-use-cache/page.tsx (15:1)",
2845-
"{module evaluation} .next-internal/server/app/use-cache-private-in-use-cache/page/actions.js (server actions loader) (1:1)",
2846-
"<FIXME-file-protocol>",
2843+
"<FIXME-next-dist-dir>",
28472844
"<FIXME-next-dist-dir>",
28482845
],
28492846
}
@@ -2900,8 +2897,8 @@ describe('Cache Components Errors', () => {
29002897
expect(output).toMatchInlineSnapshot(`
29012898
"Error: "use cache: private" must not be used within "use cache". It can only be nested inside of another "use cache: private".
29022899
at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-use-cache/page.tsx:15:1)
2903-
at __TURBOPACK__module__evaluation__ (bundler:///.next-internal/server/app/use-cache-private-in-use-cache/page/actions.js (server actions loader):1:1)
29042900
at a (<next-dist-dir>)
2901+
at b (<next-dist-dir>)
29052902
13 | }
29062903
14 |
29072904
> 15 | async function Private() {
@@ -2911,8 +2908,8 @@ describe('Cache Components Errors', () => {
29112908
18 | return <p>Private</p>
29122909
Error: "use cache: private" must not be used within "use cache". It can only be nested inside of another "use cache: private".
29132910
at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-use-cache/page.tsx:15:1)
2914-
at __TURBOPACK__module__evaluation__ (bundler:///.next-internal/server/app/use-cache-private-in-use-cache/page/actions.js (server actions loader):1:1)
2915-
at b (<next-dist-dir>)
2911+
at c (<next-dist-dir>)
2912+
at d (<next-dist-dir>)
29162913
13 | }
29172914
14 |
29182915
> 15 | async function Private() {
@@ -2930,7 +2927,6 @@ describe('Cache Components Errors', () => {
29302927
expect(output).toMatchInlineSnapshot(`
29312928
"Error: "use cache: private" must not be used within "use cache". It can only be nested inside of another "use cache: private".
29322929
at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-use-cache/page.tsx:15:1)
2933-
at __TURBOPACK__module__evaluation__ (bundler:///.next-internal/server/app/use-cache-private-in-use-cache/page/actions.js%20(server%20actions%20loader):1:1)
29342930
at a (<next-dist-dir>)
29352931
13 | }
29362932
14 |
@@ -2941,7 +2937,6 @@ describe('Cache Components Errors', () => {
29412937
18 | return <p>Private</p>
29422938
Error: "use cache: private" must not be used within "use cache". It can only be nested inside of another "use cache: private".
29432939
at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-use-cache/page.tsx:15:1)
2944-
at __TURBOPACK__module__evaluation__ (bundler:///.next-internal/server/app/use-cache-private-in-use-cache/page/actions.js%20(server%20actions%20loader):1:1)
29452940
at b (<next-dist-dir>)
29462941
13 | }
29472942
14 |

0 commit comments

Comments
 (0)