Skip to content

Commit

Permalink
DAM: Fix/set cache-control headers (#2653)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarnutsch authored Oct 25, 2024
1 parent c3bebef commit 6b0eceb
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 @@ -109,7 +109,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 @@ -130,7 +130,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)
}

@DisableCometGuards()
Expand All @@ -147,7 +147,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)
}

@DisableCometGuards()
Expand All @@ -163,7 +163,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 isValidHash(hash: string, fileParams: FileParams): boolean {
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 @@ -64,7 +64,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 @@ -90,7 +90,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 @@ -107,7 +107,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)
});
}

@DisableCometGuards()
Expand All @@ -123,7 +125,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 6b0eceb

Please sign in to comment.