Unverified Commit 6f87054c authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

fix(next-core): properly normalize app route for _ (#62307)

### What

This PR amends turbopack's behavior to handle some cases of normalized
path (`%5F` starting path), mainly fixes entry lookup works against
original path without normalization, also bends the output path of
client reference manifest matchs to what next.js expects.

Closes PACK-2553
parent 2f9d7186
......@@ -381,7 +381,7 @@ async fn get_directory_tree_internal(
// appDir ignores paths starting with an underscore
if !basename.starts_with('_') {
let result = get_directory_tree(dir, page_extensions);
subdirectories.insert(get_underscore_normalized_path(basename), result);
subdirectories.insert(basename.to_string(), result);
}
}
// TODO(WEB-952) handle symlinks in app dir
......@@ -1145,11 +1145,6 @@ async fn directory_tree_to_entrypoints_internal_untraced(
Ok(Vc::cell(result))
}
/// If path contains %5F, replace it with _. [reference](https://github.com/vercel/next.js/blob/c390c1662bc79e12cf7c037dcb382ef5ead6e492/packages/next/src/build/entries.ts#L119)
fn get_underscore_normalized_path(path: &str) -> String {
path.replace("%5F", "_")
}
/// Returns the global metadata for an app directory.
#[turbo_tasks::function]
pub async fn get_global_metadata(
......
......@@ -208,9 +208,15 @@ impl ClientReferenceManifest {
let client_reference_manifest_json = serde_json::to_string(&entry_manifest).unwrap();
// We put normalized path for the each entry key and the manifest output path,
// to conform next.js's load client reference manifest behavior:
// https://github.com/vercel/next.js/blob/2f9d718695e4c90be13c3bf0f3647643533071bf/packages/next/src/server/load-components.ts#L162-L164
// note this only applies to the manifests, assets are placed to the original
// path still (same as webpack does)
let normalized_manifest_entry = entry_name.replace("%5F", "_");
Ok(Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!(
"server/app/{entry_name}_client-reference-manifest.js",
"server/app{normalized_manifest_entry}_client-reference-manifest.js",
)),
AssetContent::file(
File::from(formatdoc! {
......@@ -218,7 +224,7 @@ impl ClientReferenceManifest {
globalThis.__RSC_MANIFEST = globalThis.__RSC_MANIFEST || {{}};
globalThis.__RSC_MANIFEST[{entry_name}] = {manifest}
"#,
entry_name = StringifyJs(&entry_name),
entry_name = StringifyJs(&normalized_manifest_entry),
manifest = &client_reference_manifest_json
})
.into(),
......
......@@ -2220,11 +2220,10 @@
"test/e2e/app-dir/_allow-underscored-root-directory/_allow-underscored-root-directory.test.ts": {
"passed": [
"_allow-underscored-root-directory should not serve app path with underscore",
"_allow-underscored-root-directory should pages path with a underscore at the root"
],
"failed": [
"_allow-underscored-root-directory should pages path with a underscore at the root",
"_allow-underscored-root-directory should serve app path with %5F"
],
"failed": [],
"pending": [],
"flakey": [],
"runtimeError": false
......@@ -4668,9 +4667,10 @@
"test/e2e/app-dir/underscore-ignore-app-paths/underscore-ignore-app-paths.test.ts": {
"passed": [
"underscore-ignore-app-paths should not serve app path with underscore",
"underscore-ignore-app-paths should serve pages path with underscore"
"underscore-ignore-app-paths should serve pages path with underscore",
"underscore-ignore-app-paths should serve app path with %5F"
],
"failed": ["underscore-ignore-app-paths should serve app path with %5F"],
"failed": [],
"pending": [],
"flakey": [],
"runtimeError": false
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