Skip to content

Commit

Permalink
Implement client_root for edge in Turbopack (#61024)
Browse files Browse the repository at this point in the history
## What?

Implements vercel/turborepo#7081. Ensures image
imports in the edge runtime have the correct asset url. Previously it
would be `/assets/file.hash.png` but it should be
`/_next/static/media/file.hash.png`.

Updates the image tests to correctly compare the values in Turbopack and
the values in Webpack. Currently it only checks webpack values, causing
the tests to fail in Turbopack.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2194
  • Loading branch information
timneutkens authored Jan 23, 2024
1 parent f82445b commit 59e042a
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 152 deletions.
68 changes: 34 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ swc_core = { version = "0.87.16", features = [
testing = { version = "0.35.14" }

# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240123.1" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240123.3" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240123.1" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240123.3" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240123.1" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240123.3" }

# General Deps

Expand Down
6 changes: 3 additions & 3 deletions packages/next-swc/crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ pub(crate) async fn get_paths_from_root(
.map({
move |&file| async move {
let path = &*file.ident().path().await?;
let relative = root
.get_path_to(path)
.context("file path must be inside the root")?;
let Some(relative) = root.get_path_to(path) else {
return Ok(None);
};

Ok(if filter(relative) {
Some(relative.to_string())
Expand Down
3 changes: 3 additions & 0 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ impl Project {
DevChunkingContext::builder(
self.project_path(),
node_root,
node_root,
node_root.join("chunks".to_string()),
node_root.join("assets".to_string()),
node_build_environment(),
Expand Down Expand Up @@ -529,6 +530,8 @@ impl Project {
get_edge_chunking_context(
self.project_path(),
self.node_root(),
self.client_relative_path(),
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 @@ -338,6 +338,7 @@ pub async fn get_client_chunking_context(
let mut builder = DevChunkingContext::builder(
project_path,
client_root,
client_root,
client_root.join("static/chunks".to_string()),
get_client_assets_path(client_root),
environment,
Expand Down
6 changes: 5 additions & 1 deletion packages/next-swc/crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,21 @@ pub async fn get_edge_resolve_options_context(
pub fn get_edge_chunking_context(
project_path: Vc<FileSystemPath>,
node_root: Vc<FileSystemPath>,
client_root: Vc<FileSystemPath>,
asset_prefix: Vc<Option<String>>,
environment: Vc<Environment>,
) -> Vc<Box<dyn EcmascriptChunkingContext>> {
let output_root = node_root.join("server/edge".to_string());
Vc::upcast(
DevChunkingContext::builder(
project_path,
output_root,
client_root,
output_root.join("chunks".to_string()),
output_root.join("assets".to_string()),
client_root.join("static/media".to_string()),
environment,
)
.asset_base_path(asset_prefix)
.reference_chunk_source_maps(should_debug("edge"))
.build(),
)
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
"@types/ws": "8.2.0",
"@vercel/ncc": "0.34.0",
"@vercel/nft": "0.26.2",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240123.1",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240123.3",
"acorn": "8.5.0",
"amphtml-validator": "1.0.35",
"anser": "1.4.9",
Expand Down
Loading

0 comments on commit 59e042a

Please sign in to comment.