Skip to content

Commit

Permalink
Add support for Fastly and remove process.env
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Dec 8, 2024
1 parent 3a8d22c commit c9b2fa6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-maps-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cdn-cache-control": patch
---

Remove dependency on node:process
5 changes: 5 additions & 0 deletions .changeset/hot-planes-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cdn-cache-control": minor
---

Adds Fastly support
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.tsup
.tsup
.DS_Store
13 changes: 13 additions & 0 deletions fastly.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import assert from "node:assert";
import { describe, it } from "node:test";
import { CacheHeaders } from "./dist/index.js";

describe("Fastly", () => {
it("merges cdn-cache-control header into cache-control", () => {
const headers = new CacheHeaders(undefined, "fastly").immutable();
assert.strictEqual(
headers.get("Cache-Control"),
"public,s-maxage=31536000,max-age=31536000,immutable",
);
});
});
2 changes: 1 addition & 1 deletion netlify.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "node:assert";
import { before, describe, it } from "node:test";
import { describe, it } from "node:test";
import { CacheHeaders } from "./dist/index.js";

describe("Netlify", () => {
Expand Down
30 changes: 16 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import process from "node:process";

// TODO: Add more CDNs
/** The CDN that the cache headers are being used with. Will work with other CDNs, but may miss platform-specific headers and directives. */
export type CDN =
| "netlify"
| "cloudflare"
| "akamai"
| "vercel"
| "fastly"
| (string & {});

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

function detectCDN(): CDN | undefined {
if (process.env.CDN) {
return process.env.CDN as CDN;
if (globalThis?.process?.env?.CDN) {
return globalThis.process.env.CDN as CDN;
}
if (process.env.VERCEL) {
if (globalThis?.process?.env.VERCEL) {
return "vercel";
}
if (
process.env.NETLIFY ||
process.env.NETLIFY_LOCAL ||
process.env.NETLIFY_BLOBS_CONTEXT
) {
if ("Netlify" in globalThis) {
return "netlify";
}

Expand Down Expand Up @@ -98,10 +93,15 @@ export class CacheHeaders extends Headers {
if (this.#cdn === "netlify") {
cdnDirectives[tieredDirective] = "";
}
this.setCdnCacheControl(cdnDirectives);

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

if (!directives["max-age"]) {
directives["max-age"] = "0";
Expand Down Expand Up @@ -208,6 +208,8 @@ export class CacheHeaders extends Headers {
switch (this.#cdn) {
case "netlify":
return "Netlify-Cache-Tag";
case "fastly":
return "Surrogate-Key";
default:
return "Cache-Tag";
}
Expand Down

0 comments on commit c9b2fa6

Please sign in to comment.