Skip to content

Commit c5032a7

Browse files
committed
add support for assets in edge
1 parent 11af8dd commit c5032a7

File tree

9 files changed

+38
-23
lines changed

9 files changed

+38
-23
lines changed

packages/next-swc/crates/next-api/src/app.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,19 @@ impl AppEndpoint {
519519
async fn output(self: Vc<Self>) -> Result<Vc<AppEndpointOutput>> {
520520
let this = self.await?;
521521

522-
let (app_entry, process_client, process_ssr) = match this.ty {
522+
let (app_entry, process_client, process_ssr, api_route) = match this.ty {
523523
AppEndpointType::Page { ty, loader_tree } => (
524524
self.app_page_entry(loader_tree),
525525
true,
526526
matches!(ty, AppPageEndpointType::Html),
527+
false,
527528
),
528529
// NOTE(alexkirsz) For routes, technically, a lot of the following code is not needed,
529530
// as we know we won't have any client references. However, for now, for simplicity's
530531
// sake, we just do the same thing as for pages.
531-
AppEndpointType::Route { path } => (self.app_route_entry(path), false, false),
532+
AppEndpointType::Route { path } => (self.app_route_entry(path), false, false, true),
532533
AppEndpointType::Metadata { metadata } => {
533-
(self.app_metadata_entry(metadata), false, false)
534+
(self.app_metadata_entry(metadata), false, false, true)
534535
}
535536
};
536537

@@ -613,7 +614,9 @@ impl AppEndpoint {
613614
NextRuntime::NodeJs => {
614615
Vc::upcast(this.app_project.project().server_chunking_context())
615616
}
616-
NextRuntime::Edge => this.app_project.project().edge_chunking_context(),
617+
NextRuntime::Edge => {
618+
this.app_project.project().edge_chunking_context(api_route)
619+
}
617620
})
618621
} else {
619622
None
@@ -837,7 +840,7 @@ impl AppEndpoint {
837840
let endpoint_output = match runtime {
838841
NextRuntime::Edge => {
839842
// create edge chunks
840-
let chunking_context = this.app_project.project().edge_chunking_context();
843+
let chunking_context = this.app_project.project().edge_chunking_context(api_route);
841844
let mut evaluatable_assets = this
842845
.app_project
843846
.edge_rsc_runtime_entries()

packages/next-swc/crates/next-api/src/instrumentation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl InstrumentationEndpoint {
9595
};
9696
evaluatable_assets.push(evaluatable);
9797

98-
let edge_chunking_context = self.project.edge_chunking_context();
98+
let edge_chunking_context = self.project.edge_chunking_context(true);
9999

100100
let edge_files = edge_chunking_context.evaluated_chunk_group_assets(
101101
module.ident(),

packages/next-swc/crates/next-api/src/middleware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl MiddlewareEndpoint {
9797
.context("Entry module must be evaluatable")?;
9898
evaluatable_assets.push(evaluatable);
9999

100-
let edge_chunking_context = self.project.edge_chunking_context();
100+
let edge_chunking_context = self.project.edge_chunking_context(false);
101101

102102
let edge_files = edge_chunking_context.evaluated_chunk_group_assets(
103103
module.ident(),

packages/next-swc/crates/next-api/src/pages.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ impl PageEndpoint {
795795
this.pages_project.ssr_module_context(),
796796
this.pages_project.edge_ssr_module_context(),
797797
this.pages_project.project().server_chunking_context(),
798-
this.pages_project.project().edge_chunking_context(),
798+
this.pages_project.project().edge_chunking_context(false),
799799
this.pages_project.ssr_runtime_entries(),
800800
this.pages_project.edge_ssr_runtime_entries(),
801801
))
@@ -815,7 +815,7 @@ impl PageEndpoint {
815815
this.pages_project.ssr_data_module_context(),
816816
this.pages_project.edge_ssr_data_module_context(),
817817
this.pages_project.project().server_chunking_context(),
818-
this.pages_project.project().edge_chunking_context(),
818+
this.pages_project.project().edge_chunking_context(false),
819819
this.pages_project.ssr_data_runtime_entries(),
820820
this.pages_project.edge_ssr_data_runtime_entries(),
821821
))
@@ -835,7 +835,7 @@ impl PageEndpoint {
835835
this.pages_project.api_module_context(),
836836
this.pages_project.edge_api_module_context(),
837837
this.pages_project.project().server_chunking_context(),
838-
this.pages_project.project().edge_chunking_context(),
838+
this.pages_project.project().edge_chunking_context(true),
839839
this.pages_project.ssr_runtime_entries(),
840840
this.pages_project.edge_ssr_runtime_entries(),
841841
))

packages/next-swc/crates/next-api/src/project.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,13 +571,18 @@ impl Project {
571571
#[turbo_tasks::function]
572572
pub(super) fn edge_chunking_context(
573573
self: Vc<Self>,
574+
api_route: bool,
574575
) -> Result<Vc<Box<dyn EcmascriptChunkingContext>>> {
575576
Ok(get_edge_chunking_context(
576577
self.next_mode(),
577578
self.project_path(),
578579
self.node_root(),
579580
self.client_relative_path(),
580-
self.next_config().computed_asset_prefix(),
581+
if api_route {
582+
Vc::cell(Some("blob:".to_string()))
583+
} else {
584+
self.next_config().computed_asset_prefix()
585+
},
581586
self.edge_compile_time_info().environment(),
582587
))
583588
}

packages/next-swc/crates/next-core/src/next_server/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ pub async fn get_server_module_options_context(
616616
next_server_rules.extend(source_transform_rules);
617617

618618
let module_options_context = ModuleOptionsContext {
619+
esm_url_rewrite_behavior: Some(UrlRewriteBehavior::Full),
619620
..module_options_context
620621
};
621622
let foreign_code_module_options_context = ModuleOptionsContext {

packages/next/src/server/next-server.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ export default class NextNodeServer extends BaseServer {
14221422
name: string
14231423
paths: string[]
14241424
wasm: { filePath: string; name: string }[]
1425-
assets: { filePath: string; name: string }[]
1425+
assets?: { filePath: string; name: string }[]
14261426
} | null {
14271427
const manifest = this.getMiddlewareManifest()
14281428
if (!manifest) {
@@ -1455,12 +1455,14 @@ export default class NextNodeServer extends BaseServer {
14551455
...binding,
14561456
filePath: join(this.distDir, binding.filePath),
14571457
})),
1458-
assets: (pageInfo.assets ?? []).map((binding) => {
1459-
return {
1460-
...binding,
1461-
filePath: join(this.distDir, binding.filePath),
1462-
}
1463-
}),
1458+
assets:
1459+
pageInfo.assets &&
1460+
pageInfo.assets.map((binding) => {
1461+
return {
1462+
...binding,
1463+
filePath: join(this.distDir, binding.filePath),
1464+
}
1465+
}),
14641466
}
14651467
}
14661468

packages/next/src/server/web/sandbox/fetch-inline-assets.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ export async function fetchInlineAsset(options: {
1919
return
2020
}
2121

22-
const hash = inputString.replace('blob:', '')
23-
const asset = options.assets?.find((x) => x.name === hash)
22+
const name = inputString.replace('blob:', '')
23+
const asset = options.assets
24+
? options.assets.find((x) => x.name === name)
25+
: {
26+
name,
27+
filePath: name,
28+
}
2429
if (!asset) {
2530
return
2631
}

test/turbopack-tests-manifest.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6491,12 +6491,11 @@
64916491
"passed": [
64926492
"og-api should respond from index",
64936493
"og-api should throw error when returning a response object in pages/api in node runtime",
6494-
"og-api should work in app route in node runtime"
6495-
],
6496-
"failed": [
6494+
"og-api should work in app route in node runtime",
64976495
"og-api should work in app route",
64986496
"og-api should work in pages/api"
64996497
],
6498+
"failed": [],
65006499
"pending": [],
65016500
"flakey": [],
65026501
"runtimeError": false

0 commit comments

Comments
 (0)