Skip to content

Commit

Permalink
feat(turbopack): attach type metadata for static metadata item
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Sep 13, 2023
1 parent 32e066f commit 171cfa9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 33 deletions.
9 changes: 8 additions & 1 deletion packages/next-swc/crates/next-core/src/loader_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ use crate::{
MetadataWithAltItem,
},
mode::NextMode,
next_app::{metadata::image::dynamic_image_metadata_source, AppPage},
next_app::{
metadata::{get_content_type, image::dynamic_image_metadata_source},
AppPage,
},
next_image::module::{BlurPlaceholderMode, StructuredImageModuleType},
};

Expand Down Expand Up @@ -291,6 +294,7 @@ impl LoaderTreeBuilder {
let helper_import = "import { fillMetadataSegment } from \
\"next/dist/lib/metadata/get-metadata-route\""
.to_string();

if !self.imports.contains(&helper_import) {
self.imports.push(helper_import);
}
Expand Down Expand Up @@ -329,6 +333,9 @@ impl LoaderTreeBuilder {
)?;
}

let content_type = get_content_type(path).await?;
writeln!(self.loader_tree_code, "{s} type: `{content_type}`,")?;

if let Some(alt_path) = alt_path {
let identifier = magic_identifier::mangle(&format!("{name} alt text #{i}"));
let inner_module_id = format!("METADATA_ALT_{i}");
Expand Down
34 changes: 34 additions & 0 deletions packages/next-swc/crates/next-core/src/next_app/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{collections::HashMap, ops::Deref};

use anyhow::Result;
use once_cell::sync::Lazy;
use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath;

use crate::next_app::{AppPage, PageSegment, PageType};

Expand Down Expand Up @@ -79,6 +81,38 @@ fn match_metadata_file<'a>(
})
}

pub(crate) async fn get_content_type(path: Vc<FileSystemPath>) -> Result<String> {
let stem = &*path.file_stem().await?;
let ext = &*path.extension().await?;

let name = stem.as_deref().unwrap_or_default();
let mut ext = ext.as_str();
if ext == "jpg" {
ext = "jpeg"
}

if name == "favicon" && ext == "ico" {
return Ok("image/x-icon".to_string());
}
if name == "sitemap" {
return Ok("application/xml".to_string());
}
if name == "robots" {
return Ok("text/plain".to_string());
}
if name == "manifest" {
return Ok("application/manifest+json".to_string());
}

if ext == "png" || ext == "jpeg" || ext == "ico" || ext == "svg" {
return Ok(mime_guess::from_ext(ext)
.first_or_octet_stream()
.to_string());
}

Ok("text/plain".to_string())
}

pub fn match_local_metadata_file<'a>(
basename: &'a str,
page_extensions: &[String],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use turbopack_binding::{
},
};

use super::get_content_type;
use crate::{
app_structure::MetadataItem,
mode::NextMode,
Expand Down Expand Up @@ -63,38 +64,6 @@ pub fn get_app_metadata_route_entry(
)
}

async fn get_content_type(path: Vc<FileSystemPath>) -> Result<String> {
let stem = &*path.file_stem().await?;
let ext = &*path.extension().await?;

let name = stem.as_deref().unwrap_or_default();
let mut ext = ext.as_str();
if ext == "jpg" {
ext = "jpeg"
}

if name == "favicon" && ext == "ico" {
return Ok("image/x-icon".to_string());
}
if name == "sitemap" {
return Ok("application/xml".to_string());
}
if name == "robots" {
return Ok("text/plain".to_string());
}
if name == "manifest" {
return Ok("application/manifest+json".to_string());
}

if ext == "png" || ext == "jpeg" || ext == "ico" || ext == "svg" {
return Ok(mime_guess::from_ext(ext)
.first_or_octet_stream()
.to_string());
}

Ok("text/plain".to_string())
}

const CACHE_HEADER_NONE: &str = "no-cache, no-store";
const CACHE_HEADER_LONG_CACHE: &str = "public, immutable, no-transform, max-age=31536000";
const CACHE_HEADER_REVALIDATE: &str = "public, max-age=0, must-revalidate";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface Options {
basePath: string
}

// [NOTE] For turbopack
// refer loader_tree's write_static|dynamic_metadata for corresponding features
async function nextMetadataImageLoader(this: any, content: Buffer) {
const options: Options = this.getOptions()
const { type, segment, pageExtensions, basePath } = options
Expand Down

0 comments on commit 171cfa9

Please sign in to comment.