fix(scaffold): Google Fonts URL内の & が & に変換される問題を修正#900
Open
teppei-d wants to merge 1 commit into
Open
Conversation
…SDOM Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
本番環境でWebフォント(Google Fonts)が適用されない現象が発生していた。
原因
2段階の問題が絡んでいた。
① JSDOM による
&変換pageCompilerのビルドプロセスでは、PugテンプレートのコンパイルにJSDOMを使用している。JSDOMがHTMLをシリアライズする際、属性値内の&を仕様に従い&にエスケープする。その結果、Google Fonts の
<link>タグが以下のように変換される。変換前(Pugソース)
変換後(ビルド出力)
② Cloudflare Fonts の挙動
Cloudflare Fonts が有効なプロジェクトでは、Google Fonts の
<link>要素が自動的に<style>(@font-face)にインライン展開される。この変換処理が&を正しく解釈できず、URLが破損してフォントが読み込めなくなっていた。&であれば正常に処理される。手動で
&を&に書き換えたところ、フォントが正常に反映されることを確認している。対応
pageCompilerのreplaceオプション(JSDOMシリアライズ・Prettier・minifier 処理が完了した後に実行される最終テキスト変換フック)を使用し、fonts.googleapis.comおよびfonts.gstatic.comのhref属性内に限定して&を&に戻す処理を追加した。正規表現を
fonts.googleapis.com/fonts.gstatic.comのhref属性内に限定することで、他の要素の&には影響しない。replaceAllにより、URL内に&が複数ある場合も全て処理される。テストについて
このスキャフォールドにテストコードは追加していない。
replace関数は純粋な文字列変換だが、このリポジトリから生成される案件リポジトリにテストコードが含まれることになりノイズになるため、意図的に追加しない方針としている。動作確認は案件リポジトリのビルド出力(
htdocs/のHTMLファイル)で、Google Fonts の<link>要素のhref属性内が&ではなく&で出力されることを確認済み。