Skip to content

Commit

Permalink
Make public prefix empty, use dynamic public env
Browse files Browse the repository at this point in the history
- Refactored analytics library: factored out `send.ts` into `vitals.ts`.

Addresses <#630 (comment)>.

Thanks @jaasonw for kicking off this PR :)
  • Loading branch information
EthanThatOneKid committed Oct 26, 2022
1 parent b24bc8e commit f952e09
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
16 changes: 0 additions & 16 deletions src/lib/public/analytics/send.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/public/analytics/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { Metric } from 'web-vitals';
export type { Metric, ReportHandler, ReportCallback, ReportOpts } from 'web-vitals';

export interface AnalyticsOptions {
id?: string;
Expand Down
26 changes: 23 additions & 3 deletions src/lib/public/analytics/vitals.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import type { AnalyticsOptions, Metric } from './types';
export { onCLS, onFCP, onFID, onLCP, onTTFB } from 'web-vitals';
import { onCLS, onFCP, onFID, onLCP, onTTFB } from 'web-vitals';
import type { AnalyticsOptions, Metric, ReportCallback, ReportOpts } from './types';

export function send(
options: AnalyticsOptions,
handlers = [onFID, onTTFB, onLCP, onCLS, onFCP]
): void {
if (!options.id) return;

try {
handlers.forEach((handler) => doVital(handler, options));
} catch (err) {
console.error('[Analytics]', err);
}
}

function doVital(
handler: (onReport: ReportCallback, opts?: ReportOpts | undefined) => void,
options: AnalyticsOptions
): void {
handler((metric: Metric) => sendToAnalytics(metric, options));
}

type VitalsNavigator = Navigator & { connection: { effectiveType: string } };

export function sendToAnalytics(metric: Metric, options: AnalyticsOptions) {
function sendToAnalytics(metric: Metric, options: AnalyticsOptions) {
if (!options.url) {
options.url = 'https://vitals.vercel-analytics.com/v1/vitals';
}
Expand Down
9 changes: 5 additions & 4 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
// https://kit.svelte.dev/docs/advanced-routing#advanced-layouts
import { page } from '$app/stores';
import { browser } from '$app/environment';
import { send } from '$lib/public/analytics/send';
import { browser, dev } from '$app/environment';
import { env } from '$env/dynamic/public';
import { send } from '$lib/public/analytics/vitals';
$: if (browser && process.env.VERCEL_ANALYTICS_ID) {
$: if (browser && !dev && env.VERCEL_ANALYTICS_ID) {
send({
id: '$VERCEL_ANALYTICS_ID',
id: env.VERCEL_ANALYTICS_ID,
path: $page.url.pathname,
params: $page.params,
navigator,
Expand Down
5 changes: 2 additions & 3 deletions svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import preprocess from 'svelte-preprocess';
const config = {
// Consult https://github.com/sveltejs/svelte-preprocess
// for more information about preprocessors
preprocess: preprocess({
replace: [['$VERCEL_ANALYTICS_ID', process.env.VERCEL_ANALYTICS_ID]],
}),
preprocess: preprocess(),

kit: {
adapter: adapter(),
env: { publicPrefix: '' },
},

vitePlugin: {
Expand Down

0 comments on commit f952e09

Please sign in to comment.