-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add client side of web vitals * feat: write web vitals to DynamoDB table
- Loading branch information
Showing
9 changed files
with
1,127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useEffect } from "react" | ||
import type { Metric } from "web-vitals" | ||
|
||
export interface AnalyticsData { | ||
page: string | ||
href: string | ||
metric: Metric | ||
} | ||
|
||
async function sendToAnalytics(metric: Metric) { | ||
const data: AnalyticsData = { | ||
page: window.location.pathname, | ||
href: window.location.href, | ||
metric | ||
} | ||
const body = JSON.stringify(data) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (navigator.sendBeacon) { | ||
navigator.sendBeacon("/analytics", body) | ||
} else { | ||
await fetch("/analytics", { body, method: "POST", keepalive: true }) | ||
} | ||
} | ||
|
||
export function useWebVitals() { | ||
useEffect(() => { | ||
async function handleWebVitals() { | ||
const { | ||
onCLS: onCls, | ||
onINP: onInp, | ||
onLCP: onLcp, | ||
onFCP: onFcp, | ||
onTTFB: onTtfb | ||
} = await import("web-vitals") | ||
|
||
onCls(sendToAnalytics) | ||
onInp(sendToAnalytics) | ||
onLcp(sendToAnalytics) | ||
onFcp(sendToAnalytics) | ||
onTtfb(sendToAnalytics) | ||
} | ||
|
||
const idleRunner = () => { | ||
handleWebVitals().catch((error: unknown) => { | ||
console.error("Failed to load web-vitals", error) | ||
}) | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (window.requestIdleCallback) { | ||
window.requestIdleCallback(idleRunner) | ||
} else { | ||
setTimeout(idleRunner, 1) | ||
} | ||
}, []) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import awsLite from "@aws-lite/client" | ||
import awsDynamoDB from "@aws-lite/dynamodb" | ||
import type { ActionFunctionArgs } from "@remix-run/node" | ||
import { Resource } from "sst" | ||
|
||
import type { AnalyticsData } from "~/components/hooks/webVitals" | ||
|
||
interface DatabaseAnalyticsData extends AnalyticsData { | ||
id: string | ||
createdAt: string | ||
} | ||
|
||
export async function action({ request }: ActionFunctionArgs) { | ||
const aws = await awsLite({ | ||
region: "eu-central-1", | ||
plugins: [awsDynamoDB] | ||
}) | ||
|
||
const data = (await request.json()) as AnalyticsData | ||
|
||
const entry: DatabaseAnalyticsData = { | ||
...data, | ||
id: data.metric.id, | ||
createdAt: new Date().toISOString() | ||
} | ||
|
||
try { | ||
// eslint-disable-next-line new-cap | ||
await aws.DynamoDB.PutItem({ | ||
TableName: Resource.WebVitals.name, | ||
Item: entry | ||
}) | ||
} catch (error) { | ||
return new Response(`Error saving data: ${error as string}`, { | ||
status: 500 | ||
}) | ||
} | ||
|
||
return new Response("", { status: 204 }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module "@aws-lite/dynamodb" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.