Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# Future Release

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


## 0.4.10

Expand Down
27 changes: 26 additions & 1 deletion amplipi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,33 @@ def doc():
return FileResponse(f'{TEMPLATE_DIR}/rest-api-doc.html')


class CachelessFiles(StaticFiles):
"""Filters what files should or shouldn't be cached by a user's browser"""

async def get_response(self, path, scope):
response = await super().get_response(path, scope)

content_type = response.headers.get("content-type", "")

# Vite wraps all javascript and CSS files into bundles called index_[hash].js index_[hash].css
# That causes this check to only let images into the cache

# The fact that those files are hashed _should_ make it so that old files are replaced with new files with new hashes post-update
# but the endpoint that provides the files has a cached response that contains the old files
if content_type.startswith("text/html") or "index" in path:
# index.html (root + SPA fallback)
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
else:
# all versioned/static assets
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"

return response


# Website
app.mount('/', StaticFiles(directory=WEB_DIR, html=True), name='web')
app.mount('/', CachelessFiles(directory=WEB_DIR, html=True), name='web')


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