From 1b32564e5dc9744e4f6b219626eaae0ebeab9891 Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Tue, 24 Nov 2026 08:44:27 +1100 Subject: [PATCH] perf: render the analytics beacon on the server Keep the same Cloudflare beacon and direct browser reporting while removing its registration code from the production client bundle. --- app/cloudflare-analytics.server.ts | 12 ++++++++++++ nuxt.config.ts | 16 ++++++++++------ shared/rum-beacon.ts | 2 ++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 app/cloudflare-analytics.server.ts diff --git a/app/cloudflare-analytics.server.ts b/app/cloudflare-analytics.server.ts new file mode 100644 index 00000000..5b3adec6 --- /dev/null +++ b/app/cloudflare-analytics.server.ts @@ -0,0 +1,12 @@ +import { CLOUDFLARE_WEB_ANALYTICS_TOKEN } from '#shared/rum-beacon' + +export default defineNuxtPlugin({ + env: { islands: false }, + setup() { + useScript({ + 'src': 'https://static.cloudflareinsights.com/beacon.min.js', + 'data-cf-beacon': JSON.stringify({ token: CLOUDFLARE_WEB_ANALYTICS_TOKEN, spa: true }), + 'crossorigin': false, + }, { bundle: true, trigger: 'server' }) + }, +}) diff --git a/nuxt.config.ts b/nuxt.config.ts index b6b8dafe..9b3f7c83 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -9,6 +9,7 @@ import { withBuildAssetMissFallthrough } from './scripts/lib/static-asset-fallth import { externalCheckin } from './shared/checkin-external.ts' import { iconifyCollections } from './shared/icon-collections.ts' import { REGISTRY_SKILL } from './shared/llms-txt.ts' +import { CLOUDFLARE_WEB_ANALYTICS_TOKEN } from './shared/rum-beacon.ts' import { SENTRY_DSN, sentryRelease, sentryReportingEnabled } from './shared/sentry.ts' import { SESSION_NAME } from './shared/server/session-access.ts' import { CLI_INSTALL_SCRIPTS } from './shared/skill-commands.ts' @@ -249,6 +250,9 @@ export default defineNuxtConfig({ // first rule. Skill, repo and category pages follow once it holds. }, + // Keep the beacon in the production HTML and its registration out of the client bundle. + plugins: process.env.NODE_ENV === 'production' ? ['~/cloudflare-analytics.server.ts'] : [], + scripts: { // Bundling a registry script downloads it at build time. When // static.cloudflareinsights.com is unreachable, that download threw and @@ -263,17 +267,17 @@ export default defineNuxtConfig({ // static.cloudflareinsights.com at transform time, so every vitest file // fails on the network-less CI runner (#204). Telemetry serves no test, // and vitest sets NODE_ENV=test before nuxt.config.ts is evaluated. - ...(process.env.NODE_ENV === 'test' - ? {} - : { + ...(process.env.NODE_ENV === 'development' + ? { cloudflareWebAnalytics: { - token: 'fefd4b7eafe04d5f81621e43e5d5ef80', + token: CLOUDFLARE_WEB_ANALYTICS_TOKEN, // RUM cannot collect Worker subrequests. Send events from the browser. // https://developers.cloudflare.com/web-analytics/faq/#can-i-use-real-user-monitoring-rum-with-cloudflare-workers proxy: false, - trigger: 'server', + trigger: 'client', }, - }), + } + : {}), }, }, diff --git a/shared/rum-beacon.ts b/shared/rum-beacon.ts index 1380b3dd..66d8cc3c 100644 --- a/shared/rum-beacon.ts +++ b/shared/rum-beacon.ts @@ -1,3 +1,5 @@ +export const CLOUDFLARE_WEB_ANALYTICS_TOKEN = 'fefd4b7eafe04d5f81621e43e5d5ef80' + /** * Timing entries carry their page or resource URL in `name`, so it scrubs * like any other URL key at every nesting depth.