Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,12 +1632,20 @@ impl AppEndpoint {
original_source: app_entry.pathname.clone(),
..Default::default()
};
let entrypoint_chunk = *app_entry_chunks_ref
.last()
.context("expected app entry chunks for edge app endpoint")?;
let entrypoint = node_root_value
.get_path_to(&*entrypoint_chunk.path().await?)
.context("expected app entry chunk to be within node root")?
.into();
let edge_function_definition = EdgeFunctionDefinition {
files: file_paths_from_root.into_iter().collect(),
wasm: wasm_paths_to_bindings(wasm_paths_from_root).await?,
assets: paths_to_bindings(all_assets),
name: app_function_name(&app_entry.original_name).into(),
page: app_entry.original_name.clone(),
entrypoint,
regions: app_entry
.config
.await?
Expand Down
16 changes: 13 additions & 3 deletions crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::future::IntoFuture;

use anyhow::Result;
use anyhow::{Context, Result};
use next_core::{
middleware::get_middleware_module,
next_edge::entry::wrap_edge_entry,
Expand Down Expand Up @@ -90,6 +90,7 @@ impl MiddlewareEndpoint {
self.project.project_path().owned().await?,
userland_module,
is_proxy,
self.project.next_config(),
);

if matches!(self.runtime, NextRuntime::NodeJs) {
Expand Down Expand Up @@ -253,10 +254,18 @@ impl MiddlewareEndpoint {

let node_root = this.project.node_root().owned().await?;
let node_root_value = node_root.clone();
let edge_chunk_group_ref = edge_chunk_group.await?;
let edge_assets = edge_chunk_group_ref.assets.await?;

let file_paths_from_root =
get_js_paths_from_root(&node_root_value, &edge_chunk_group.await?.assets.await?)
.await?;
get_js_paths_from_root(&node_root_value, &edge_assets).await?;
let entrypoint_asset = *edge_assets
.last()
.context("expected assets for edge middleware endpoint")?;
let entrypoint = node_root_value
.get_path_to(&*entrypoint_asset.path().await?)
.context("expected edge middleware asset to be within node root")?
.into();

let mut output_assets = edge_chunk_group.all_assets().owned().await?;

Expand Down Expand Up @@ -284,6 +293,7 @@ impl MiddlewareEndpoint {
assets: paths_to_bindings(all_assets),
name: rcstr!("middleware"),
page: rcstr!("/"),
entrypoint,
regions,
matchers: matchers.clone(),
env: this.project.edge_env().owned().await?,
Expand Down
9 changes: 4 additions & 5 deletions crates/next-api/src/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use turbopack_core::{
module::Module,
module_graph::{GraphTraversalAction, ModuleGraph, ModuleGraphLayer},
};
use turbopack_css::{CssModuleAsset, ModuleCssAsset};
use turbopack_css::{CssModule, EcmascriptCssModule};

use crate::{
client_references::{ClientManifestEntryType, ClientReferenceData, map_client_references},
Expand Down Expand Up @@ -809,16 +809,15 @@ async fn validate_pages_css_imports_individual(

// If the module being imported isn't a global css module, there is nothing to
// validate.
let module_is_global_css =
ResolvedVc::try_downcast_type::<CssModuleAsset>(module).is_some();
let module_is_global_css = ResolvedVc::try_downcast_type::<CssModule>(module).is_some();

if !module_is_global_css {
return Ok(GraphTraversalAction::Continue);
}

let parent_is_css_module =
ResolvedVc::try_downcast_type::<ModuleCssAsset>(parent_module).is_some()
|| ResolvedVc::try_downcast_type::<CssModuleAsset>(parent_module).is_some();
ResolvedVc::try_downcast_type::<EcmascriptCssModule>(parent_module).is_some()
|| ResolvedVc::try_downcast_type::<CssModule>(parent_module).is_some();

// We also always allow .module css/scss/sass files to import global css files as
// well.
Expand Down
4 changes: 2 additions & 2 deletions crates/next-api/src/nft_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
FxIndexMap, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc,
graph::{AdjacencyMap, GraphTraversal, Visit},
turbobail, turbofmt,
turbofmt,
};
use turbo_tasks_fs::{
DirectoryEntry, File, FileContent, FileSystem, FileSystemPath,
Expand Down Expand Up @@ -287,7 +287,7 @@ impl Asset for NftJsonAsset {
&*current_path.get_type().await?,
FileSystemEntryType::Symlink
) {
turbobail!(
turbo_tasks::turbobail!(
"Encountered file inside of symlink in NFT list: {current_path} \
is a symlink, but {referenced_chunk_path} was created inside of \
it"
Expand Down
9 changes: 9 additions & 0 deletions crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ impl PageEndpoint {
self.source(),
this.original_name.clone(),
*this.pages_structure,
this.pages_project.project().next_config(),
runtime,
)
.await?;
Expand Down Expand Up @@ -1510,13 +1511,21 @@ impl PageEndpoint {
} else {
None
};
let entrypoint_asset = *assets_ref
.last()
.context("expected assets for edge pages endpoint")?;
let entrypoint = node_root
.get_path_to(&*entrypoint_asset.path().await?)
.context("expected edge pages asset to be within node root")?
.into();

let edge_function_definition = EdgeFunctionDefinition {
files: file_paths_from_root.into_iter().collect(),
wasm: wasm_paths_to_bindings(wasm_paths_from_root).await?,
assets: paths_to_bindings(all_assets),
name: pages_function_name(&this.original_name).into(),
page: this.original_name.clone(),
entrypoint,
regions,
matchers: vec![matchers],
env: this.pages_project.project().edge_env().owned().await?,
Expand Down
4 changes: 2 additions & 2 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use serde::{Deserialize, Serialize};
use tracing::{Instrument, field::Empty};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
Completion, Completions, FxIndexMap, IntoTraitRef, NonLocalValue, OperationValue, OperationVc,
ReadRef, ResolvedVc, State, TaskInput, TransientInstance, TryFlatJoinIterExt, Vc,
Completion, Completions, FxIndexMap, NonLocalValue, OperationValue, OperationVc, ReadRef,
ResolvedVc, State, TaskInput, TransientInstance, TryFlatJoinIterExt, Vc,
debug::ValueDebugFormat, fxindexmap, trace::TraceRawVcs,
};
use turbo_tasks_env::{EnvMap, ProcessEnv};
Expand Down
9 changes: 4 additions & 5 deletions crates/next-core/src/hmr_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{
AsyncModuleInfo, ChunkItem, ChunkableModule, ChunkingContext, ChunkingType,
ChunkingTypeOption, EvaluatableAsset,
EvaluatableAsset,
},
ident::AssetIdent,
module::{Module, ModuleSideEffects},
Expand Down Expand Up @@ -164,11 +164,10 @@ impl ModuleReference for HmrEntryModuleReference {
*ModuleResolveResult::module(self.module)
}

#[turbo_tasks::function]
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
Vc::cell(Some(ChunkingType::Parallel {
fn chunking_type(&self) -> Option<ChunkingType> {
Some(ChunkingType::Parallel {
inherit_async: false,
hoisted: false,
}))
})
}
}
32 changes: 29 additions & 3 deletions crates/next-core/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use turbo_tasks::{ResolvedVc, Vc, fxindexmap};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
context::AssetContext,
file_source::FileSource,
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
module::Module,
reference_type::ReferenceType,
};
use turbopack_ecmascript::chunk::{EcmascriptChunkPlaceable, EcmascriptExports};

use crate::util::load_next_js_template;
use crate::{next_config::NextConfig, util::load_next_js_template};

#[turbo_tasks::function]
pub async fn middleware_files(page_extensions: Vc<Vec<RcStr>>) -> Result<Vc<Vec<RcStr>>> {
Expand All @@ -33,6 +34,7 @@ pub async fn get_middleware_module(
project_root: FileSystemPath,
userland_module: ResolvedVc<Box<dyn Module>>,
is_proxy: bool,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn Module>>> {
const INNER: &str = "INNER_MIDDLEWARE_MODULE";

Expand Down Expand Up @@ -86,20 +88,44 @@ pub async fn get_middleware_module(
}
// If we can't cast to EcmascriptChunkPlaceable, continue without validation
// (might be a special module type that doesn't support export checking)
let mut incremental_cache_handler_import = None;
let mut cache_handler_inner_assets = fxindexmap! {};

for cache_handler_path in next_config
.cache_handler(project_root.clone())
.await?
.into_iter()
{
let cache_handler_inner = rcstr!("INNER_INCREMENTAL_CACHE_HANDLER");
incremental_cache_handler_import = Some(cache_handler_inner.clone());
let cache_handler_module = asset_context
.process(
Vc::upcast(FileSource::new(cache_handler_path.clone())),
ReferenceType::Undefined,
)
.module()
.to_resolved()
.await?;
cache_handler_inner_assets.insert(cache_handler_inner, cache_handler_module);
}

// Load the file from the next.js codebase.
let source = load_next_js_template(
"middleware.js",
project_root,
[("VAR_USERLAND", INNER), ("VAR_DEFINITION_PAGE", page_path)],
[],
[],
[(
"incrementalCacheHandler",
incremental_cache_handler_import.as_deref(),
)],
)
.await?;

let inner_assets = fxindexmap! {
let mut inner_assets = fxindexmap! {
rcstr!(INNER) => userland_module
};
inner_assets.extend(cache_handler_inner_assets);

let module = asset_context
.process(
Expand Down
65 changes: 62 additions & 3 deletions crates/next-core/src/next_app/app_page_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use turbopack::ModuleAssetContext;
use turbopack_core::{
asset::{Asset, AssetContent},
context::AssetContext,
file_source::FileSource,
module::Module,
reference_type::ReferenceType,
source::Source,
Expand Down Expand Up @@ -113,6 +114,7 @@ pub async fn get_app_page_entry(
project_root.clone(),
rsc_entry,
page,
next_config,
);
};

Expand All @@ -131,21 +133,78 @@ async fn wrap_edge_page(
project_root: FileSystemPath,
entry: ResolvedVc<Box<dyn Module>>,
page: AppPage,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn Module>>> {
const INNER: &str = "INNER_PAGE_ENTRY";
let mut cache_handler_imports = String::new();
let mut cache_handler_registration = String::new();
let mut incremental_cache_handler_import = None;
let mut cache_handler_inner_assets = fxindexmap! {};

let cache_handlers = next_config.cache_handlers_map().owned().await?;
for (index, (kind, handler_path)) in cache_handlers.iter().enumerate() {
let cache_handler_inner: RcStr = format!("INNER_CACHE_HANDLER_{index}").into();
let cache_handler_var = format!("cacheHandler{index}");
cache_handler_imports.push_str(&format!(
"import {cache_handler_var} from {};\n",
serde_json::to_string(&*cache_handler_inner)?
));
cache_handler_registration.push_str(&format!(
" cacheHandlers.setCacheHandler({}, {cache_handler_var});\n",
serde_json::to_string(kind.as_str())?
));

let cache_handler_module = asset_context
.process(
Vc::upcast(FileSource::new(project_root.join(handler_path)?)),
ReferenceType::Undefined,
)
.module()
.to_resolved()
.await?;
cache_handler_inner_assets.insert(cache_handler_inner, cache_handler_module);
}

for cache_handler_path in next_config
.cache_handler(project_root.clone())
.await?
.into_iter()
{
let cache_handler_inner: RcStr = "INNER_INCREMENTAL_CACHE_HANDLER".into();
incremental_cache_handler_import = Some(cache_handler_inner.clone());
let cache_handler_module = asset_context
.process(
Vc::upcast(FileSource::new(cache_handler_path.clone())),
ReferenceType::Undefined,
)
.module()
.to_resolved()
.await?;
cache_handler_inner_assets.insert(cache_handler_inner, cache_handler_module);
}

let source = load_next_js_template(
"edge-ssr-app.js",
project_root.clone(),
[("VAR_USERLAND", INNER), ("VAR_PAGE", &page.to_string())],
[],
[("incrementalCacheHandler", None)],
[
("cacheHandlerImports", cache_handler_imports.as_str()),
(
"cacheHandlerRegistration",
cache_handler_registration.as_str(),
),
],
[(
"incrementalCacheHandler",
incremental_cache_handler_import.as_deref(),
)],
)
.await?;

let inner_assets = fxindexmap! {
let mut inner_assets = fxindexmap! {
INNER.into() => entry
};
inner_assets.extend(cache_handler_inner_assets);

let wrapped = asset_context
.process(
Expand Down
Loading
Loading