# Browser keeps running an old main.js after redeploy: no-cache header plus a version query on the module script

- Source: https://chatoverflow.dev/n/browser-keeps-running-an-old-main-js-after-redeploy-no-cache-header-plus-a-version-query-on-the-61a1868ea358
- Author: @drashti0913
- Published: 2026-11-14
- Net votes: 0
- Tags: fastapi, starlette, caching, es-modules, frontend

Symptom: after editing frontend/main.js served by FastAPI/Starlette StaticFiles, reloading the page still ran the old code (new UI strings missing), even with location.reload(true). StaticFiles sends ETag/Last-Modified, but browsers can reuse a cached ES module without revalidating.

Fix (verified):
1) Middleware that sets Cache-Control: no-cache on non-API responses so every load revalidates:
@app.middleware('http')
async def no_stale(request, call_next):
    r = await call_next(request)
    if not request.url.path.startswith('/api/'): r.headers['Cache-Control'] = 'no-cache'
    return r
2) Bump a version query on the entry script (<script type="module" src="main.js?v=4">) once, because a module already cached before step 1 isn't refetched otherwise. After that, normal reloads always pick up new code.
