Unverified Commit 11dfdf8d authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

Turbopack: Strip relative path prefix when generating PageLoaderAsset (#54040)

### What?

Strip the relative path prefix (`_next/`) when generating the `PageLoaderAsset`. This is necessary because the page loader will internally use `__turbopack_load__`, which prepends the prefix back onto the path.

### Why?

Without this, we'd try loading `_next/_next/path/to/file.js`, because we'd prepend the prefix onto a path that already contains it.

### How?

When generating the page loader asset, we "proxy" the `OutputAsset` onto a path which has the prefix removed.

~~Depends on https://github.com/vercel/turbo/pull/5717~~

Closes WEB-1377
parent 8def0c84
This diff is collapsed.
...@@ -46,11 +46,11 @@ swc_core = { version = "0.79.59" } ...@@ -46,11 +46,11 @@ swc_core = { version = "0.79.59" }
testing = { version = "0.33.21" } testing = { version = "0.33.21" }
# Turbo crates # Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230814.2" } turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230816.1" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros.. # [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-230814.2" } turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230816.1" }
# [TODO]: need to refactor embed_directory! macro usage in next-core # [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230814.2" } turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230816.1" }
# General Deps # General Deps
...@@ -123,7 +123,9 @@ serde = { version = "1.0.152", features = ["derive"] } ...@@ -123,7 +123,9 @@ serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93" serde_json = "1.0.93"
serde_qs = "0.11.0" serde_qs = "0.11.0"
serde_yaml = "0.9.17" serde_yaml = "0.9.17"
shadow-rs = { version = "0.23.0", default-features = false, features = ["tzdb"] } shadow-rs = { version = "0.23.0", default-features = false, features = [
"tzdb",
] }
syn = "1.0.107" syn = "1.0.107"
tempfile = "3.3.0" tempfile = "3.3.0"
thiserror = "1.0.38" thiserror = "1.0.38"
......
...@@ -24,7 +24,7 @@ use next_core::{ ...@@ -24,7 +24,7 @@ use next_core::{
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, Completion, TaskInput, TryJoinIterExt, Value, Vc}; use turbo_tasks::{trace::TraceRawVcs, Completion, TaskInput, TryJoinIterExt, Value, Vc};
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_fs::{File, FileSystem, FileSystemPath, VirtualFileSystem}, turbo::tasks_fs::{File, FileSystem, FileSystemPath, FileSystemPathOption, VirtualFileSystem},
turbopack::{ turbopack::{
build::BuildChunkingContext, build::BuildChunkingContext,
core::{ core::{
...@@ -567,6 +567,7 @@ impl PageEndpoint { ...@@ -567,6 +567,7 @@ impl PageEndpoint {
Vc::upcast(client_chunking_context), Vc::upcast(client_chunking_context),
self.source(), self.source(),
this.pathname, this.pathname,
self.client_relative_path(),
))); )));
Ok(Vc::cell(client_chunks)) Ok(Vc::cell(client_chunks))
...@@ -811,6 +812,11 @@ impl PageEndpoint { ...@@ -811,6 +812,11 @@ impl PageEndpoint {
Ok(page_output.cell()) Ok(page_output.cell())
} }
#[turbo_tasks::function]
fn client_relative_path(&self) -> Vc<FileSystemPathOption> {
Vc::cell(Some(self.pages_project.project().client_relative_path()))
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
"check": "tsc --noEmit" "check": "tsc --noEmit"
}, },
"dependencies": { "dependencies": {
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230814.2", "@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230816.1",
"@vercel/turbopack-node": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230814.2", "@vercel/turbopack-node": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230816.1",
"anser": "^2.1.1", "anser": "^2.1.1",
"css.escape": "^1.5.1", "css.escape": "^1.5.1",
"next": "*", "next": "*",
......
...@@ -3,6 +3,7 @@ use std::io::Write; ...@@ -3,6 +3,7 @@ use std::io::Write;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use indexmap::indexmap; use indexmap::indexmap;
use turbo_tasks::{TryJoinIterExt, Value, Vc}; use turbo_tasks::{TryJoinIterExt, Value, Vc};
use turbo_tasks_fs::FileSystemPathOption;
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_fs::{rope::RopeBuilder, File, FileContent, FileSystemPath}, turbo::tasks_fs::{rope::RopeBuilder, File, FileContent, FileSystemPath},
turbopack::{ turbopack::{
...@@ -16,6 +17,7 @@ use turbopack_binding::{ ...@@ -16,6 +17,7 @@ use turbopack_binding::{
ident::AssetIdent, ident::AssetIdent,
module::Module, module::Module,
output::{OutputAsset, OutputAssets}, output::{OutputAsset, OutputAssets},
proxied_asset::ProxiedAsset,
reference_type::{EntryReferenceSubType, ReferenceType}, reference_type::{EntryReferenceSubType, ReferenceType},
source::Source, source::Source,
virtual_source::VirtualSource, virtual_source::VirtualSource,
...@@ -34,6 +36,7 @@ pub async fn create_page_loader( ...@@ -34,6 +36,7 @@ pub async fn create_page_loader(
client_chunking_context: Vc<Box<dyn ChunkingContext>>, client_chunking_context: Vc<Box<dyn ChunkingContext>>,
entry_asset: Vc<Box<dyn Source>>, entry_asset: Vc<Box<dyn Source>>,
pathname: Vc<String>, pathname: Vc<String>,
rebase_prefix_path: Vc<FileSystemPathOption>,
) -> Result<Vc<Box<dyn ContentSource>>> { ) -> Result<Vc<Box<dyn ContentSource>>> {
let asset = PageLoaderAsset { let asset = PageLoaderAsset {
server_root, server_root,
...@@ -41,6 +44,7 @@ pub async fn create_page_loader( ...@@ -41,6 +44,7 @@ pub async fn create_page_loader(
client_chunking_context, client_chunking_context,
entry_asset, entry_asset,
pathname, pathname,
rebase_prefix_path,
} }
.cell(); .cell();
...@@ -96,6 +100,7 @@ pub struct PageLoaderAsset { ...@@ -96,6 +100,7 @@ pub struct PageLoaderAsset {
pub client_chunking_context: Vc<Box<dyn ChunkingContext>>, pub client_chunking_context: Vc<Box<dyn ChunkingContext>>,
pub entry_asset: Vc<Box<dyn Source>>, pub entry_asset: Vc<Box<dyn Source>>,
pub pathname: Vc<String>, pub pathname: Vc<String>,
pub rebase_prefix_path: Vc<FileSystemPathOption>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -107,6 +112,7 @@ impl PageLoaderAsset { ...@@ -107,6 +112,7 @@ impl PageLoaderAsset {
client_chunking_context: Vc<Box<dyn ChunkingContext>>, client_chunking_context: Vc<Box<dyn ChunkingContext>>,
entry_asset: Vc<Box<dyn Source>>, entry_asset: Vc<Box<dyn Source>>,
pathname: Vc<String>, pathname: Vc<String>,
rebase_prefix_path: Vc<FileSystemPathOption>,
) -> Vc<Self> { ) -> Vc<Self> {
Self { Self {
server_root, server_root,
...@@ -114,6 +120,7 @@ impl PageLoaderAsset { ...@@ -114,6 +120,7 @@ impl PageLoaderAsset {
client_chunking_context, client_chunking_context,
entry_asset, entry_asset,
pathname, pathname,
rebase_prefix_path,
} }
.cell() .cell()
} }
...@@ -138,12 +145,31 @@ impl PageLoaderAsset { ...@@ -138,12 +145,31 @@ impl PageLoaderAsset {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn chunks_data(self: Vc<Self>) -> Result<Vc<ChunksData>> { async fn chunks_data(
self: Vc<Self>,
rebase_prefix_path: Vc<FileSystemPathOption>,
) -> Result<Vc<ChunksData>> {
let this = self.await?; let this = self.await?;
Ok(ChunkData::from_assets( let mut chunks = self.get_page_chunks();
this.server_root,
self.get_page_chunks(), // If we are provided a prefix path, we need to rewrite our chunk paths to
)) // remove that prefix.
if let Some(rebase_path) = &*rebase_prefix_path.await? {
let root_path = rebase_path.root();
let rebased = chunks
.await?
.iter()
.map(|chunk| {
Vc::upcast(ProxiedAsset::new(
*chunk,
FileSystemPath::rebase(chunk.ident().path(), *rebase_path, root_path),
))
})
.collect();
chunks = Vc::cell(rebased);
};
Ok(ChunkData::from_assets(this.server_root, chunks))
} }
} }
...@@ -171,7 +197,9 @@ impl OutputAsset for PageLoaderAsset { ...@@ -171,7 +197,9 @@ impl OutputAsset for PageLoaderAsset {
references.push(chunk); references.push(chunk);
} }
for chunk_data in &*self.chunks_data().await? { // We don't need to strip the client relative prefix, because we won't be using
// these reference paths with `__turbopack_load__`.
for chunk_data in &*self.chunks_data(FileSystemPathOption::none()).await? {
references.extend(chunk_data.references().await?.iter().copied()); references.extend(chunk_data.references().await?.iter().copied());
} }
...@@ -185,7 +213,11 @@ impl Asset for PageLoaderAsset { ...@@ -185,7 +213,11 @@ impl Asset for PageLoaderAsset {
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> { async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = &*self.await?; let this = &*self.await?;
let chunks_data = self.chunks_data().await?; // The asset emits `__turbopack_load_page_chunks__`, which is implemented using
// `__turbopack_load__`. `__turbopack_load__` will prefix the path with the
// relative client path (`_next/`), so we need to remove that when we emit the
// chunk paths.
let chunks_data = self.chunks_data(this.rebase_prefix_path).await?;
let chunks_data = chunks_data.iter().try_join().await?; let chunks_data = chunks_data.iter().try_join().await?;
let chunks_data: Vec<_> = chunks_data let chunks_data: Vec<_> = chunks_data
.iter() .iter()
......
...@@ -3,6 +3,7 @@ use indexmap::{indexmap, IndexMap}; ...@@ -3,6 +3,7 @@ use indexmap::{indexmap, IndexMap};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
use turbo_tasks::{trace::TraceRawVcs, Value, Vc}; use turbo_tasks::{trace::TraceRawVcs, Value, Vc};
use turbo_tasks_fs::FileSystemPathOption;
use turbopack_binding::{ use turbopack_binding::{
turbo::{ turbo::{
tasks_env::{CustomProcessEnv, EnvMap, ProcessEnv}, tasks_env::{CustomProcessEnv, EnvMap, ProcessEnv},
...@@ -482,6 +483,7 @@ async fn create_page_source_for_file( ...@@ -482,6 +483,7 @@ async fn create_page_source_for_file(
Vc::upcast(client_chunking_context), Vc::upcast(client_chunking_context),
page_asset, page_asset,
pathname, pathname,
FileSystemPathOption::none(),
), ),
])) ]))
}) })
...@@ -566,6 +568,7 @@ async fn create_not_found_page_source( ...@@ -566,6 +568,7 @@ async fn create_not_found_page_source(
client_chunking_context, client_chunking_context,
page_asset, page_asset,
pathname, pathname,
FileSystemPathOption::none(),
); );
Ok(Vc::upcast(CombinedContentSource::new(vec![ Ok(Vc::upcast(CombinedContentSource::new(vec![
......
...@@ -1431,11 +1431,11 @@ importers: ...@@ -1431,11 +1431,11 @@ importers:
packages/next-swc/crates/next-core/js: packages/next-swc/crates/next-core/js:
dependencies: dependencies:
'@vercel/turbopack-ecmascript-runtime': '@vercel/turbopack-ecmascript-runtime':
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230814.2 specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230816.1
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230814.2(react-refresh@0.12.0)(webpack@5.86.0)' version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230816.1(react-refresh@0.12.0)(webpack@5.86.0)'
'@vercel/turbopack-node': '@vercel/turbopack-node':
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230814.2 specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230816.1
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230814.2' version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230816.1'
anser: anser:
specifier: ^2.1.1 specifier: ^2.1.1
version: 2.1.1 version: 2.1.1
...@@ -26406,9 +26406,9 @@ packages: ...@@ -26406,9 +26406,9 @@ packages:
/zwitch@2.0.4: /zwitch@2.0.4:
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==} resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230814.2(react-refresh@0.12.0)(webpack@5.86.0)': '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230816.1(react-refresh@0.12.0)(webpack@5.86.0)':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230814.2} resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230816.1}
id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230814.2' id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230816.1'
name: '@vercel/turbopack-ecmascript-runtime' name: '@vercel/turbopack-ecmascript-runtime'
version: 0.0.0 version: 0.0.0
dependencies: dependencies:
...@@ -26419,8 +26419,8 @@ packages: ...@@ -26419,8 +26419,8 @@ packages:
- webpack - webpack
dev: false dev: false
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230814.2': '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230816.1':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230814.2} resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230816.1}
name: '@vercel/turbopack-node' name: '@vercel/turbopack-node'
version: 0.0.0 version: 0.0.0
dependencies: dependencies:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment