Skip to content

Commit

Permalink
Improve route support (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiaslins authored Jan 25, 2024
1 parent 4c3469e commit fa990dd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 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.7",
"version": "1.0.8",
"description": "Speed Insights is a tool for measuring web performance and providing suggestions for improvement.",
"keywords": [
"speed-insights",
Expand Down
15 changes: 5 additions & 10 deletions packages/web/src/nextjs/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use client';
import { useMemo } from 'react';
import { useParams, usePathname, useSearchParams } from 'next/navigation.js';
import { computeRoute } from '../utils';

Expand All @@ -8,15 +7,11 @@ export const useRoute = (): string | null => {
const searchParams = useSearchParams();
const path = usePathname();

const finalParams = useMemo(() => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- can be null on pages router
if (!params) return null;
if (Object.keys(params).length !== 0) {
return params;
}
// For pages router, we need to use `searchParams` because `params` is an empty object
return { ...Object.fromEntries(searchParams.entries()) };
}, [params, searchParams]);
const finalParams = {
...Object.fromEntries(searchParams.entries()),
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- can be empty in pages router
...(params || {}),
};

return computeRoute(path, finalParams);
};

0 comments on commit fa990dd

Please sign in to comment.