Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DAM: Fix/set cache-control headers (v6) #2807

Merged
merged 1 commit into from
Nov 25, 2024
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
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
Loading