@@ -2,6 +2,8 @@ import { logger } from '@sentry/core';
2
2
import type { NuxtSSRContext } from 'nuxt/app' ;
3
3
import type { NuxtPage } from 'nuxt/schema' ;
4
4
5
+ export type NuxtPageSubset = { path : NuxtPage [ 'path' ] ; file : NuxtPage [ 'file' ] } ;
6
+
5
7
/**
6
8
* Extracts route information from the SSR context modules and URL.
7
9
*
@@ -17,12 +19,12 @@ import type { NuxtPage } from 'nuxt/schema';
17
19
*
18
20
* @param buildTimePagesData
19
21
* 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()' }]
21
23
*/
22
24
export function extractParametrizedRouteFromContext (
23
25
ssrContextModules ?: NuxtSSRContext [ 'modules' ] ,
24
26
currentUrl ?: NuxtSSRContext [ 'url' ] ,
25
- buildTimePagesData : NuxtPage [ ] = [ ] ,
27
+ buildTimePagesData : NuxtPageSubset [ ] = [ ] ,
26
28
) : null | { parametrizedRoute : string } {
27
29
if ( ! ssrContextModules || ! currentUrl ) {
28
30
logger . warn ( 'SSR context modules or URL is not available.' ) ;
@@ -35,30 +37,28 @@ export function extractParametrizedRouteFromContext(
35
37
36
38
const modulesArray = Array . from ( ssrContextModules ) ;
37
39
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 ( '/' ) ;
49
42
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 ;
52
45
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 } ` ;
59
49
} ) ;
60
50
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
+ }
62
62
63
- return parametrizedRoute === null ? null : { parametrizedRoute } ;
63
+ return null ;
64
64
}
0 commit comments