Skip to content

Commit

Permalink
fix(api-file-manager): improve handling of cache-control headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Jul 30, 2024
1 parent b638f0d commit 5795b78
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const assetDeliveryConfig = (params: AssetDeliveryParams) => {
const region = process.env.AWS_REGION as string;

const {
presignedUrlTtl = 900,
// Presigned URLs last 7 days (maximum length allowed by AWS).
presignedUrlTtl = 604800,
imageResizeWidths = [100, 300, 500, 750, 1000, 1500, 2500],
...baseParams
} = params;
Expand Down
1 change: 1 addition & 0 deletions packages/api-file-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@webiny/pubsub": "0.0.0",
"@webiny/tasks": "0.0.0",
"@webiny/validation": "0.0.0",
"cache-control-parser": "^2.0.6",
"lodash": "^4.17.21",
"object-hash": "^3.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import { AssetOutputStrategy, SetCacheControlHeaders } from "~/delivery";
import { ResponseHeaders } from "@webiny/handler";

export class PrivateCache extends SetCacheControlHeaders {
constructor(days: number, strategy: AssetOutputStrategy | undefined) {
super(
ResponseHeaders.create({
"cache-control": `private, max-age=${86400 * days}`
}),
strategy
);
import { parse, stringify } from "cache-control-parser";
import { Asset, AssetOutputStrategy, AssetReply } from "~/delivery";

export class PrivateCache implements AssetOutputStrategy {
private strategy: AssetOutputStrategy;

constructor(strategy: AssetOutputStrategy) {
this.strategy = strategy;
}

async output(asset: Asset): Promise<AssetReply> {
const reply = await this.strategy.output(asset);

reply.setHeaders(headers => {
headers.set("cache-control", (value = "") => {
const cacheControl = parse(value);
cacheControl["private"] = true;
cacheControl["public"] = false;
return stringify(cacheControl);
});
return headers;
});

return reply;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ export class PrivateFilesAssetProcessor implements AssetProcessor {
return asset;
}

console.log("file", file);

try {
await this.assetAuthorizer.authorize(file);
} catch (error) {
Expand All @@ -58,7 +56,10 @@ export class PrivateFilesAssetProcessor implements AssetProcessor {
const processedAsset = await this.assetProcessor.process(assetRequest, asset);

processedAsset.setOutputStrategy(strategy => {
return isPrivateFile ? new PrivateCache(30, strategy) : new PublicCache(365, strategy);
if (!strategy) {
throw Error(`No asset output strategy is configured!`);
}
return isPrivateFile ? new PrivateCache(strategy) : new PublicCache(strategy);
});

return processedAsset;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import { AssetOutputStrategy, SetCacheControlHeaders } from "~/delivery";
import { ResponseHeaders } from "@webiny/handler";

export class PublicCache extends SetCacheControlHeaders {
constructor(days: number, strategy: AssetOutputStrategy | undefined) {
super(
ResponseHeaders.create({
"cache-control": `public, max-age=${86400 * days}`
}),
strategy
);
import { parse, stringify } from "cache-control-parser";
import { Asset, AssetOutputStrategy, AssetReply } from "~/delivery";

export class PublicCache implements AssetOutputStrategy {
private strategy: AssetOutputStrategy;

constructor(strategy: AssetOutputStrategy) {
this.strategy = strategy;
}

async output(asset: Asset): Promise<AssetReply> {
const reply = await this.strategy.output(asset);

reply.setHeaders(headers => {
headers.set("cache-control", (value = "") => {
const cacheControl = parse(value);
cacheControl["private"] = false;
cacheControl["public"] = true;
return stringify(cacheControl);
});
return headers;
});

return reply;
}
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15868,6 +15868,7 @@ __metadata:
"@webiny/tasks": 0.0.0
"@webiny/utils": 0.0.0
"@webiny/validation": 0.0.0
cache-control-parser: ^2.0.6
jest: ^29.7.0
lodash: ^4.17.21
object-hash: ^3.0.0
Expand Down Expand Up @@ -22540,6 +22541,13 @@ __metadata:
languageName: node
linkType: hard

"cache-control-parser@npm:^2.0.6":
version: 2.0.6
resolution: "cache-control-parser@npm:2.0.6"
checksum: 18c7905e5bd96c83a06b8cdeb448638ea0a9ffa2a43a77b38930e6373f88169a35df44d2e065267b522cc5fc8ddec9c323073598d20e903c924d1b01cba46588
languageName: node
linkType: hard

"cacheable-lookup@npm:^5.0.3":
version: 5.0.4
resolution: "cacheable-lookup@npm:5.0.4"
Expand Down

0 comments on commit 5795b78

Please sign in to comment.