Skip to content

Commit

Permalink
DAM: Fix/set cache-control headers (v6) (#2807)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Karnutsch <dkarnutsch@users.noreply.github.com>
  • Loading branch information
thomasdax98 and dkarnutsch authored Nov 25, 2024
1 parent 010b5c4 commit ee5b5ae
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .changeset/real-lizards-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@comet/cms-api": patch
---

DAM: Fix/set cache-control headers

- Public endpoints should cache files for 1 day
- Private endpoints should cache files for 1 year - but only in local caches (not CDN)
8 changes: 4 additions & 4 deletions packages/api/cms-api/src/dam/files/files.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function createFilesController({ Scope: PassedScope }: { Scope?: Type<Dam
throw new ForbiddenException();
}

return this.streamFile(file, res, { range, overrideHeaders: { "Cache-control": "private" } });
return this.streamFile(file, res, { range, overrideHeaders: { "cache-control": "max-age=31536000, private" } }); // Local caches only (1 year)
}

@Get(`/download/preview/${fileUrl}`)
Expand All @@ -128,7 +128,7 @@ export function createFilesController({ Scope: PassedScope }: { Scope?: Type<Dam
}

res.setHeader("Content-Disposition", "attachment");
return this.streamFile(file, res, { range, overrideHeaders: { "Cache-control": "private" } });
return this.streamFile(file, res, { range, overrideHeaders: { "cache-control": "max-age=31536000, private" } }); // Local caches only (1 year)
}

@DisableGlobalGuard()
Expand All @@ -152,7 +152,7 @@ export function createFilesController({ Scope: PassedScope }: { Scope?: Type<Dam
}

res.setHeader("Content-Disposition", "attachment");
return this.streamFile(file, res, { range });
return this.streamFile(file, res, { range, overrideHeaders: { "cache-control": "max-age=86400, public" } }); // Public cache (1 day)
}

@DisableGlobalGuard()
Expand All @@ -175,7 +175,7 @@ export function createFilesController({ Scope: PassedScope }: { Scope?: Type<Dam
throw new NotFoundException();
}

return this.streamFile(file, res, { range });
return this.streamFile(file, res, { range, overrideHeaders: { "cache-control": "max-age=86400, public" } }); // Public cache (1 day)
}

private checkCdnOrigin(incomingCdnOriginHeader: string): void {
Expand Down
12 changes: 8 additions & 4 deletions packages/api/cms-api/src/dam/images/images.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class ImagesController {
}

return this.getCroppedImage(file, params, accept, res, {
"cache-control": "private",
"cache-control": "max-age=31536000, private", // Local caches only (1 year)
});
}

Expand All @@ -102,7 +102,7 @@ export class ImagesController {
}

return this.getCroppedImage(file, params, accept, res, {
"cache-control": "private",
"cache-control": "max-age=31536000, private", // Local caches only (1 year)
});
}

Expand All @@ -126,7 +126,9 @@ export class ImagesController {
throw new NotFoundException();
}

return this.getCroppedImage(file, params, accept, res);
return this.getCroppedImage(file, params, accept, res, {
"cache-control": "max-age=86400, public", // Public cache (1 day)
});
}

@DisableGlobalGuard()
Expand All @@ -149,7 +151,9 @@ export class ImagesController {
throw new NotFoundException();
}

return this.getCroppedImage(file, params, accept, res);
return this.getCroppedImage(file, params, accept, res, {
"cache-control": "max-age=86400, public", // Public cache (1 day)
});
}

private isValidHash({ hash, ...imageParams }: HashImageParams): boolean {
Expand Down

0 comments on commit ee5b5ae

Please sign in to comment.