From cdfd10282d9207c8610a0c8d13b2c4c85b1cfbff Mon Sep 17 00:00:00 2001 From: Johann Derdak Date: Tue, 7 May 2024 17:08:06 +0200 Subject: [PATCH] wip: optionally load fathom when env vars are set --- clients/web/package.json | 1 + clients/web/pnpm-lock.yaml | 11 +++++++++++ clients/web/src/lib/load-fathom.ts | 8 ++++++++ clients/web/src/render-client.tsx | 2 ++ clients/web/tsconfig.json | 2 +- 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 clients/web/src/lib/load-fathom.ts diff --git a/clients/web/package.json b/clients/web/package.json index da10dfb3..793e47de 100644 --- a/clients/web/package.json +++ b/clients/web/package.json @@ -60,6 +60,7 @@ "@sentry/browser": "^7.101.0", "@sentry/vite-plugin": "^2.14.0", "@solid-primitives/map": "^0.4.9", + "@solid-primitives/script-loader": "^2.1.2", "@solidjs/router": "^0.12.0", "@tanstack/solid-virtual": "^3.0.4", "clsx": "^2.1.0", diff --git a/clients/web/pnpm-lock.yaml b/clients/web/pnpm-lock.yaml index 5b4afd82..48c96929 100644 --- a/clients/web/pnpm-lock.yaml +++ b/clients/web/pnpm-lock.yaml @@ -35,6 +35,9 @@ dependencies: '@solid-primitives/map': specifier: ^0.4.9 version: 0.4.9(solid-js@1.8.14) + '@solid-primitives/script-loader': + specifier: ^2.1.2 + version: 2.1.2(solid-js@1.8.14) '@solidjs/router': specifier: ^0.12.0 version: 0.12.0(solid-js@1.8.14) @@ -1329,6 +1332,14 @@ packages: solid-js: 1.8.14 dev: false + /@solid-primitives/script-loader@2.1.2(solid-js@1.8.14): + resolution: {integrity: sha512-dIeJ3FWfBAlYZzQ0XLpv1ukHcTxPYphCURS01Hri18ykb28eCOdYtgn3OVNgYzAvm9y8rW5IoyIMY+JzYD6Fhw==} + peerDependencies: + solid-js: ^1.6.12 + dependencies: + solid-js: 1.8.14 + dev: false + /@solid-primitives/static-store@0.0.5(solid-js@1.8.14): resolution: {integrity: sha512-ssQ+s/wrlFAEE4Zw8GV499yBfvWx7SMm+ZVc11wvao4T5xg9VfXCL9Oa+x4h+vPMvSV/Knv5LrsLiUa+wlJUXQ==} peerDependencies: diff --git a/clients/web/src/lib/load-fathom.ts b/clients/web/src/lib/load-fathom.ts new file mode 100644 index 00000000..1a71b437 --- /dev/null +++ b/clients/web/src/lib/load-fathom.ts @@ -0,0 +1,8 @@ +import { createScriptLoader } from "@solid-primitives/script-loader"; + +export function loadFathom() { + const fathomUrl = import.meta.env.VITE_FATHOM_URL; + const fathomId = import.meta.env.VITE_FATHOM_ID; + if (fathomUrl && fathomId) + createScriptLoader({ src: fathomUrl, "data-site": fathomId, defer: true }); +} diff --git a/clients/web/src/render-client.tsx b/clients/web/src/render-client.tsx index 501d2937..b51060c5 100644 --- a/clients/web/src/render-client.tsx +++ b/clients/web/src/render-client.tsx @@ -3,6 +3,7 @@ import "~/css/global.css"; import Entry from "./entry"; import * as Sentry from "@sentry/browser"; import { DEV } from "solid-js"; +import { loadFathom } from "./lib/load-fathom"; if (!DEV) { Sentry.init({ @@ -11,6 +12,7 @@ if (!DEV) { integrations: (def) => [...def, Sentry.browserTracingIntegration()], tracesSampleRate: 0.1, }); + loadFathom(); } const app = document.getElementById("app"); diff --git a/clients/web/tsconfig.json b/clients/web/tsconfig.json index cbc13ed5..f48737ac 100644 --- a/clients/web/tsconfig.json +++ b/clients/web/tsconfig.json @@ -26,7 +26,7 @@ "jsx": "preserve", "jsxImportSource": "solid-js", - "types": ["vitest/globals"] + "types": ["vitest/globals", "vite/client"] }, "include": ["src", "tests"] }