diff --git a/.gitignore b/.gitignore index ce5cb8e..7e0652c 100644 --- a/.gitignore +++ b/.gitignore @@ -24,9 +24,11 @@ .DS_Store *.pem *.sqlite - -# debug -npm-debug.log* +# external experiments +nova-calendar-main/ + +# debug +npm-debug.log* yarn-debug.log* yarn-error.log* .pnpm-debug.log* diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 903a16c..336b058 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,3 +1,4 @@ +import "@/lib/performance-measure-polyfill" import type { Metadata } from "next" import "./globals.css" import { ConvexClientProvider } from "./ConvexClientProvider" diff --git a/src/lib/performance-measure-polyfill.ts b/src/lib/performance-measure-polyfill.ts new file mode 100644 index 0000000..311ff58 --- /dev/null +++ b/src/lib/performance-measure-polyfill.ts @@ -0,0 +1,41 @@ +export {} + +declare global { + var __performanceMeasurePatched: boolean | undefined +} + +function isNegativeTimestampError(error: unknown): boolean { + if (!(error instanceof Error)) return false + return error.message.toLowerCase().includes("cannot have a negative time stamp") +} + +if (typeof performance !== "undefined" && typeof performance.measure === "function") { + if (!globalThis.__performanceMeasurePatched) { + const originalMeasure = performance.measure.bind(performance) + performance.measure = ((name: string, startOrOptions?: string | PerformanceMeasureOptions, endMark?: string) => { + try { + return originalMeasure(name, startOrOptions as string | PerformanceMeasureOptions, endMark) + } catch (error) { + if (!isNegativeTimestampError(error)) { + throw error + } + + // Next.js occasionally triggers an invalid interval when profiling slow routes. + // Clamp the measurement to start at 0 to avoid crashing the page. + if (typeof startOrOptions === "object" && startOrOptions) { + const safeOptions: PerformanceMeasureOptions = { + ...startOrOptions, + start: 0, + } + try { + return originalMeasure(name, safeOptions, endMark) + } catch { + return undefined + } + } + return undefined + } + }) as typeof performance.measure + globalThis.__performanceMeasurePatched = true + } +}