Skip to content

Open source CapCut alternative.

TypeScript

fix(site): drop recovered chunk errors and bridges

David Led5c693c08724
diff --git a/site/src/instrumentation-client.test.ts b/site/src/instrumentation-client.test.ts
index 0149ebf6..a560b98e 100644
--- a/site/src/instrumentation-client.test.ts
+++ b/site/src/instrumentation-client.test.ts
@@ -7,6 +7,9 @@ type Event = { event: string; properties: { $exception_list: unknown[] } };
type BeforeSend = (event: Event) => Event | null;
let beforeSend: BeforeSend;
+// What the client listens for on the window, so a case can fire an event.
+const listeners = new Map<string, (e: unknown) => void>();
+const session = new Map<string, string>();
const saved = {
env: { ...process.env },
window: (globalThis as { window?: unknown }).window,
@@ -20,7 +23,13 @@ beforeAll(async () => {
default: { init: (_key: string, config: { before_send: BeforeSend }) => (beforeSend = config.before_send) },
} as never);
Object.assign(process.env, { NEXT_PUBLIC_POSTHOG_KEY: "test", NODE_ENV: "production" });
- Object.assign(globalThis, { window: { location: { pathname: "/" }, addEventListener: () => {} } });
+ Object.assign(globalThis, {
+ window: {
+ location: { pathname: "/", reload: () => {} },
+ addEventListener: (type: string, fn: (e: unknown) => void) => listeners.set(type, fn),
+ sessionStorage: { getItem: (k: string) => session.get(k) ?? null, setItem: (k: string, v: string) => session.set(k, v) },
+ },
+ });
await import("./instrumentation-client");
});
@@ -61,6 +70,35 @@ describe("before_send", () => {
expect(beforeSend(exception(wallet))).toBeNull();
});
+ test("drops an in-app browser's bridge failing", () => {
+ // Instagram's web view on Android runs its bridge from its own scheme.
+ const bridge = {
+ type: "Error",
+ value: "Error invoking postMessage: Java object is gone",
+ stacktrace: {
+ frames: [
+ { filename: "iabjs://navigation_performance_logger_android", function: "sendDataToNative" },
+ { filename: "iabjs://navigation_performance_logger_android", function: "sendBeforeUnloadMessage" },
+ ],
+ },
+ };
+
+ expect(beforeSend(exception(bridge))).toBeNull();
+ });
+
+ test("drops a failed chunk load once a reload recovers it", () => {
+ const chunk = {
+ type: "ChunkLoadError",
+ value: "Failed to load chunk /_next/static/chunks/a.js from module 1",
+ stacktrace: { frames: [{ filename: "https://donkeycut.com/_next/static/chunks/turbopack-a.js" }] },
+ };
+ expect(beforeSend(exception(chunk))).not.toBeNull();
+
+ listeners.get("unhandledrejection")?.({ reason: Object.assign(new Error(chunk.value), { name: "ChunkLoadError" }) });
+
+ expect(beforeSend(exception(chunk))).toBeNull();
+ });
+
test("drops a hydration error on a page the browser translated", () => {
// Chrome's translator tags <html> and relabels its language.
Object.assign(globalThis, { document: translatedDocument("pt", ["translated-ltr"]) });
diff --git a/site/src/instrumentation-client.ts b/site/src/instrumentation-client.ts
index b24bfdfb..ca9c67f9 100644
--- a/site/src/instrumentation-client.ts
+++ b/site/src/instrumentation-client.ts
@@ -18,6 +18,10 @@ const replayFree = () => REPLAY_FREE.test(window.location.pathname);
// embed one get it masked before the event leaves the browser.
const scrubUrls = (s: string) => s.replace(/https?:\/\/\S+/g, "<url>");
+// Where a script this page runs can come from: its own origin, posthog-js's
+// asset host, or a blob it made.
+const WEB_ADDRESS = /^(https?:|blob:|\/)/;
+
/**
* Whether this exception was thrown by script the page never served.
*
@@ -25,13 +29,14 @@ const scrubUrls = (s: string) => s.replace(/https?:\/\/\S+/g, "<url>");
* its own bridge into every page it opens, and that bridge throws when its
* host tears the native side down mid-page ("Java object is gone"). The frames
* come from no file, because there is no file: the code was evaluated into the
- * document. Anything this page shipped, minified or not, has a filename on
- * every frame, and an exception carrying no stack at all (a DOMException, say)
- * is ours to keep — so the test is a stack that exists and names nothing.
+ * document, or from the web view's own scheme ("iabjs://…"). Anything this
+ * page shipped, minified or not, names a web address on every frame, and an
+ * exception carrying no stack at all (a DOMException, say) is ours to keep —
+ * so the test is a stack that exists and names no web address.
*/
const fromInjectedScript = (item: { stacktrace?: { frames?: { filename?: string }[] } }) => {
const frames = item?.stacktrace?.frames;
- return Array.isArray(frames) && frames.length > 0 && frames.every((f) => !f?.filename);
+ return Array.isArray(frames) && frames.length > 0 && frames.every((f) => !WEB_ADDRESS.test(f?.filename ?? ""));
};
/**
@@ -146,9 +151,14 @@ const evaluatedSyntaxError = (item: { type?: string; value?: string; stacktrace?
item?.type === "SyntaxError" && !item.stacktrace?.frames?.length && !/JSON/.test(item.value ?? "");
// A tab left open across a deploy recovers from the old build's missing chunks.
-if (process.env.NODE_ENV === "production") {
- reloadOnChunkError(window);
-}
+const chunkReloading = process.env.NODE_ENV === "production" ? reloadOnChunkError(window) : () => false;
+
+// The error name the bundler's runtime gives a lazy chunk that failed to load.
+const CHUNK_LOAD_ERROR = "ChunkLoadError";
+
+/** Whether this exception is a failed chunk load the page is reloading for.
+ * A chunk that fails again after that reload is reported. */
+const recoveredChunk = (item: { type?: string }) => item?.type === CHUNK_LOAD_ERROR && chunkReloading();
// Production builds only: a dev server on localhost sends HMR build overlays
// and half-saved-file errors straight into production error tracking.
@@ -184,6 +194,7 @@ if (process.env.NEXT_PUBLIC_POSTHOG_KEY && process.env.NODE_ENV === "production"
translatorArtifact(list[0]) ||
droppedFetch(list[0]) ||
cancellation(list[0]) ||
+ recoveredChunk(list[0]) ||
evaluatedSyntaxError(list[0])
) {
return null;
diff --git a/site/src/lib/chunkReload.test.ts b/site/src/lib/chunkReload.test.ts
index e56df099..bd2f6e41 100644
--- a/site/src/lib/chunkReload.test.ts
+++ b/site/src/lib/chunkReload.test.ts
@@ -57,6 +57,16 @@ describe("reloadOnChunkError", () => {
expect(win.reloads).toBe(1);
});
+ test("says when a reload is on its way", () => {
+ const win = fakeWindow();
+ const reloading = reloadOnChunkError(win, () => win.now);
+ expect(reloading()).toBe(false);
+
+ win.fire("unhandledrejection", { reason: chunkError() });
+
+ expect(reloading()).toBe(true);
+ });
+
test("leaves every other error alone", () => {
const win = fakeWindow();
reloadOnChunkError(win, () => win.now);
diff --git a/site/src/lib/chunkReload.ts b/site/src/lib/chunkReload.ts
index 2fa0c1b6..cd31b28d 100644
--- a/site/src/lib/chunkReload.ts
+++ b/site/src/lib/chunkReload.ts
@@ -20,9 +20,11 @@ interface ReloadWindow {
* A tab open across a deploy still asks for the old build's chunks, and once
* those are gone the import rejects and the tree it was loading dies. A
* reload fetches the current build, at most once per window; a tab whose
- * storage throws keeps the failure.
+ * storage throws keeps the failure. Returns whether a reload is on its way,
+ * so error tracking can leave out the failures it recovers.
*/
-export function reloadOnChunkError(win: ReloadWindow, now: () => number = Date.now): void {
+export function reloadOnChunkError(win: ReloadWindow, now: () => number = Date.now): () => boolean {
+ let reloading = false;
const recover = (error: unknown) => {
if ((error as { name?: string } | null)?.name !== CHUNK_LOAD_ERROR) {
return;
@@ -40,9 +42,11 @@ export function reloadOnChunkError(win: ReloadWindow, now: () => number = Date.n
return;
}
+ reloading = true;
win.location.reload();
};
win.addEventListener("error", (e) => recover(e.error));
win.addEventListener("unhandledrejection", (e) => recover(e.reason));
+ return () => reloading;
}