fix: __FRSH_BUILD_ID is not defined (#1207)

This could happen if the runtime/utils.ts coe was loaded
prior to server/constants.ts, because they had some shared state on
`globalThis`.

I have mitigated this by not relying on global state anymore.

Fixes #1191

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
Luca Casonato
2023-05-30 10:46:44 +10:00
committed by GitHub
co-authored by Asher Gomez
parent 675036b33c
commit 87b087e358
10 changed files with 66 additions and 46 deletions
+12 -13
View File
@@ -1,13 +1,12 @@
import { assertEquals } from "../../tests/deps.ts";
import { asset, assetSrcSet } from "./utils.ts";
globalThis.__FRSH_BUILD_ID = "ID123";
import { BUILD_ID } from "./build_id.ts";
Deno.test("asset", () => {
assertEquals(asset("/test.png"), "/test.png?__frsh_c=ID123");
assertEquals(asset("/test?f=1"), "/test?f=1&__frsh_c=ID123");
assertEquals(asset("/test#foo"), "/test?__frsh_c=ID123#foo");
assertEquals(asset("/test?f=1#foo"), "/test?f=1&__frsh_c=ID123#foo");
assertEquals(asset("/test.png"), `/test.png?__frsh_c=${BUILD_ID}`);
assertEquals(asset("/test?f=1"), `/test?f=1&__frsh_c=${BUILD_ID}`);
assertEquals(asset("/test#foo"), `/test?__frsh_c=${BUILD_ID}#foo`);
assertEquals(asset("/test?f=1#foo"), `/test?f=1&__frsh_c=${BUILD_ID}#foo`);
assertEquals(asset("./test.png"), "./test.png");
assertEquals(asset("//example.com/logo.png"), "//example.com/logo.png");
@@ -19,31 +18,31 @@ Deno.test("asset", () => {
});
Deno.test("assetSrcSet", () => {
assertEquals(assetSrcSet("/img.png"), "/img.png?__frsh_c=ID123");
assertEquals(assetSrcSet("/img.png"), `/img.png?__frsh_c=${BUILD_ID}`);
assertEquals(
assetSrcSet("/img.png, /img.png 2x"),
"/img.png?__frsh_c=ID123, /img.png?__frsh_c=ID123 2x",
`/img.png?__frsh_c=${BUILD_ID}, /img.png?__frsh_c=${BUILD_ID} 2x`,
);
assertEquals(assetSrcSet("/img.png 1x"), "/img.png?__frsh_c=ID123 1x");
assertEquals(assetSrcSet("/img.png 1x"), `/img.png?__frsh_c=${BUILD_ID} 1x`);
assertEquals(
assetSrcSet("/img.png 1x, /img.png 2x"),
"/img.png?__frsh_c=ID123 1x, /img.png?__frsh_c=ID123 2x",
`/img.png?__frsh_c=${BUILD_ID} 1x, /img.png?__frsh_c=${BUILD_ID} 2x`,
);
assertEquals(
assetSrcSet("/img.png 1.5x, /img.png 3x"),
"/img.png?__frsh_c=ID123 1.5x, /img.png?__frsh_c=ID123 3x",
`/img.png?__frsh_c=${BUILD_ID} 1.5x, /img.png?__frsh_c=${BUILD_ID} 3x`,
);
//test with queries
assertEquals(
assetSrcSet("/img.png?w=140, /img.png?w=200 2x"),
"/img.png?w=140&__frsh_c=ID123, /img.png?w=200&__frsh_c=ID123 2x",
`/img.png?w=140&__frsh_c=${BUILD_ID}, /img.png?w=200&__frsh_c=${BUILD_ID} 2x`,
);
// test with extra spaces
assertEquals(
assetSrcSet("/img-s.png 300w, /img-l.png 600w , /img-xl.png 900w"),
"/img-s.png?__frsh_c=ID123 300w, /img-l.png?__frsh_c=ID123 600w , /img-xl.png?__frsh_c=ID123 900w",
`/img-s.png?__frsh_c=${BUILD_ID} 300w, /img-l.png?__frsh_c=${BUILD_ID} 600w , /img-xl.png?__frsh_c=${BUILD_ID} 900w`,
);
// test with ( syntax