Skip to content

Commit

Permalink
fix: utm parameters takes precedence over route params (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy authored Nov 6, 2024
1 parent 926b277 commit 1d1845e
Show file tree
Hide file tree
Showing 11 changed files with 686 additions and 511 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18.17
18.19
7 changes: 4 additions & 3 deletions apps/nextjs/app/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export default function Blog({
params: { slug },
export default async function Blog({
params,
}: {
params: { slug: string };
params: Promise<{ slug: string }>;
}) {
const { slug } = await params;
return <div>My blog page {slug}</div>;
}
6 changes: 1 addition & 5 deletions apps/nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverActions: true,
},
};
const nextConfig = {};

module.exports = nextConfig;
6 changes: 3 additions & 3 deletions apps/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
},
"dependencies": {
"@vercel/speed-insights": "workspace:*",
"next": "^14.0.4",
"react": "18.2.0",
"react-dom": "18.2.0"
"next": "15.0.1",
"react": "19.0.0-rc-69d4b800-20241021",
"react-dom": "19.0.0-rc-69d4b800-20241021"
},
"devDependencies": {
"@playwright/test": "1.37.1"
Expand Down
3 changes: 2 additions & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vercel/speed-insights",
"version": "1.0.14",
"version": "1.1.0",
"description": "Speed Insights is a tool for measuring web performance and providing suggestions for improvement.",
"keywords": [
"speed-insights",
Expand Down Expand Up @@ -89,6 +89,7 @@
"type-check": "tsc --noEmit"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@remix-run/react": "^2.5.0",
"@sveltejs/kit": "^2.7",
"@swc/core": "^1.3.103",
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/generic.server.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @jest-environment node */
import { describe, it, expect } from '@jest/globals';
import { injectSpeedInsights } from './generic';

describe('injectSpeedInsights()', () => {
Expand Down
17 changes: 8 additions & 9 deletions packages/web/src/generic.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { describe, it, expect } from '@jest/globals';
import { injectSpeedInsights } from './generic';

describe('injectSpeedInsights()', () => {
it('allows no parameters', () => {
expect(injectSpeedInsights()).toEqual({
setRoute: expect.any(Function) as () => void,
setRoute: expect.any(Function),
});
expectInjectedScript();
});

it('can set framework', () => {
const framework = 'sveltekit';
expect(injectSpeedInsights({ framework })).toEqual({
setRoute: expect.any(Function) as () => void,
setRoute: expect.any(Function),
});
expectInjectedScript({ framework });
});
Expand All @@ -21,13 +22,11 @@ function expectInjectedScript({
src = 'https://va.vercel-scripts.com/v1/speed-insights/script.debug.js',
framework = '',
} = {}): void {
const candidateScript = document.querySelector('script');
expect(candidateScript).toBeDefined();
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- the previous assertion fails otherwise */
const script = candidateScript!;
expect(script.defer).toBe(true);
expect(script.src).toBe(src);
expect(script.dataset.sdkn).toBe(
const script = document.querySelector('script');
expect(script).toBeDefined();
expect(script?.defer).toBe(true);
expect(script?.src).toBe(src);
expect(script?.dataset.sdkn).toBe(
`@vercel/speed-insights${framework ? `/${framework}` : ''}`,
);
}
4 changes: 2 additions & 2 deletions packages/web/src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { initQueue } from './queue';
import type { SpeedInsightsProps } from './types';
import { isBrowser, isDevelopment, computeRoute } from './utils';

const SCRIPT_URL = `https://va.vercel-scripts.com/v1/speed-insights`;
const SCRIPT_URL = 'https://va.vercel-scripts.com/v1/speed-insights';
const PROD_SCRIPT_URL = `${SCRIPT_URL}/script.js`;
const DEV_SCRIPT_URL = `${SCRIPT_URL}/script.debug.js`;
const PROXY_SCRIPT_URL = `/_vercel/speed-insights/script.js`;
const PROXY_SCRIPT_URL = '/_vercel/speed-insights/script.js';

/**
* Injects the Vercel Speed Insights script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/speed-insights).
Expand Down
16 changes: 9 additions & 7 deletions packages/web/src/nextjs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ export const useRoute = (): string | null => {
const params = useParams();
const searchParams = useSearchParams() || new URLSearchParams();
const path = usePathname();

const finalParams = {
...Object.fromEntries(searchParams.entries()),
...(params || {}),
};

return params ? computeRoute(path, finalParams) : null;
// Until we have route parameters, we don't compute the route
if (!params) {
return null;
}
// in Next.js@13, useParams() could return an empty object for pages router, and we default to searchParams.
const finalParams = Object.keys(params).length
? params
: Object.fromEntries(searchParams.entries());
return computeRoute(path, finalParams);
};
1 change: 1 addition & 0 deletions packages/web/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from '@jest/globals';
import { computeRoute } from './utils';

describe('utils', () => {
Expand Down
Loading

0 comments on commit 1d1845e

Please sign in to comment.