Skip to content

Commit

Permalink
Add client_root for edge (#7081)
Browse files Browse the repository at this point in the history
### Description

Ensures edge compilation outputs the right asset urls. Currently in
Next.js with Turbopack enabled they show `/assets/file.hash.png` but it
should be relative to the `asset_prefix` and client path.

<!--
  ✍️ Write a short summary of your work.
  If necessary, include relevant screenshots.
-->

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
  • Loading branch information
timneutkens and sokra authored Jan 23, 2024
1 parent bdc40cb commit 31816d1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/turbopack-cli/src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ async fn source(
let build_chunking_context = DevChunkingContext::builder(
project_path,
build_output_root,
build_output_root,
build_output_root.join("chunks".to_string()),
build_output_root.join("assets".to_string()),
node_build_environment(),
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-cli/src/dev/web_entry_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn get_client_chunking_context(
DevChunkingContext::builder(
project_path,
server_root,
server_root,
server_root.join("/_chunks".to_string()),
server_root.join("/_assets".to_string()),
environment,
Expand Down
10 changes: 7 additions & 3 deletions crates/turbopack-dev/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ pub struct DevChunkingContext {
/// This path get stripped off of chunk paths before generating output asset
/// paths.
context_path: Vc<FileSystemPath>,
/// This path is used to compute the url to request chunks or assets from
/// This path is used to compute the url to request chunks from
output_root: Vc<FileSystemPath>,
/// This path is used to compute the url to request assets from
client_root: Vc<FileSystemPath>,
/// Chunks are placed at this path
chunk_root_path: Vc<FileSystemPath>,
/// Chunks reference source maps assets
Expand All @@ -105,6 +107,7 @@ impl DevChunkingContext {
pub fn builder(
context_path: Vc<FileSystemPath>,
output_root: Vc<FileSystemPath>,
client_root: Vc<FileSystemPath>,
chunk_root_path: Vc<FileSystemPath>,
asset_root_path: Vc<FileSystemPath>,
environment: Vc<Environment>,
Expand All @@ -113,6 +116,7 @@ impl DevChunkingContext {
chunking_context: DevChunkingContext {
context_path,
output_root,
client_root,
chunk_root_path,
reference_chunk_source_maps: true,
reference_css_chunk_source_maps: true,
Expand Down Expand Up @@ -235,8 +239,8 @@ impl ChunkingContext for DevChunkingContext {
let this = self.await?;
let asset_path = ident.path().await?.to_string();
let asset_path = asset_path
.strip_prefix(&format!("{}/", this.output_root.await?.path))
.context("expected output_root to contain asset path")?;
.strip_prefix(&format!("{}/", this.client_root.await?.path))
.context("expected asset_path to contain client_root")?;

Ok(Vc::cell(format!(
"{}{}",
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-tests/tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ async fn run_test(prepared_test: Vc<PreparedTest>) -> Result<Vc<RunTestResult>>
let chunking_context = DevChunkingContext::builder(
project_root,
chunk_root_path,
static_root_path,
chunk_root_path,
static_root_path,
env,
Expand Down
13 changes: 10 additions & 3 deletions crates/turbopack-tests/tests/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,16 @@ async fn run_test(resource: String) -> Result<Vc<FileSystemPath>> {

let chunking_context: Vc<Box<dyn ChunkingContext>> = match options.runtime {
Runtime::Dev => Vc::upcast(
DevChunkingContext::builder(project_root, path, chunk_root_path, static_root_path, env)
.runtime_type(options.runtime_type)
.build(),
DevChunkingContext::builder(
project_root,
path,
path,
chunk_root_path,
static_root_path,
env,
)
.runtime_type(options.runtime_type)
.build(),
),
Runtime::Build => Vc::upcast(
BuildChunkingContext::builder(
Expand Down

1 comment on commit 31816d1

@vercel
Copy link

@vercel vercel bot commented on 31816d1 Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.