Skip to content

Commit e8d531b

Browse files
feat(api): set max-age for images (#3711)
Image files are immutable and we expect deletion to result in no further requests for a given image, so we can set the max-age to something thicc. Resolves #3426 @ebr @brandonrising @maryhipp
2 parents 23c1a6b + 545e2f5 commit e8d531b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

invokeai/app/api/routers/images.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
images_router = APIRouter(prefix="/v1/images", tags=["images"])
2121

22+
# images are immutable; set a high max-age
23+
IMAGE_MAX_AGE = 31536000
24+
2225

2326
@images_router.post(
2427
"/",
@@ -155,12 +158,14 @@ async def get_image_full(
155158
if not ApiDependencies.invoker.services.images.validate_path(path):
156159
raise HTTPException(status_code=404)
157160

158-
return FileResponse(
161+
response = FileResponse(
159162
path,
160163
media_type="image/png",
161164
filename=image_name,
162165
content_disposition_type="inline",
163166
)
167+
response.headers["Cache-Control"] = f"max-age={IMAGE_MAX_AGE}"
168+
return response
164169
except Exception as e:
165170
raise HTTPException(status_code=404)
166171

@@ -189,9 +194,11 @@ async def get_image_thumbnail(
189194
if not ApiDependencies.invoker.services.images.validate_path(path):
190195
raise HTTPException(status_code=404)
191196

192-
return FileResponse(
197+
response = FileResponse(
193198
path, media_type="image/webp", content_disposition_type="inline"
194199
)
200+
response.headers["Cache-Control"] = f"max-age={IMAGE_MAX_AGE}"
201+
return response
195202
except Exception as e:
196203
raise HTTPException(status_code=404)
197204

0 commit comments

Comments
 (0)