Unverified Commit cc775795 authored by Leah's avatar Leah Committed by GitHub

fix(turbopack): edge api entry path should not have `/route` suffix (#54851)

### Description

Lookup is done by the page name, which does not include the `route` suffix

Closes WEB-1481
parent e1cc97f3
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use next_core::{ use next_core::{
all_server_paths, all_server_paths,
middleware::{get_middleware_module, wrap_edge_entry}, middleware::get_middleware_module,
mode::NextMode, mode::NextMode,
next_edge::entry::wrap_edge_entry,
next_manifests::{EdgeFunctionDefinition, MiddlewareMatcher, MiddlewaresManifestV2}, next_manifests::{EdgeFunctionDefinition, MiddlewareMatcher, MiddlewaresManifestV2},
next_server::{get_server_runtime_entries, ServerContextType}, next_server::{get_server_runtime_entries, ServerContextType},
util::parse_config_from_source, util::parse_config_from_source,
......
use anyhow::Result; use anyhow::Result;
use indexmap::indexmap; use indexmap::indexmap;
use indoc::writedoc;
use turbo_tasks::{Value, Vc}; use turbo_tasks::{Value, Vc};
use turbo_tasks_fs::{rope::RopeBuilder, File, FileSystemPath}; use turbo_tasks_fs::{File, FileSystemPath};
use turbopack_binding::turbopack::{ use turbopack_binding::turbopack::core::{
core::{ asset::AssetContent, context::AssetContext, module::Module, reference_type::ReferenceType,
asset::AssetContent, context::AssetContext, module::Module, reference_type::ReferenceType, virtual_source::VirtualSource,
virtual_source::VirtualSource,
},
ecmascript::utils::StringifyJs,
}; };
use crate::util::{load_next_js_template, virtual_next_js_template_path}; use crate::util::{load_next_js_template, virtual_next_js_template_path};
...@@ -55,38 +51,3 @@ pub async fn get_middleware_module( ...@@ -55,38 +51,3 @@ pub async fn get_middleware_module(
Ok(module) Ok(module)
} }
#[turbo_tasks::function]
pub async fn wrap_edge_entry(
context: Vc<Box<dyn AssetContext>>,
project_root: Vc<FileSystemPath>,
entry: Vc<Box<dyn Module>>,
original_name: String,
) -> Result<Vc<Box<dyn Module>>> {
use std::io::Write;
let mut source = RopeBuilder::default();
writedoc!(
source,
r#"
import * as module from "MODULE"
self._ENTRIES ||= {{}}
self._ENTRIES[{}] = module
"#,
StringifyJs(&format_args!("middleware_{}", original_name))
)?;
let file = File::from(source.build());
// TODO(alexkirsz) Figure out how to name this virtual asset.
let virtual_source = VirtualSource::new(
project_root.join("edge-wrapper.js".to_string()),
AssetContent::file(file.into()),
);
let inner_assets = indexmap! {
"MODULE".to_string() => entry
};
Ok(context.process(
Vc::upcast(virtual_source),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
))
}
...@@ -120,7 +120,7 @@ pub async fn get_app_route_entry( ...@@ -120,7 +120,7 @@ pub async fn get_app_route_entry(
); );
if is_edge { if is_edge {
rsc_entry = wrap_edge_entry(context, project_root, rsc_entry, original_page_name.clone()); rsc_entry = wrap_edge_entry(context, project_root, rsc_entry, pathname.clone());
} }
let Some(rsc_entry) = let Some(rsc_entry) =
......
use anyhow::Result;
use indexmap::indexmap;
use indoc::formatdoc;
use turbo_tasks::{Value, Vc};
use turbo_tasks_fs::{File, FileSystemPath};
use turbopack_binding::turbopack::{
core::{
asset::AssetContent, context::AssetContext, module::Module, reference_type::ReferenceType,
virtual_source::VirtualSource,
},
ecmascript::utils::StringifyJs,
};
#[turbo_tasks::function]
pub async fn wrap_edge_entry(
context: Vc<Box<dyn AssetContext>>,
project_root: Vc<FileSystemPath>,
entry: Vc<Box<dyn Module>>,
pathname: String,
) -> Result<Vc<Box<dyn Module>>> {
let source = formatdoc!(
r#"
import * as module from "MODULE"
self._ENTRIES ||= {{}}
self._ENTRIES[{}] = module
"#,
StringifyJs(&format_args!("middleware_{}", pathname))
);
let file = File::from(source);
// TODO(alexkirsz) Figure out how to name this virtual asset.
let virtual_source = VirtualSource::new(
project_root.join("edge-wrapper.js".to_string()),
AssetContent::file(file.into()),
);
let inner_assets = indexmap! {
"MODULE".to_string() => entry
};
Ok(context.process(
Vc::upcast(virtual_source),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
))
}
pub mod context; pub mod context;
pub mod entry;
pub mod page_transition; pub mod page_transition;
pub mod route_regex; pub mod route_regex;
pub mod route_transition; pub mod route_transition;
...@@ -2,7 +2,6 @@ use std::io::Write; ...@@ -2,7 +2,6 @@ use std::io::Write;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use indexmap::indexmap; use indexmap::indexmap;
use indoc::writedoc;
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbo_tasks_fs::{rope::Rope, FileSystemPath}; use turbo_tasks_fs::{rope::Rope, FileSystemPath};
use turbopack_binding::{ use turbopack_binding::{
...@@ -14,7 +13,6 @@ use turbopack_binding::{ ...@@ -14,7 +13,6 @@ use turbopack_binding::{
core::{ core::{
asset::AssetContent, asset::AssetContent,
context::AssetContext, context::AssetContext,
module::Module,
reference_type::{EntryReferenceSubType, ReferenceType}, reference_type::{EntryReferenceSubType, ReferenceType},
source::Source, source::Source,
virtual_source::VirtualSource, virtual_source::VirtualSource,
...@@ -23,7 +21,10 @@ use turbopack_binding::{ ...@@ -23,7 +21,10 @@ use turbopack_binding::{
}, },
}; };
use crate::util::{load_next_js_template, virtual_next_js_template_path, NextRuntime}; use crate::{
next_edge::entry::wrap_edge_entry,
util::{load_next_js_template, virtual_next_js_template_path, NextRuntime},
};
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn create_page_ssr_entry_module( pub async fn create_page_ssr_entry_module(
...@@ -137,37 +138,3 @@ pub async fn create_page_ssr_entry_module( ...@@ -137,37 +138,3 @@ pub async fn create_page_ssr_entry_module(
Ok(ssr_module) Ok(ssr_module)
} }
#[turbo_tasks::function]
pub async fn wrap_edge_entry(
context: Vc<Box<dyn AssetContext>>,
project_root: Vc<FileSystemPath>,
entry: Vc<Box<dyn Module>>,
pathname: String,
) -> Result<Vc<Box<dyn Module>>> {
let mut source = RopeBuilder::default();
writedoc!(
source,
r#"
import * as module from "MODULE"
self._ENTRIES ||= {{}}
self._ENTRIES[{}] = module
"#,
StringifyJs(&format_args!("middleware_{}", pathname))
)?;
let file = File::from(source.build());
// TODO(alexkirsz) Figure out how to name this virtual asset.
let virtual_source = VirtualSource::new(
project_root.join("edge-wrapper.js".to_string()),
AssetContent::file(file.into()),
);
let inner_assets = indexmap! {
"MODULE".to_string() => entry
};
Ok(context.process(
Vc::upcast(virtual_source),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
))
}
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