Skip to content

Commit 2a94606

Browse files
Deactivate frontend caching via the backend
1 parent 74623ec commit 2a94606

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
# Future Release
44

5+
* Web App
6+
* Changed caching rules to ensure that users don't get stuck with old versions of the webapp post update
7+
58

69
## 0.4.10
710

amplipi/app.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,33 @@ def doc():
940940
return FileResponse(f'{TEMPLATE_DIR}/rest-api-doc.html')
941941

942942

943+
class CachelessFiles(StaticFiles):
944+
"""Filters what files should or shouldn't be cached by a user's browser"""
945+
946+
async def get_response(self, path, scope):
947+
response = await super().get_response(path, scope)
948+
949+
content_type = response.headers.get("content-type", "")
950+
951+
# Vite wraps all javascript and CSS files into bundles called index_[hash].js index_[hash].css
952+
# That causes this check to only let images into the cache
953+
954+
# The fact that those files are hashed _should_ make it so that old files are replaced with new files with new hashes post-update
955+
# but the endpoint that provides the files has a cached response that contains the old files
956+
if content_type.startswith("text/html") or "index" in path:
957+
# index.html (root + SPA fallback)
958+
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
959+
response.headers["Pragma"] = "no-cache"
960+
response.headers["Expires"] = "0"
961+
else:
962+
# all versioned/static assets
963+
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
964+
965+
return response
966+
967+
943968
# Website
944-
app.mount('/', StaticFiles(directory=WEB_DIR, html=True), name='web')
969+
app.mount('/', CachelessFiles(directory=WEB_DIR, html=True), name='web')
945970

946971

947972
def create_app(mock_ctrl=None, mock_streams=None, config_file=None, delay_saves=None, settings: models.AppSettings = models.AppSettings()) -> FastAPI:

0 commit comments

Comments
 (0)