-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api-file-manager): improve handling of cache-control headers
- Loading branch information
Showing
6 changed files
with
63 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 24 additions & 11 deletions
35
packages/api-file-manager/src/delivery/AssetDelivery/privateFiles/PrivateCache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 24 additions & 11 deletions
35
packages/api-file-manager/src/delivery/AssetDelivery/privateFiles/PublicCache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters