0Pricing
Flask Academy · Lezione

Cache busting con le versioni dei file

Forzi i browser a recuperare gli asset aggiornati.

Cache busting con le versioni dei file è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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=1718000000

Content 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. 🚀

Domande Frequenti

La lezione «Cache busting con le versioni dei file» è gratuita?

Sì — il testo completo di «Cache busting con le versioni dei file» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.

Cosa imparerò in «Cache busting con le versioni dei file»?

Forzi i browser a recuperare gli asset aggiornati. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Flask Academy?

Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Cache busting con le versioni dei file»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Flask Academy?

Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. La convenzione della cartella static
  2. Collegare gli asset con url_for static
  3. Cache busting con le versioni dei file
  4. I file statici nella realtà della produzione
← Torna a Flask Academy