Skip to content

Commit

Permalink
add support for assets in edge
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Mar 12, 2024
1 parent 11af8dd commit c5032a7
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 23 deletions.
13 changes: 8 additions & 5 deletions packages/next-swc/crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,18 +519,19 @@ impl AppEndpoint {
async fn output(self: Vc<Self>) -> Result<Vc<AppEndpointOutput>> {
let this = self.await?;

let (app_entry, process_client, process_ssr) = match this.ty {
let (app_entry, process_client, process_ssr, api_route) = match this.ty {
AppEndpointType::Page { ty, loader_tree } => (
self.app_page_entry(loader_tree),
true,
matches!(ty, AppPageEndpointType::Html),
false,
),
// NOTE(alexkirsz) For routes, technically, a lot of the following code is not needed,
// as we know we won't have any client references. However, for now, for simplicity's
// sake, we just do the same thing as for pages.
AppEndpointType::Route { path } => (self.app_route_entry(path), false, false),
AppEndpointType::Route { path } => (self.app_route_entry(path), false, false, true),
AppEndpointType::Metadata { metadata } => {
(self.app_metadata_entry(metadata), false, false)
(self.app_metadata_entry(metadata), false, false, true)
}
};

Expand Down Expand Up @@ -613,7 +614,9 @@ impl AppEndpoint {
NextRuntime::NodeJs => {
Vc::upcast(this.app_project.project().server_chunking_context())
}
NextRuntime::Edge => this.app_project.project().edge_chunking_context(),
NextRuntime::Edge => {
this.app_project.project().edge_chunking_context(api_route)
}
})
} else {
None
Expand Down Expand Up @@ -837,7 +840,7 @@ impl AppEndpoint {
let endpoint_output = match runtime {
NextRuntime::Edge => {
// create edge chunks
let chunking_context = this.app_project.project().edge_chunking_context();
let chunking_context = this.app_project.project().edge_chunking_context(api_route);
let mut evaluatable_assets = this
.app_project
.edge_rsc_runtime_entries()
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-api/src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl InstrumentationEndpoint {
};
evaluatable_assets.push(evaluatable);

let edge_chunking_context = self.project.edge_chunking_context();
let edge_chunking_context = self.project.edge_chunking_context(true);

let edge_files = edge_chunking_context.evaluated_chunk_group_assets(
module.ident(),
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl MiddlewareEndpoint {
.context("Entry module must be evaluatable")?;
evaluatable_assets.push(evaluatable);

let edge_chunking_context = self.project.edge_chunking_context();
let edge_chunking_context = self.project.edge_chunking_context(false);

let edge_files = edge_chunking_context.evaluated_chunk_group_assets(
module.ident(),
Expand Down
6 changes: 3 additions & 3 deletions packages/next-swc/crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ impl PageEndpoint {
this.pages_project.ssr_module_context(),
this.pages_project.edge_ssr_module_context(),
this.pages_project.project().server_chunking_context(),
this.pages_project.project().edge_chunking_context(),
this.pages_project.project().edge_chunking_context(false),
this.pages_project.ssr_runtime_entries(),
this.pages_project.edge_ssr_runtime_entries(),
))
Expand All @@ -815,7 +815,7 @@ impl PageEndpoint {
this.pages_project.ssr_data_module_context(),
this.pages_project.edge_ssr_data_module_context(),
this.pages_project.project().server_chunking_context(),
this.pages_project.project().edge_chunking_context(),
this.pages_project.project().edge_chunking_context(false),
this.pages_project.ssr_data_runtime_entries(),
this.pages_project.edge_ssr_data_runtime_entries(),
))
Expand All @@ -835,7 +835,7 @@ impl PageEndpoint {
this.pages_project.api_module_context(),
this.pages_project.edge_api_module_context(),
this.pages_project.project().server_chunking_context(),
this.pages_project.project().edge_chunking_context(),
this.pages_project.project().edge_chunking_context(true),
this.pages_project.ssr_runtime_entries(),
this.pages_project.edge_ssr_runtime_entries(),
))
Expand Down
7 changes: 6 additions & 1 deletion packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,13 +571,18 @@ impl Project {
#[turbo_tasks::function]
pub(super) fn edge_chunking_context(
self: Vc<Self>,
api_route: bool,
) -> Result<Vc<Box<dyn EcmascriptChunkingContext>>> {
Ok(get_edge_chunking_context(
self.next_mode(),
self.project_path(),
self.node_root(),
self.client_relative_path(),
self.next_config().computed_asset_prefix(),
if api_route {
Vc::cell(Some("blob:".to_string()))
} else {
self.next_config().computed_asset_prefix()
},
self.edge_compile_time_info().environment(),
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ pub async fn get_server_module_options_context(
next_server_rules.extend(source_transform_rules);

let module_options_context = ModuleOptionsContext {
esm_url_rewrite_behavior: Some(UrlRewriteBehavior::Full),
..module_options_context
};
let foreign_code_module_options_context = ModuleOptionsContext {
Expand Down
16 changes: 9 additions & 7 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ export default class NextNodeServer extends BaseServer {
name: string
paths: string[]
wasm: { filePath: string; name: string }[]
assets: { filePath: string; name: string }[]
assets?: { filePath: string; name: string }[]
} | null {
const manifest = this.getMiddlewareManifest()
if (!manifest) {
Expand Down Expand Up @@ -1455,12 +1455,14 @@ export default class NextNodeServer extends BaseServer {
...binding,
filePath: join(this.distDir, binding.filePath),
})),
assets: (pageInfo.assets ?? []).map((binding) => {
return {
...binding,
filePath: join(this.distDir, binding.filePath),
}
}),
assets:
pageInfo.assets &&
pageInfo.assets.map((binding) => {
return {
...binding,
filePath: join(this.distDir, binding.filePath),
}
}),
}
}

Expand Down
9 changes: 7 additions & 2 deletions packages/next/src/server/web/sandbox/fetch-inline-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ export async function fetchInlineAsset(options: {
return
}

const hash = inputString.replace('blob:', '')
const asset = options.assets?.find((x) => x.name === hash)
const name = inputString.replace('blob:', '')
const asset = options.assets
? options.assets.find((x) => x.name === name)
: {
name,
filePath: name,
}
if (!asset) {
return
}
Expand Down
5 changes: 2 additions & 3 deletions test/turbopack-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6491,12 +6491,11 @@
"passed": [
"og-api should respond from index",
"og-api should throw error when returning a response object in pages/api in node runtime",
"og-api should work in app route in node runtime"
],
"failed": [
"og-api should work in app route in node runtime",
"og-api should work in app route",
"og-api should work in pages/api"
],
"failed": [],
"pending": [],
"flakey": [],
"runtimeError": false
Expand Down

0 comments on commit c5032a7

Please sign in to comment.