Skip to content

Commit

Permalink
Do not cache SPA HTML pages in client (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan authored Oct 18, 2024
1 parent ae84763 commit 135fde2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
35 changes: 22 additions & 13 deletions backend/api/static_files.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
"""Single-page application middleware.
Our application is organized as a single-page application (SPA). This middleware class
extends the functionality of the StaticFiles middleware and was inspired by:
<https://stackoverflow.com/questions/63069190/how-to-capture-arbitrary-paths-at-one-route-in-fastapi>
"""

__authors__ = ["Kris Jordan"]
__copyright__ = "Copyright 2023"
__license__ = "MIT"

from fastapi import Response
from starlette.responses import FileResponse
from starlette.staticfiles import StaticFiles
from starlette.types import Scope
import os

from fastapi.staticfiles import StaticFiles


class StaticFileMiddleware(StaticFiles):
def __init__(self, directory: os.PathLike, index: str = "index.html") -> None:
Expand All @@ -35,3 +26,21 @@ def lookup_path(self, path: str) -> tuple[str, os.stat_result | None]:
return (full_path, stat_result)
else:
return (full_path, stat_result)

async def get_response(self, path: str, scope: Scope) -> Response:
"""Override get_response to set cache-control headers for index.html."""

# Explicitly handle the root path ("/")
if path in ["", "/", "."]:
path = self.index # Treat the root as a request for index.html

full_path, _ = self.lookup_path(path)

# If serving index.html, set cache-control header to prevent caching
if full_path.endswith(self.index):
response = FileResponse(full_path)
response.headers["Cache-Control"] = "no-store"
return response

# For other static files, let the default caching behavior handle it
return await super().get_response(path, scope)
8 changes: 3 additions & 5 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@

# Add application-wide exception handling middleware for commonly encountered API Exceptions
@app.exception_handler(UserPermissionException)
def permission_exception_handler(request: Request, e: UserPermissionException):
def user_permission_exception_handler(request: Request, e: UserPermissionException):
return JSONResponse(status_code=403, content={"message": str(e)})


@app.exception_handler(CoursePermissionException)
def permission_exception_handler(request: Request, e: UserPermissionException):
def course_permission_exception_handler(request: Request, e: UserPermissionException):
return JSONResponse(status_code=403, content={"message": str(e)})


Expand All @@ -132,9 +132,7 @@ def reservation_exception_handler(request: Request, e: ReservationException):


@app.exception_handler(CourseDataScrapingException)
def resource_not_found_exception_handler(
request: Request, e: CourseDataScrapingException
):
def course_data_scraping_exception(request: Request, e: CourseDataScrapingException):
return JSONResponse(status_code=500, content={"message": str(e)})


Expand Down

0 comments on commit 135fde2

Please sign in to comment.