Skip to content

Commit

Permalink
webvitals
Browse files Browse the repository at this point in the history
  • Loading branch information
icco committed Sep 14, 2024
1 parent d98df99 commit b8faa59
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Roboto, Roboto_Mono, Roboto_Slab } from "next/font/google";

import { Header, Size } from "@/components/Header";
import { ThemeProvider } from "@/components/ThemeProvider";
import { WebVitals } from "@/components/WebVitals";

const roboto = Roboto({
weight: "400",
Expand Down Expand Up @@ -53,6 +54,7 @@ export default function RootLayout({
<body>
<ThemeProvider defaultTheme="system" enableSystem>
<Header size={Size.Small} />
<WebVitals />
<main>{children}</main>
</ThemeProvider>
</body>
Expand Down
17 changes: 17 additions & 0 deletions src/components/WebVitals.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use client";

import { useReportWebVitals } from "next/web-vitals";

export function WebVitals() {
useReportWebVitals((metric) => {
const body = JSON.stringify(metric);
const url = "https://reportd.natwelch.com/analytics/natwelch";

// Use `navigator.sendBeacon()` if available, falling back to `fetch()`.
if (navigator.sendBeacon) {
navigator.sendBeacon(url, body);
} else {
fetch(url, { body, method: "POST", keepalive: true });
}
});
}

0 comments on commit b8faa59

Please sign in to comment.