feat: add commithash to error screen

This commit is contained in:
Percs 2024-10-11 17:35:06 -05:00
parent 0da63dc296
commit 9ea9863ced
2 changed files with 24 additions and 14 deletions

View file

@ -3,6 +3,7 @@ import { rspack } from "@rspack/core";
import { RsdoctorRspackPlugin } from "@rsdoctor/rspack-plugin"; import { RsdoctorRspackPlugin } from "@rsdoctor/rspack-plugin";
import { readFile } from "node:fs/promises"; import { readFile } from "node:fs/promises";
import { execSync } from "node:child_process";
import { join } from "path"; import { join } from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
const __dirname = fileURLToPath(new URL(".", import.meta.url)); const __dirname = fileURLToPath(new URL(".", import.meta.url));
@ -66,6 +67,14 @@ export default defineConfig({
new rspack.DefinePlugin({ new rspack.DefinePlugin({
VERSION: JSON.stringify(packagemeta.version), VERSION: JSON.stringify(packagemeta.version),
}), }),
new rspack.DefinePlugin({
COMMITHASH: JSON.stringify(
execSync("git rev-parse HEAD", { encoding: "utf-8" }).replace(
/\r?\n|\r/g,
""
)
),
}),
process.env.DEBUG === "true" process.env.DEBUG === "true"
? new RsdoctorRspackPlugin({ ? new RsdoctorRspackPlugin({
supports: { supports: {

View file

@ -1,14 +1,15 @@
export function errorTemplate(trace: string, fetchedURL: string) { export function errorTemplate(trace: string, fetchedURL: string) {
// turn script into a data URI so we don"t have to escape any HTML values // turn script into a data URI so we don"t have to escape any HTML values
const script = ` const script = `
errorTrace.value = ${JSON.stringify(trace)}; errorTrace.value = ${JSON.stringify(trace)};
fetchedURL.textContent = ${JSON.stringify(fetchedURL)}; fetchedURL.textContent = ${JSON.stringify(fetchedURL)};
for (const node of document.querySelectorAll("#hostname")) node.textContent = ${JSON.stringify(location.hostname)}; for (const node of document.querySelectorAll("#hostname")) node.textContent = ${JSON.stringify(location.hostname)};
reload.addEventListener("click", () => location.reload()); reload.addEventListener("click", () => location.reload());
version.textContent = ${JSON.stringify(VERSION)}; version.textContent = ${JSON.stringify(VERSION)};
build.textContent = ${JSON.stringify(COMMITHASH)};
`; `;
return `<!DOCTYPE html> return `<!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
@ -155,7 +156,7 @@ export function errorTemplate(trace: string, fetchedURL: string) {
<br> <br>
<button id="reload" class="primary">Reload</button> <button id="reload" class="primary">Reload</button>
</div> </div>
<p id="version-wrapper"><i>Scramjet v<span id="version"></span></i></p> <p id="version-wrapper"><i>Scramjet v<span id="version"></span> (build <span id="build"></span>)</i></p>
<script> <script>
CSS.paintWorklet.addModule(\`data:application/javascript;charset=utf8,${encodeURIComponent(` CSS.paintWorklet.addModule(\`data:application/javascript;charset=utf8,${encodeURIComponent(`
const HTML_COLOR_SCALE = [ const HTML_COLOR_SCALE = [
@ -229,15 +230,15 @@ export function errorTemplate(trace: string, fetchedURL: string) {
} }
export function renderError(err: unknown, fetchedURL: string) { export function renderError(err: unknown, fetchedURL: string) {
const headers = { const headers = {
"content-type": "text/html", "content-type": "text/html",
}; };
if (crossOriginIsolated) { if (crossOriginIsolated) {
headers["Cross-Origin-Embedder-Policy"] = "require-corp"; headers["Cross-Origin-Embedder-Policy"] = "require-corp";
} }
return new Response(errorTemplate(String(err), fetchedURL), { return new Response(errorTemplate(String(err), fetchedURL), {
status: 500, status: 500,
headers: headers, headers: headers,
}); });
} }