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
+10
View File
@@ -0,0 +1,10 @@
import { toHashString } from "./deps.ts";
const deploymentId = Deno.env.get("DENO_DEPLOYMENT_ID") ||
crypto.randomUUID();
const buildIdHash = await crypto.subtle.digest(
"SHA-1",
new TextEncoder().encode(deploymentId),
);
export const BUILD_ID = toHashString(buildIdHash, "hex");
+28 -4
View File
@@ -1,7 +1,14 @@
import { BuildOptions } from "https://deno.land/x/esbuild@v0.17.11/mod.js";
import { BUILD_ID } from "./constants.ts";
import { denoPlugin, esbuild, toFileUrl } from "./deps.ts";
import {
denoPlugin,
esbuild,
esbuildTypes,
escape,
fromFileUrl,
toFileUrl,
} from "./deps.ts";
import { Island, Plugin } from "./types.ts";
import { BUILD_ID } from "./build_id.ts";
export interface JSXConfig {
jsx: "react" | "react-jsx";
@@ -87,7 +94,6 @@ export class Bundler {
: { minify: true };
const bundle = await esbuild.build({
bundle: true,
define: { __FRSH_BUILD_ID: `"${BUILD_ID}"` },
entryPoints,
format: "esm",
metafile: true,
@@ -98,7 +104,10 @@ export class Bundler {
absWorkingDir,
outfile: "",
platform: "neutral",
plugins: [denoPlugin({ importMapURL: this.#importMapURL })],
plugins: [
buildIdPlugin(BUILD_ID),
denoPlugin({ importMapURL: this.#importMapURL }),
],
sourcemap: this.#dev ? "linked" : false,
splitting: true,
target: ["chrome99", "firefox99", "safari15"],
@@ -149,3 +158,18 @@ export class Bundler {
// return this.#preloads.get(path) ?? [];
// }
}
function buildIdPlugin(buildId: string): esbuildTypes.Plugin {
const file = import.meta.resolve("../runtime/build_id.ts");
const url = new URL(file);
const path = fromFileUrl(url);
return {
name: "fresh-build-id",
setup(build) {
build.onLoad(
{ filter: new RegExp(escape(path)), namespace: "file" },
() => ({ contents: `export const BUILD_ID = "${buildId}";` }),
);
},
};
}
+1 -20
View File
@@ -1,30 +1,11 @@
import { INTERNAL_PREFIX } from "../runtime/utils.ts";
import { toHashString } from "./deps.ts";
import { BUILD_ID } from "./build_id.ts";
export const REFRESH_JS_URL = `${INTERNAL_PREFIX}/refresh.js`;
export const ALIVE_URL = `${INTERNAL_PREFIX}/alive`;
export const JS_PREFIX = `/js`;
export const DEBUG = !Deno.env.get("DENO_DEPLOYMENT_ID");
const deploymentId = Deno.env.get("DENO_DEPLOYMENT_ID") ||
crypto.randomUUID();
const buildIdHash = await crypto.subtle.digest(
"SHA-1",
new TextEncoder().encode(deploymentId),
);
export const BUILD_ID = toHashString(buildIdHash, "hex");
export function bundleAssetUrl(path: string) {
return `${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}${path}`;
}
globalThis.__FRSH_BUILD_ID = BUILD_ID;
declare global {
interface Crypto {
randomUUID(): string;
}
// deno-lint-ignore no-var
var __FRSH_BUILD_ID: string;
}
+2 -1
View File
@@ -12,7 +12,8 @@ import { h } from "preact";
import * as router from "./router.ts";
import { Manifest } from "./mod.ts";
import { Bundler, JSXConfig } from "./bundle.ts";
import { ALIVE_URL, BUILD_ID, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts";
import { ALIVE_URL, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts";
import { BUILD_ID } from "./build_id.ts";
import DefaultErrorHandler from "./default_error_page.ts";
import {
AppModule,
+1
View File
@@ -16,6 +16,7 @@ export {
typeByExtension,
} from "https://deno.land/std@0.189.0/media_types/mod.ts";
export { toHashString } from "https://deno.land/std@0.189.0/crypto/to_hash_string.ts";
export { escape } from "https://deno.land/std@0.189.0/regexp/escape.ts";
// -- esbuild --
// @deno-types="https://deno.land/x/esbuild@v0.17.11/mod.d.ts"