Unverified Commit 933c9865 authored by Leah's avatar Leah Committed by GitHub

fix(turbopack): read preload option for google fonts (#61679)

### What?

I forgot to check the `preload` option when implementing it and just
looked at the `subsets`.

This also fixes duplicate preload tags by adding them to a set in the
default `_document.tsx`



Closes PACK-2392
parent 23955574
...@@ -383,8 +383,14 @@ async fn update_google_stylesheet( ...@@ -383,8 +383,14 @@ async fn update_google_stylesheet(
&format!("font-family: '{}';", &*scoped_font_family.await?), &format!("font-family: '{}';", &*scoped_font_family.await?),
); );
let font_files = let font_files = find_font_files_in_css(
find_font_files_in_css(&stylesheet, options.subsets.as_deref().unwrap_or_default()); &stylesheet,
if options.preload {
options.subsets.as_deref().unwrap_or_default()
} else {
Default::default()
},
);
let has_size_adjust = *has_size_adjust.await?; let has_size_adjust = *has_size_adjust.await?;
......
...@@ -79,27 +79,25 @@ pub(super) fn options_from_request( ...@@ -79,27 +79,25 @@ pub(super) fn options_from_request(
)); ));
} }
// Invariant enforced above: either None or Some(the only item in the vec) // Invariant enforced above: either None or Some(the only item in the vec)
let argument = request.arguments.last(); let argument = request.arguments.last().cloned().unwrap_or_default();
// `import` comes from the imported symbol in JS, which separates with _ // `import` comes from the imported symbol in JS, which separates with _
let font_family = request.import.replace('_', " "); let font_family = request.import.replace('_', " ");
let font_data = data.get(&font_family).context("Unknown font")?; let font_data = data.get(&font_family).context("Unknown font")?;
let requested_weights: IndexSet<String> = argument let requested_weights: IndexSet<String> = argument
.and_then(|argument| { .weight
argument.weight.as_ref().map(|w| match w { .map(|w| match w {
OneOrManyStrings::One(one) => indexset! {one.to_owned()}, OneOrManyStrings::One(one) => indexset! {one},
OneOrManyStrings::Many(many) => IndexSet::from_iter(many.iter().cloned()), OneOrManyStrings::Many(many) => IndexSet::from_iter(many),
})
}) })
.unwrap_or_default(); .unwrap_or_default();
let mut styles = argument let mut styles = argument
.and_then(|argument| { .style
argument.style.as_ref().map(|w| match w { .map(|w| match w {
OneOrManyStrings::One(one) => vec![one.to_owned()], OneOrManyStrings::One(one) => vec![one],
OneOrManyStrings::Many(many) => many.clone(), OneOrManyStrings::Many(many) => many,
})
}) })
.unwrap_or_default(); .unwrap_or_default();
...@@ -162,9 +160,7 @@ pub(super) fn options_from_request( ...@@ -162,9 +160,7 @@ pub(super) fn options_from_request(
} }
} }
let display = argument let display = argument.display.unwrap_or_else(|| "swap".to_owned());
.and_then(|a| a.display.to_owned())
.unwrap_or_else(|| "swap".to_owned());
if !ALLOWED_DISPLAY_VALUES.contains(&display.as_ref()) { if !ALLOWED_DISPLAY_VALUES.contains(&display.as_ref()) {
return Err(anyhow!( return Err(anyhow!(
...@@ -175,7 +171,7 @@ pub(super) fn options_from_request( ...@@ -175,7 +171,7 @@ pub(super) fn options_from_request(
)); ));
} }
if let Some(axes) = argument.and_then(|a| a.axes.as_ref()) { if let Some(axes) = argument.axes.as_ref() {
if !axes.is_empty() && !matches!(weights, FontWeights::Variable) { if !axes.is_empty() && !matches!(weights, FontWeights::Variable) {
return Err(anyhow!("Axes can only be defined for variable fonts")); return Err(anyhow!("Axes can only be defined for variable fonts"));
} }
...@@ -186,12 +182,12 @@ pub(super) fn options_from_request( ...@@ -186,12 +182,12 @@ pub(super) fn options_from_request(
weights, weights,
styles, styles,
display, display,
preload: argument.map(|a| a.preload).unwrap_or(true), preload: argument.preload.unwrap_or(true),
selected_variable_axes: argument.and_then(|a| a.axes.clone()), selected_variable_axes: argument.axes,
fallback: argument.and_then(|a| a.fallback.clone()), fallback: argument.fallback,
adjust_font_fallback: argument.map(|a| a.adjust_font_fallback).unwrap_or(true), adjust_font_fallback: argument.adjust_font_fallback.unwrap_or(true),
variable: argument.and_then(|a| a.variable.clone()), variable: argument.variable,
subsets: argument.and_then(|a| a.subsets.clone()), subsets: argument.subsets,
}) })
} }
......
...@@ -10,28 +10,22 @@ pub(super) struct NextFontRequest { ...@@ -10,28 +10,22 @@ pub(super) struct NextFontRequest {
pub arguments: Vec<NextFontRequestArguments>, pub arguments: Vec<NextFontRequestArguments>,
} }
#[derive(Debug, Deserialize)] #[derive(Clone, Debug, Default, Deserialize)]
pub(super) struct NextFontRequestArguments { pub(super) struct NextFontRequestArguments {
pub weight: Option<OneOrManyStrings>, pub weight: Option<OneOrManyStrings>,
pub subsets: Option<Vec<String>>, pub subsets: Option<Vec<String>>,
pub style: Option<OneOrManyStrings>, pub style: Option<OneOrManyStrings>,
pub display: Option<String>, pub display: Option<String>,
#[serde(default)] pub preload: Option<bool>,
pub preload: bool,
pub axes: Option<Vec<String>>, pub axes: Option<Vec<String>>,
pub fallback: Option<Vec<String>>, pub fallback: Option<Vec<String>>,
#[serde(default = "default_adjust_font_fallback")] pub adjust_font_fallback: Option<bool>,
pub adjust_font_fallback: bool,
pub variable: Option<String>, pub variable: Option<String>,
} }
#[derive(Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub(super) enum OneOrManyStrings { pub(super) enum OneOrManyStrings {
One(String), One(String),
Many(Vec<String>), Many(Vec<String>),
} }
fn default_adjust_font_fallback() -> bool {
true
}
...@@ -371,10 +371,9 @@ function getNextFontLinkTags( ...@@ -371,10 +371,9 @@ function getNextFontLinkTags(
const appFontsEntry = nextFontManifest.pages['/_app'] const appFontsEntry = nextFontManifest.pages['/_app']
const pageFontsEntry = nextFontManifest.pages[dangerousAsPath] const pageFontsEntry = nextFontManifest.pages[dangerousAsPath]
const preloadedFontFiles = [ const preloadedFontFiles = Array.from(
...(appFontsEntry ?? []), new Set([...(appFontsEntry ?? []), ...(pageFontsEntry ?? [])])
...(pageFontsEntry ?? []), )
]
// If no font files should preload but there's an entry for the path, add a preconnect tag. // If no font files should preload but there's an entry for the path, add a preconnect tag.
const preconnectToSelf = !!( const preconnectToSelf = !!(
......
...@@ -5857,29 +5857,28 @@ ...@@ -5857,29 +5857,28 @@
}, },
"test/e2e/next-font/index.test.ts": { "test/e2e/next-font/index.test.ts": {
"passed": [ "passed": [
"next/font app computed styles page using fallback fonts",
"next/font app computed styles page using variables",
"next/font app computed styles page with fonts",
"next/font app import values Variable font without weight range",
"next/font app import values page with font",
"next/font app import values page with local fonts",
"next/font app should use production cache control for fonts"
],
"failed": [
"next/font app Fallback fontfaces google Fraunces", "next/font app Fallback fontfaces google Fraunces",
"next/font app Fallback fontfaces google Indie flower", "next/font app Fallback fontfaces google Indie flower",
"next/font app Fallback fontfaces local Fraunces", "next/font app Fallback fontfaces local Fraunces",
"next/font app Fallback fontfaces local Indie flower", "next/font app Fallback fontfaces local Indie flower",
"next/font app Fallback fontfaces local Roboto multiple weights and styles",
"next/font app Fallback fontfaces local Roboto multiple weights and styles - variable 1", "next/font app Fallback fontfaces local Roboto multiple weights and styles - variable 1",
"next/font app Fallback fontfaces local Roboto multiple weights and styles - variable 2", "next/font app Fallback fontfaces local Roboto multiple weights and styles - variable 2",
"next/font app Fallback fontfaces local Roboto multiple weights and styles",
"next/font app computed styles page using fallback fonts",
"next/font app computed styles page using variables",
"next/font app computed styles page with fonts",
"next/font app import values Variable font without weight range",
"next/font app import values page with font",
"next/font app import values page with local fonts",
"next/font app preload font without preloadable subsets", "next/font app preload font without preloadable subsets",
"next/font app preload font without size adjust", "next/font app preload font without size adjust",
"next/font app preload google fonts with multiple weights/styles", "next/font app preload google fonts with multiple weights/styles",
"next/font app preload page with fonts", "next/font app preload page with fonts",
"next/font app preload page with local fonts", "next/font app preload page with local fonts",
"next/font app preload page without fonts" "next/font app preload page without fonts",
"next/font app should use production cache control for fonts"
], ],
"failed": [],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
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