Skip to content

Commit ef90f2a

Browse files
committed
refactor to make it better readable
1 parent b13dfdb commit ef90f2a

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

packages/nuxt/src/runtime/utils/route-extraction.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { logger } from '@sentry/core';
22
import type { NuxtSSRContext } from 'nuxt/app';
33
import type { NuxtPage } from 'nuxt/schema';
44

5+
export type NuxtPageSubset = { path: NuxtPage['path']; file: NuxtPage['file'] };
6+
57
/**
68
* Extracts route information from the SSR context modules and URL.
79
*
@@ -17,12 +19,12 @@ import type { NuxtPage } from 'nuxt/schema';
1719
*
1820
* @param buildTimePagesData
1921
* An array of NuxtPage objects representing the build-time pages data.
20-
* Example: [{ name: 'some-path', path: '/some/path' }, { name: 'user-userId', path: '/user/:userId()' }]
22+
* Example: [{ file: '/a/file/pages/some/path', path: '/some/path' }, { file: '/a/file/pages/user/[userId].vue', path: '/user/:userId()' }]
2123
*/
2224
export function extractParametrizedRouteFromContext(
2325
ssrContextModules?: NuxtSSRContext['modules'],
2426
currentUrl?: NuxtSSRContext['url'],
25-
buildTimePagesData: NuxtPage[] = [],
27+
buildTimePagesData: NuxtPageSubset[] = [],
2628
): null | { parametrizedRoute: string } {
2729
if (!ssrContextModules || !currentUrl) {
2830
logger.warn('SSR context modules or URL is not available.');
@@ -35,30 +37,28 @@ export function extractParametrizedRouteFromContext(
3537

3638
const modulesArray = Array.from(ssrContextModules);
3739

38-
// Find the route data that corresponds to a module in ssrContext.modules
39-
const foundRouteData = buildTimePagesData.find(routeData => {
40-
if (!routeData.file) return false;
41-
42-
return modulesArray.some(module => {
43-
// Extract the folder name and relative path from the page file
44-
// e.g., 'pages/test-param/[param].vue' -> folder: 'pages', path: 'test-param/[param].vue'
45-
const filePathParts = module.split('/');
46-
47-
// Exclude root-level files (e.g., 'app.vue')
48-
if (filePathParts.length < 2) return false;
40+
const modulePagePaths = modulesArray.map(module => {
41+
const filePathParts = module.split('/');
4942

50-
// Normalize path separators to handle both Unix and Windows paths
51-
const normalizedRouteFile = routeData.file?.replace(/\\/g, '/');
43+
// Exclude root-level files (e.g., 'app.vue')
44+
if (filePathParts.length < 2) return null;
5245

53-
const pagesFolder = filePathParts[0];
54-
const pageRelativePath = filePathParts.slice(1).join('/');
55-
56-
// Check if any module in ssrContext.modules ends with the same folder/relative path structure
57-
return normalizedRouteFile?.endsWith(`/${pagesFolder}/${pageRelativePath}`);
58-
});
46+
const pagesFolder = filePathParts[0];
47+
const pageRelativePath = filePathParts.slice(1).join('/');
48+
return `/${pagesFolder}/${pageRelativePath}`;
5949
});
6050

61-
const parametrizedRoute = foundRouteData?.path ?? null;
51+
for (const routeData of buildTimePagesData) {
52+
if (routeData.file && routeData.path) {
53+
// Handle Windows paths
54+
const normalizedFile = routeData.file.replace(/\\/g, '/');
55+
56+
// Check if any module of the requested page ends with the same folder/relative path structure as the parametrized filePath from build time.
57+
if (modulePagePaths.some(filePath => filePath && normalizedFile.endsWith(filePath))) {
58+
return { parametrizedRoute: routeData.path };
59+
}
60+
}
61+
}
6262

63-
return parametrizedRoute === null ? null : { parametrizedRoute };
63+
return null;
6464
}

0 commit comments

Comments
 (0)