Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(turbopack/next-core): Migrate app_page_loader_tree structs to ResolvedVc #74114

Merged
merged 1 commit into from
Dec 19, 2024
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
12 changes: 9 additions & 3 deletions crates/next-core/src/app_page_loader_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,14 @@ impl AppPageLoaderTreeBuilder {
) -> Result<()> {
match item {
MetadataWithAltItem::Static { path, alt_path } => {
self.write_static_metadata_item(app_page, name, item, *path, *alt_path)
.await?;
self.write_static_metadata_item(
app_page,
name,
item,
**path,
alt_path.as_deref().copied(),
)
.await?;
}
MetadataWithAltItem::Dynamic { path, .. } => {
let i = self.base.unique_number();
Expand All @@ -183,7 +189,7 @@ impl AppPageLoaderTreeBuilder {

let source = dynamic_image_metadata_source(
Vc::upcast(self.base.module_asset_context),
*path,
**path,
name.into(),
app_page.clone(),
);
Expand Down
54 changes: 35 additions & 19 deletions crates/next-core/src/app_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use serde::{Deserialize, Serialize};
use tracing::Instrument;
use turbo_rcstr::RcStr;
use turbo_tasks::{
debug::ValueDebugFormat, fxindexmap, trace::TraceRawVcs, FxIndexMap, ResolvedVc, TaskInput,
TryJoinIterExt, ValueDefault, ValueToString, Vc,
debug::ValueDebugFormat, fxindexmap, trace::TraceRawVcs, FxIndexMap, NonLocalValue, ResolvedVc,
TaskInput, TryJoinIterExt, ValueDefault, ValueToString, Vc,
};
use turbo_tasks_fs::{DirectoryContent, DirectoryEntry, FileSystemEntryType, FileSystemPath};
use turbopack_core::issue::{
Expand All @@ -27,7 +27,7 @@ use crate::{
};

/// A final route in the app directory.
#[turbo_tasks::value(local)]
#[turbo_tasks::value]
#[derive(Default, Debug, Clone)]
pub struct AppDirModules {
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -76,24 +76,34 @@ impl AppDirModules {
}

/// A single metadata file plus an optional "alt" text file.
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, TraceRawVcs)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, TraceRawVcs, NonLocalValue)]
pub enum MetadataWithAltItem {
Static {
path: Vc<FileSystemPath>,
alt_path: Option<Vc<FileSystemPath>>,
path: ResolvedVc<FileSystemPath>,
alt_path: Option<ResolvedVc<FileSystemPath>>,
},
Dynamic {
path: Vc<FileSystemPath>,
path: ResolvedVc<FileSystemPath>,
},
}

/// A single metadata file.
#[derive(
Copy, Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq, TaskInput, TraceRawVcs,
Copy,
Clone,
Debug,
Hash,
Serialize,
Deserialize,
PartialEq,
Eq,
TaskInput,
TraceRawVcs,
NonLocalValue,
)]
pub enum MetadataItem {
Static { path: Vc<FileSystemPath> },
Dynamic { path: Vc<FileSystemPath> },
Static { path: ResolvedVc<FileSystemPath> },
Dynamic { path: ResolvedVc<FileSystemPath> },
}

#[turbo_tasks::function]
Expand All @@ -120,7 +130,7 @@ pub async fn get_metadata_route_name(meta: MetadataItem) -> Result<Vc<RcStr>> {
}

impl MetadataItem {
pub fn into_path(self) -> Vc<FileSystemPath> {
pub fn into_path(self) -> ResolvedVc<FileSystemPath> {
match self {
MetadataItem::Static { path } => path,
MetadataItem::Dynamic { path } => path,
Expand All @@ -138,7 +148,9 @@ impl From<MetadataWithAltItem> for MetadataItem {
}

/// Metadata file that can be placed in any segment of the app directory.
#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, TraceRawVcs)]
#[derive(
Default, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, TraceRawVcs, NonLocalValue,
)]
pub struct Metadata {
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub icon: Vec<MetadataWithAltItem>,
Expand Down Expand Up @@ -338,17 +350,17 @@ async fn get_directory_tree_internal(
"opengraph-image" => &mut metadata_open_graph,
"sitemap" => {
if dynamic {
modules.metadata.sitemap = Some(MetadataItem::Dynamic { path: *file });
modules.metadata.sitemap = Some(MetadataItem::Dynamic { path: file });
} else {
modules.metadata.sitemap = Some(MetadataItem::Static { path: *file });
modules.metadata.sitemap = Some(MetadataItem::Static { path: file });
}
continue;
}
_ => continue,
};

if dynamic {
entry.push((number, MetadataWithAltItem::Dynamic { path: *file }));
entry.push((number, MetadataWithAltItem::Dynamic { path: file }));
continue;
}

Expand All @@ -357,14 +369,18 @@ async fn get_directory_tree_internal(
let basename = file_name
.rsplit_once('.')
.map_or(file_name, |(basename, _)| basename);
let alt_path = file.parent().join(format!("{}.alt.txt", basename).into());
let alt_path = file
.parent()
.join(format!("{}.alt.txt", basename).into())
.to_resolved()
.await?;
let alt_path = matches!(&*alt_path.get_type().await?, FileSystemEntryType::File)
.then_some(alt_path);

entry.push((
number,
MetadataWithAltItem::Static {
path: *file,
path: file,
alt_path,
},
));
Expand Down Expand Up @@ -1401,9 +1417,9 @@ pub async fn get_global_metadata(
};

if dynamic {
*entry = Some(MetadataItem::Dynamic { path: *file });
*entry = Some(MetadataItem::Dynamic { path: file });
} else {
*entry = Some(MetadataItem::Static { path: *file });
*entry = Some(MetadataItem::Static { path: file });
}
// TODO(WEB-952) handle symlinks in app dir
}
Expand Down
14 changes: 6 additions & 8 deletions crates/next-core/src/next_app/metadata/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ pub async fn get_app_metadata_route_source(
is_multi_dynamic: bool,
) -> Result<Vc<Box<dyn Source>>> {
Ok(match metadata {
MetadataItem::Static { path } => static_route_source(mode, path),
MetadataItem::Static { path } => static_route_source(mode, *path),
MetadataItem::Dynamic { path } => {
let stem = path.file_stem().await?;
let stem = stem.as_deref().unwrap_or_default();

if stem == "robots" || stem == "manifest" {
dynamic_text_route_source(path)
dynamic_text_route_source(*path)
} else if stem == "sitemap" {
dynamic_site_map_route_source(mode, path, is_multi_dynamic)
dynamic_site_map_route_source(mode, *path, is_multi_dynamic)
} else {
dynamic_image_route_source(path)
dynamic_image_route_source(*path)
}
}
})
Expand All @@ -60,11 +60,9 @@ pub async fn get_app_metadata_route_entry(
) -> Vc<AppEntry> {
// Read original source's segment config before replacing source into
// dynamic|static metadata route handler.
let original_path = match metadata {
MetadataItem::Static { path } | MetadataItem::Dynamic { path } => path,
};
let original_path = metadata.into_path();

let source = Vc::upcast(FileSource::new(original_path));
let source = Vc::upcast(FileSource::new(*original_path));
let segment_config = parse_segment_config_from_source(source);
let is_dynamic_metadata = matches!(metadata, MetadataItem::Dynamic { .. });
let is_multi_dynamic: bool = if Some(segment_config).is_some() {
Expand Down
Loading