Skip to content

Commit

Permalink
@next/font/google: Hash filenames used in virtual file paths (vercel/…
Browse files Browse the repository at this point in the history
…turbo#2978)

* Implement `DeterministicHash` for `&str`

* @next/font/google: Hash filenames used in virtual file paths

* Apply suggestions from code review

Co-authored-by: Leah <github.leah@hrmny.sh>

Co-authored-by: Leah <github.leah@hrmny.sh>
  • Loading branch information
wbinnssmith and ForsakenHarmony committed Dec 13, 2022
1 parent 3db16aa commit bcb256a
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/next-swc/crates/next-core/src/next_font_google/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use once_cell::sync::Lazy;
use turbo_tasks::primitives::{OptionStringVc, OptionU16Vc, StringVc};
use turbo_tasks_fetch::fetch;
use turbo_tasks_fs::{FileContent, FileSystemPathVc};
use turbo_tasks_hash::hash_xxh3_hash64;
use turbopack_core::{
issue::IssueSeverity,
resolve::{
Expand Down Expand Up @@ -72,9 +73,10 @@ impl ImportMappingReplacement for NextFontGoogleReplacer {
let query = &*query_vc.await?;
let options = font_options_from_query_map(*query_vc);
let properties = get_font_css_properties(options).await?;
let request_id = get_request_id(*query_vc).await?.await?;
let js_asset = VirtualAssetVc::new(
attached_next_js_package_path(self.project_path)
.join("internal/font/google/inter.js"),
.join(&format!("internal/font/google/{}.js", request_id)),
FileContent::Content(
formatdoc!(
r#"
Expand Down Expand Up @@ -137,15 +139,17 @@ impl ImportMappingReplacement for NextFontGoogleCssModuleReplacer {
let Request::Module {
module: _,
path: _,
query,
query: query_vc,
} = request else {
return Ok(ImportMapResult::NoEntry.into());
};
request.request();

let options = font_options_from_query_map(*query);
let options = font_options_from_query_map(*query_vc);
let stylesheet_url = get_stylesheet_url_from_options(options);
let request_id = get_request_id(*query_vc).await?.await?;
let css_virtual_path = attached_next_js_package_path(self.project_path)
.join("internal/font/google/cssmodule.module.css");
.join(&format!("internal/font/google/{}.module.css", request_id));

let stylesheet_res = fetch(
stylesheet_url,
Expand Down Expand Up @@ -212,6 +216,23 @@ impl ImportMappingReplacement for NextFontGoogleCssModuleReplacer {
}
}

async fn get_request_id(query_vc: QueryMapVc) -> Result<StringVc> {
let query = &*query_vc.await?;
let query = query.as_ref().context("Query map must be present")?;
let mut to_hash = vec![];
for (k, v) in query {
to_hash.push(k);
to_hash.push(v);
}

let options = font_options_from_query_map(query_vc).await?;
Ok(StringVc::cell(format!(
"{}_{:x?}",
options.font_family.to_lowercase().replace(' ', "_"),
hash_xxh3_hash64(to_hash)
)))
}

#[turbo_tasks::function]
async fn get_stylesheet_url_from_options(options: NextFontGoogleOptionsVc) -> Result<StringVc> {
let options = options.await?;
Expand Down

0 comments on commit bcb256a

Please sign in to comment.