ファイルバージョンによるキャッシュ破棄
ブラウザーに更新済みアセットを取得させます
「ファイルバージョンによるキャッシュ破棄」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Browsers Love to Cache
Browsers save assets to load pages faster. That is great until you ship a new style.css and visitors keep seeing the cached old one. 🗂️
The Stale Asset Problem
If the URL stays /static/style.css forever, the browser assumes nothing changed. Your fresh design simply never reaches the user.
Change the URL to Refetch
The fix is cache busting: change the asset URL whenever the file changes. A new URL forces the browser to download again.
A Version Query String
The simplest trick adds a version query parameter. /static/style.css?v=2 is treated as a new resource by the browser.
<link href="{{ url_for('static', filename='style.css') }}?v=2">Bump It on Each Release
Each time you deploy changed CSS, raise the v number. A central variable beats editing the value in many templates by hand.
Use the File's mtime
A smarter bust uses the file modification time. Read getmtime so the version changes only when the file actually does.
import os
v = int(os.path.getmtime("static/style.css"))Let url_for Carry Extras
Any unknown keyword you pass url_for becomes a query parameter, so you can attach a computed version cleanly.
url_for("static", filename="style.css", v=v)
# -> /static/style.css?v=1718000000Content Hashes Are Best
The strongest approach names files by a hash of their contents, like style.abc123.css. New contents mean a new name and a guaranteed refetch.
Hashes Enable Long Caching
Because a hashed name only changes when contents change, you can cache it almost forever. Speed and freshness stop fighting each other.
Build Tools Do the Work
Asset bundlers generate hashed filenames and a manifest mapping logical names to hashed ones, so your templates ask for the current build.
Pick the Right Tier
Start with a simple ?v= version for small projects. Move to content hashes once a build pipeline is worth the setup.
Quick Check
Why does adding ?v=2 to an asset URL make the browser fetch a fresh copy?
Recap: Cache Busting
You learned to change asset URLs so browsers refetch updates: a version query for small apps, content hashes for serious builds. 🚀
よくある質問
「ファイルバージョンによるキャッシュ破棄」レッスンは無料ですか?
はい。「ファイルバージョンによるキャッシュ破棄」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「ファイルバージョンによるキャッシュ破棄」で何を学びますか?
ブラウザーに更新済みアセットを取得させます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「ファイルバージョンによるキャッシュ破棄」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- staticフォルダーの規約
- url_for staticでアセットにリンクする
- ファイルバージョンによるキャッシュ破棄
- 本番環境における静的ファイルの現実