From 581f6b7b8f30a6d73557320c348a1a66d901c0e6 Mon Sep 17 00:00:00 2001 From: Adrien Date: Sun, 28 May 2023 10:45:14 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Add=20workaround=20for=20fastapi?= =?UTF-8?q?-cache=20issue=20#144?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/main.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/backend/main.py b/backend/main.py index b27bd7c..d8b359b 100755 --- a/backend/main.py +++ b/backend/main.py @@ -2,7 +2,7 @@ import uvicorn from contextlib import asynccontextmanager -from fastapi import FastAPI +from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from fastapi_cache import FastAPICache @@ -43,6 +43,24 @@ app.add_middleware( app.mount("/widget", StaticFiles(directory="../frontend/", html=True), name="widget") +# The cache-control header entry is not managed properly by fastapi-cache: +# For now, a request with a cache-control set to no-cache +# is interpreted as disabling the use of the server cache. +# Cf. Improve Cache-Control header parsing and handling +# https://github.com/long2ice/fastapi-cache/issues/144 workaround +@app.middleware("http") +async def fastapi_cache_issue_144_workaround(request: Request, call_next): + entries = request.headers.__dict__["_list"] + new_entries = [ + entry for entry in entries if entry[0].decode().lower() != "cache-control" + ] + + request.headers.__dict__["_list"] = new_entries + + return await call_next(request) + + + app.include_router(line.router) app.include_router(stop.router)