Skip to content

Commit 3a77195

Browse files
authored
Add support for Fastly and remove process.env (#13)
1 parent 3a8d22c commit 3a77195

File tree

6 files changed

+42
-16
lines changed

6 files changed

+42
-16
lines changed

.changeset/clever-maps-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cdn-cache-control": patch
3+
---
4+
5+
Remove dependency on node:process

.changeset/hot-planes-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cdn-cache-control": minor
3+
---
4+
5+
Adds Fastly support

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,5 @@ dist
128128
.yarn/build-state.yml
129129
.yarn/install-state.gz
130130
.pnp.*
131-
.tsup
131+
.tsup
132+
.DS_Store

fastly.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import assert from "node:assert";
2+
import { describe, it } from "node:test";
3+
import { CacheHeaders } from "./dist/index.js";
4+
5+
describe("Fastly", () => {
6+
it("merges cdn-cache-control header into cache-control", () => {
7+
const headers = new CacheHeaders(undefined, "fastly").immutable();
8+
assert.strictEqual(
9+
headers.get("Cache-Control"),
10+
"public,s-maxage=31536000,max-age=31536000,immutable",
11+
);
12+
});
13+
});

netlify.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "node:assert";
2-
import { before, describe, it } from "node:test";
2+
import { describe, it } from "node:test";
33
import { CacheHeaders } from "./dist/index.js";
44

55
describe("Netlify", () => {

src/index.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import process from "node:process";
2-
3-
// TODO: Add more CDNs
41
/** The CDN that the cache headers are being used with. Will work with other CDNs, but may miss platform-specific headers and directives. */
52
export type CDN =
63
| "netlify"
74
| "cloudflare"
85
| "akamai"
96
| "vercel"
7+
| "fastly"
108
| (string & {});
119

1210
/** Number of seconds in one minute */
@@ -28,20 +26,17 @@ const cdnCacheControlHeaderNames = new Map<CDN, string>([
2826
["cloudflare", "Cloudflare-CDN-Cache-Control"],
2927
["akamai", "Akamai-Cache-Control"],
3028
["vercel", "Vercel-CDN-Cache-Control"],
29+
["fastly", "Cache-Control"],
3130
]);
3231

3332
function detectCDN(): CDN | undefined {
34-
if (process.env.CDN) {
35-
return process.env.CDN as CDN;
33+
if (globalThis?.process?.env?.CDN) {
34+
return globalThis.process.env.CDN as CDN;
3635
}
37-
if (process.env.VERCEL) {
36+
if (globalThis?.process?.env.VERCEL) {
3837
return "vercel";
3938
}
40-
if (
41-
process.env.NETLIFY ||
42-
process.env.NETLIFY_LOCAL ||
43-
process.env.NETLIFY_BLOBS_CONTEXT
44-
) {
39+
if ("Netlify" in globalThis) {
4540
return "netlify";
4641
}
4742

@@ -98,10 +93,15 @@ export class CacheHeaders extends Headers {
9893
if (this.#cdn === "netlify") {
9994
cdnDirectives[tieredDirective] = "";
10095
}
101-
this.setCdnCacheControl(cdnDirectives);
10296

103-
directives.public = "";
104-
delete directives["s-maxage"];
97+
// If the CDN cache-control header is the same as the browser cache-control header, we merge the directives.
98+
if (this.cdnCacheControlHeaderName === "Cache-Control") {
99+
Object.assign(directives, cdnDirectives);
100+
} else {
101+
this.setCdnCacheControl(cdnDirectives);
102+
delete directives["s-maxage"];
103+
directives.public = "";
104+
}
105105

106106
if (!directives["max-age"]) {
107107
directives["max-age"] = "0";
@@ -208,6 +208,8 @@ export class CacheHeaders extends Headers {
208208
switch (this.#cdn) {
209209
case "netlify":
210210
return "Netlify-Cache-Tag";
211+
case "fastly":
212+
return "Surrogate-Key";
211213
default:
212214
return "Cache-Tag";
213215
}

0 commit comments

Comments
 (0)