Unverified Commit b50ecb7e authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

add support for static implicit metadata in turbopack (#48823)

### What?

* support local static metadata files; opengraph-image, twitter-image,
favicon, manifest, icon, apple-icon
* support global static metadata files: robots.txt, sitemap.xml,
favicon.ico
* dynamic metadata is not yet implemented, but yields a warning

It's implemented a bit different compared to the webpack version.
All images will use the usual image machinery, so they are emitted to
output directory, content hashed and the url is shared with the same
import import in the app.

### Why?

Unsupported, and we want to have that.

### How?

see also https://github.com/vercel/turbo/pull/4692

fixes WEB-524
parent 0cfa3fce
...@@ -8,7 +8,7 @@ use napi::{ ...@@ -8,7 +8,7 @@ use napi::{
}; };
use next_core::app_structure::{ use next_core::app_structure::{
find_app_dir, get_entrypoints as get_entrypoints_impl, Components, ComponentsVc, Entrypoint, find_app_dir, get_entrypoints as get_entrypoints_impl, Components, ComponentsVc, Entrypoint,
EntrypointsVc, LoaderTree, LoaderTreeVc, EntrypointsVc, LoaderTree, LoaderTreeVc, MetadataWithAltItem,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_binding::{ use turbo_binding::{
...@@ -41,7 +41,8 @@ async fn project_fs(project_dir: &str, watching: bool) -> Result<FileSystemVc> { ...@@ -41,7 +41,8 @@ async fn project_fs(project_dir: &str, watching: bool) -> Result<FileSystemVc> {
struct LoaderTreeForJs { struct LoaderTreeForJs {
segment: String, segment: String,
parallel_routes: HashMap<String, LoaderTreeForJsReadRef>, parallel_routes: HashMap<String, LoaderTreeForJsReadRef>,
components: serde_json::Value, #[turbo_tasks(trace_ignore)]
components: ComponentsForJs,
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, ValueDebugFormat, TraceRawVcs)] #[derive(PartialEq, Eq, Serialize, Deserialize, ValueDebugFormat, TraceRawVcs)]
...@@ -69,10 +70,57 @@ async fn fs_path_to_path(project_path: FileSystemPathVc, path: FileSystemPathVc) ...@@ -69,10 +70,57 @@ async fn fs_path_to_path(project_path: FileSystemPathVc, path: FileSystemPathVc)
} }
} }
#[derive(Default, Deserialize, Serialize, PartialEq, Eq, ValueDebugFormat)]
#[serde(rename_all = "camelCase")]
struct ComponentsForJs {
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
layout: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
loading: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
template: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
default: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
route: Option<String>,
metadata: MetadataForJs,
}
#[derive(Default, Deserialize, Serialize, PartialEq, Eq, ValueDebugFormat)]
#[serde(rename_all = "camelCase")]
struct MetadataForJs {
#[serde(skip_serializing_if = "Vec::is_empty")]
icon: Vec<MetadataForJsItem>,
#[serde(skip_serializing_if = "Vec::is_empty")]
apple: Vec<MetadataForJsItem>,
#[serde(skip_serializing_if = "Vec::is_empty")]
twitter: Vec<MetadataForJsItem>,
#[serde(skip_serializing_if = "Vec::is_empty")]
open_graph: Vec<MetadataForJsItem>,
#[serde(skip_serializing_if = "Vec::is_empty")]
favicon: Vec<MetadataForJsItem>,
}
#[derive(Deserialize, Serialize, PartialEq, Eq, ValueDebugFormat)]
#[serde(tag = "type", rename_all = "camelCase")]
enum MetadataForJsItem {
Static {
path: String,
alt_path: Option<String>,
},
Dynamic {
path: String,
},
}
async fn prepare_components_for_js( async fn prepare_components_for_js(
project_path: FileSystemPathVc, project_path: FileSystemPathVc,
components: ComponentsVc, components: ComponentsVc,
) -> Result<serde_json::Value> { ) -> Result<ComponentsForJs> {
let Components { let Components {
page, page,
layout, layout,
...@@ -83,59 +131,66 @@ async fn prepare_components_for_js( ...@@ -83,59 +131,66 @@ async fn prepare_components_for_js(
route, route,
metadata, metadata,
} = &*components.await?; } = &*components.await?;
let mut map = serde_json::value::Map::new(); let mut result = ComponentsForJs::default();
async fn add( async fn add(
map: &mut serde_json::value::Map<String, serde_json::Value>, result: &mut Option<String>,
project_path: FileSystemPathVc, project_path: FileSystemPathVc,
key: &str,
value: &Option<FileSystemPathVc>, value: &Option<FileSystemPathVc>,
) -> Result<()> { ) -> Result<()> {
if let Some(value) = value { if let Some(value) = value {
map.insert( *result = Some(fs_path_to_path(project_path, *value).await?);
key.to_string(),
fs_path_to_path(project_path, *value).await?.into(),
);
} }
Ok::<_, anyhow::Error>(()) Ok::<_, anyhow::Error>(())
} }
add(&mut map, project_path, "page", page).await?; add(&mut result.page, project_path, page).await?;
add(&mut map, project_path, "layout", layout).await?; add(&mut result.layout, project_path, layout).await?;
add(&mut map, project_path, "error", error).await?; add(&mut result.error, project_path, error).await?;
add(&mut map, project_path, "loading", loading).await?; add(&mut result.loading, project_path, loading).await?;
add(&mut map, project_path, "template", template).await?; add(&mut result.template, project_path, template).await?;
add(&mut map, project_path, "default", default).await?; add(&mut result.default, project_path, default).await?;
add(&mut map, project_path, "route", route).await?; add(&mut result.route, project_path, route).await?;
let mut meta = serde_json::value::Map::new(); async fn add_meta<'a>(
async fn add_meta( meta: &mut Vec<MetadataForJsItem>,
meta: &mut serde_json::value::Map<String, serde_json::Value>,
project_path: FileSystemPathVc, project_path: FileSystemPathVc,
key: &str, value: impl Iterator<Item = &'a MetadataWithAltItem>,
value: &Vec<FileSystemPathVc>,
) -> Result<()> { ) -> Result<()> {
if !value.is_empty() { let mut value = value.peekable();
meta.insert( if value.peek().is_some() {
key.to_string(), *meta = value
value .map(|value| async move {
.iter() Ok(match value {
.map(|value| async move { MetadataWithAltItem::Static { path, alt_path } => {
Ok(serde_json::Value::from( let path = fs_path_to_path(project_path, *path).await?;
fs_path_to_path(project_path, *value).await?, let alt_path = if let Some(alt_path) = alt_path {
)) Some(fs_path_to_path(project_path, *alt_path).await?)
} else {
None
};
MetadataForJsItem::Static { path, alt_path }
}
MetadataWithAltItem::Dynamic { path } => {
let path = fs_path_to_path(project_path, *path).await?;
MetadataForJsItem::Dynamic { path }
}
}) })
.try_join() })
.await? .try_join()
.into(), .await?;
);
} }
Ok::<_, anyhow::Error>(()) Ok::<_, anyhow::Error>(())
} }
add_meta(&mut meta, project_path, "icon", &metadata.icon).await?; let meta = &mut result.metadata;
add_meta(&mut meta, project_path, "apple", &metadata.apple).await?; add_meta(&mut meta.icon, project_path, metadata.icon.iter()).await?;
add_meta(&mut meta, project_path, "twitter", &metadata.twitter).await?; add_meta(&mut meta.apple, project_path, metadata.apple.iter()).await?;
add_meta(&mut meta, project_path, "openGraph", &metadata.open_graph).await?; add_meta(&mut meta.twitter, project_path, metadata.twitter.iter()).await?;
add_meta(&mut meta, project_path, "favicon", &metadata.favicon).await?; add_meta(
map.insert("metadata".to_string(), meta.into()); &mut meta.open_graph,
Ok(map.into()) project_path,
metadata.open_graph.iter(),
)
.await?;
add_meta(&mut meta.favicon, project_path, metadata.favicon.iter()).await?;
Ok(result)
} }
#[tasks::function] #[tasks::function]
......
use turbo_binding::turbopack::{
core::{
asset::AssetVc,
context::{AssetContext, AssetContextVc},
},
ecmascript::{
EcmascriptInputTransform, EcmascriptInputTransformsVc, EcmascriptModuleAssetType,
EcmascriptModuleAssetVc,
},
};
use turbo_tasks::Value;
pub(crate) fn as_es_module_asset(
asset: AssetVc,
context: AssetContextVc,
) -> EcmascriptModuleAssetVc {
EcmascriptModuleAssetVc::new(
asset,
context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
}]),
Default::default(),
context.compile_time_info(),
)
}
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
mod app_render; mod app_render;
mod app_source; mod app_source;
pub mod app_structure; pub mod app_structure;
mod asset_helpers;
mod babel; mod babel;
mod embed_js; mod embed_js;
pub mod env; pub mod env;
......
...@@ -24,25 +24,13 @@ use super::source_asset::StructuredImageSourceAsset; ...@@ -24,25 +24,13 @@ use super::source_asset::StructuredImageSourceAsset;
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct StructuredImageModuleType {} pub struct StructuredImageModuleType {}
#[turbo_tasks::value_impl] impl StructuredImageModuleType {
impl StructuredImageModuleTypeVc { pub(crate) fn create_module(
#[turbo_tasks::function]
pub fn new() -> Self {
StructuredImageModuleTypeVc::cell(StructuredImageModuleType {})
}
}
#[turbo_tasks::value_impl]
impl CustomModuleType for StructuredImageModuleType {
#[turbo_tasks::function]
async fn create_module(
&self,
source: AssetVc, source: AssetVc,
context: AssetContextVc, context: AssetContextVc,
_part: Option<ModulePartVc>, ) -> EcmascriptModuleAssetVc {
) -> Result<AssetVc> {
let static_asset = StaticModuleAssetVc::new(source, context); let static_asset = StaticModuleAssetVc::new(source, context);
Ok(EcmascriptModuleAssetVc::new_with_inner_assets( EcmascriptModuleAssetVc::new_with_inner_assets(
StructuredImageSourceAsset { image: source }.cell().into(), StructuredImageSourceAsset { image: source }.cell().into(),
context, context,
Value::new(EcmascriptModuleAssetType::Ecmascript), Value::new(EcmascriptModuleAssetType::Ecmascript),
...@@ -55,6 +43,26 @@ impl CustomModuleType for StructuredImageModuleType { ...@@ -55,6 +43,26 @@ impl CustomModuleType for StructuredImageModuleType {
"IMAGE".to_string() => static_asset.into() "IMAGE".to_string() => static_asset.into()
)), )),
) )
.into()) }
}
#[turbo_tasks::value_impl]
impl StructuredImageModuleTypeVc {
#[turbo_tasks::function]
pub fn new() -> Self {
StructuredImageModuleTypeVc::cell(StructuredImageModuleType {})
}
}
#[turbo_tasks::value_impl]
impl CustomModuleType for StructuredImageModuleType {
#[turbo_tasks::function]
fn create_module(
&self,
source: AssetVc,
context: AssetContextVc,
_part: Option<ModulePartVc>,
) -> AssetVc {
StructuredImageModuleType::create_module(source, context).into()
} }
} }
...@@ -45,6 +45,7 @@ use turbo_tasks::{ ...@@ -45,6 +45,7 @@ use turbo_tasks::{
use turbo_tasks_fs::json::parse_json_with_source_context; use turbo_tasks_fs::json::parse_json_with_source_context;
use crate::{ use crate::{
asset_helpers::as_es_module_asset,
embed_js::{next_asset, next_js_file}, embed_js::{next_asset, next_js_file},
next_config::NextConfigVc, next_config::NextConfigVc,
next_edge::{ next_edge::{
...@@ -156,19 +157,6 @@ async fn get_config( ...@@ -156,19 +157,6 @@ async fn get_config(
Ok(OptionEcmascriptModuleAssetVc::cell(config_asset)) Ok(OptionEcmascriptModuleAssetVc::cell(config_asset))
} }
fn as_es_module_asset(asset: AssetVc, context: AssetContextVc) -> EcmascriptModuleAssetVc {
EcmascriptModuleAssetVc::new(
asset,
context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
}]),
Default::default(),
context.compile_time_info(),
)
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn next_config_changed( async fn next_config_changed(
context: AssetContextVc, context: AssetContextVc,
......
{
"name": "Next.js Static Manifest",
"short_name": "Next.js App",
"description": "Next.js App",
"start_url": "/",
"display": "standalone",
"background_color": "#fff",
"theme_color": "#fff"
}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://vercel.com/</loc>
<lastmod>2023-03-06T18:04:14.008Z</lastmod>
</url>
</urlset>
...@@ -5,7 +5,84 @@ import { useEffect } from 'react' ...@@ -5,7 +5,84 @@ import { useEffect } from 'react'
export default function Test() { export default function Test() {
useEffect(() => { useEffect(() => {
import('@turbo/pack-test-harness').then(() => { import('@turbo/pack-test-harness').then(() => {
it('should run', () => {}) it('should have the correct link tags', () => {
let links = Array.from(document.querySelectorAll('link'))
expect(
links.map((l) => ({
href: l.getAttribute('href'),
rel: l.getAttribute('rel'),
sizes: l.getAttribute('sizes'),
}))
).toEqual([
expect.objectContaining({
rel: 'manifest',
href: expect.stringMatching(/^\/_next\/static\/.+\.webmanifest$/),
sizes: null,
}),
expect.objectContaining({
rel: 'icon',
href: expect.stringMatching(/^\/_next\/static\/.+\.ico$/),
sizes: '48x48',
}),
expect.objectContaining({
rel: 'icon',
href: expect.stringMatching(/^\/_next\/static\/.+\.png$/),
sizes: '32x32',
}),
expect.objectContaining({
rel: 'icon',
href: expect.stringMatching(/^\/_next\/static\/.+\.png$/),
sizes: '64x64',
}),
expect.objectContaining({
rel: 'apple-touch-icon',
href: expect.stringMatching(/^\/_next\/static\/.+\.png$/),
sizes: '114x114',
}),
])
})
it('should have the correct meta tags', () => {
const meta = Array.from(document.querySelectorAll('meta'))
const metaObject = Object.fromEntries(
meta
.filter((l) => l.getAttribute('property'))
.map((l) => [l.getAttribute('property'), l.getAttribute('content')])
)
expect(metaObject).toEqual({
'og:image': expect.stringMatching(/^.+\/_next\/static\/.+\.png$/),
'og:image:width': '114',
'og:image:height': '114',
'og:image:alt': 'This is an alt text.',
})
})
it('should provide a robots.txt', async () => {
const res = await fetch('/robots.txt')
expect(res.status).toBe(200)
expect(await res.text()).toBe('User-Agent: *\nDisallow:\n')
})
it('should provide a sitemap.xml', async () => {
const res = await fetch('/sitemap.xml')
expect(res.status).toBe(200)
expect(await res.text()).toBe(
`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://vercel.com/</loc>
<lastmod>2023-03-06T18:04:14.008Z</lastmod>
</url>
</urlset>
`
)
})
it('should provide a favicon.ico', async () => {
const res = await fetch('/favicon.ico')
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('image/x-icon')
})
}) })
return () => {} return () => {}
}, []) }, [])
......
PlainIssue {
severity: Warning,
context: "[project]/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/implicit-metadata/input/app",
category: "unsupported",
title: "Implicit metadata from filesystem is currently not supported in Turbopack",
description: "The following files were found in the app directory, but are not supported by Turbopack. They are ignored:\n\n- [project]/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/implicit-metadata/input/app/apple-icon.png\n- [project]/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/implicit-metadata/input/app/icon1234.png\n- [project]/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/implicit-metadata/input/app/icon234.png",
detail: "",
documentation_link: "",
source: None,
sub_issues: [],
processing_path: Some(
[
PlainIssueProcessingPathItem {
context: Some(
"[project]/packages/next-swc/crates/next-dev-tests/tests/integration/next/app/implicit-metadata/input/app",
),
description: "Next.js App Page Route /",
},
],
),
}
\ No newline at end of file
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