Skip to content

perf: ハッシュ付きの Astro アセットをブラウザに長期キャッシュさせる - #822

Open
devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/1795146235-immutable-asset-cache
Open

devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/1795146235-immutable-asset-cache

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

public/_headers を追加し、ファイル名にハッシュを含む Astro のアセット(/_astro/*)に Cache-Control: public, max-age=31536000, immutable を付けました。これまでは CSS や画像も含めてすべて public, max-age=0, must-revalidate だったため、2 回目以降のページ表示でも、ブラウザは表示を止める CSS(BaseLayout.*.css)を毎回サーバーに問い合わせていました。

# public/_headers
/_astro/*
  Cache-Control: public, max-age=31536000, immutable
  • Workers Static Assets の _headers を使っています。run_worker_first は /posts/* だけなので、/_astro/* へのリクエストは Worker を通らず、Worker の変更は不要です。
  • /link-card-images/* は対象外にしました。cacheImage() はダウンロードした画像の内容ではなく取得元 URL をハッシュ化しているため、取得元の画像が変わってもファイル名が変わらず、古い画像が残るおそれがあります。
  • HTML、favicon、Partytown のファイルは URL が固定なので、既定値のままにしています。
  • tests/worker/asset-cache.test.ts: immutable の対象が /_astro/* 以外に広がらないことを確認するテストを追加しました。
  • docs/asset-caching.md: 対象範囲と、対象外にした理由を記載しました。

計測結果

pnpm run build:ci でビルドした dist を wrangler dev で配信し、Playwright で同じブラウザコンテキストから /posts/nespresso-mold/ を 2 回開きました。2 回目の表示を 5 回計測した中央値です(解析タグは除外)。

2 回目の表示 変更前 変更後
サーバーへのリクエスト数(304 を含む) 5 3
そのうち 304 の再検証 5 3
ナビゲーション開始から load まで(localhost) 20.6 ms 19.9 ms

CSS とアバター画像は、2 回目以降はサーバーに問い合わせずブラウザのキャッシュから読み込まれます。localhost では往復の遅延がほぼないため、時間の差は誤差の範囲です。本番では、記事を移動するたびに CSS の再検証 1 往復が減る見込みですが、デプロイ前のため本番の値は計測していません。

再現方法

pnpm run build:ci
pnpm exec wrangler dev
curl -sI http://localhost:8787/_astro/<BaseLayout の CSS ファイル名> | grep -i cache-control
# 本番(デプロイ後)
curl -sI https://sur33.com/_astro/<CSS ファイル名> | grep -i cache-control

コスト・注意点

  • /_astro/* に、ハッシュを含まない固定名のファイルを置くと、ブラウザに最長 1 年間古いものが残ります。Astro が生成するファイルだけが置かれる前提です。
  • 初回表示の速さは変わりません。

テスト

  • pnpm run lint: エラー 0
  • pnpm run test:worker: 74 件成功
  • pnpm run test:e2e --project=chromium: 70 件成功、2 件スキップ

Latency Optimizer opened this PR for Daiki
Implemented in session Speed up blog page loads at sur33.com
Review with Devin · Open in Devin Desktop


Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access that start with 'Devin'.

  • Disable automatic comment, CI, and merge conflict monitoring

@coderabbitai

coderabbitai Bot commented Nov 20, 2026

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a8922f99-8f67-4c9a-a498-c64ac67940c5

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
blog 1163243 Commit Preview URL

Branch Preview URL
Nov 20 2026, 03:53 AM

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants