-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(nuxt): Use virtual module for Nuxt pages data (SSR route parametrization) #20020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+85
−23
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/nuxt/src/runtime/plugins/route-detector-legacy.server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { debug, getActiveSpan, getRootSpan, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; | ||
| import { defineNuxtPlugin } from 'nuxt/app'; | ||
| import type { NuxtPageSubset } from '../utils/route-extraction'; | ||
| import { extractParametrizedRouteFromContext } from '../utils/route-extraction'; | ||
|
|
||
| export default defineNuxtPlugin(nuxtApp => { | ||
| nuxtApp.hooks.hook('app:rendered', async renderContext => { | ||
| let buildTimePagesData: NuxtPageSubset[]; | ||
| try { | ||
| // This is a common Nuxt pattern to import build-time generated data (until Nuxt v3): https://nuxt.com/docs/4.x/api/kit/templates#creating-a-virtual-file-for-runtime-plugin | ||
| // @ts-expect-error This import is dynamically resolved at build time (`addTemplate` in module.ts) | ||
| const { default: importedPagesData } = await import('#build/sentry--nuxt-pages-data.mjs'); | ||
| buildTimePagesData = importedPagesData || []; | ||
| debug.log('Imported build-time pages data:', buildTimePagesData); | ||
| } catch (error) { | ||
| buildTimePagesData = []; | ||
| debug.warn('Failed to import build-time pages data:', error); | ||
| } | ||
|
|
||
| const ssrContext = renderContext.ssrContext; | ||
|
|
||
| const routeInfo = extractParametrizedRouteFromContext( | ||
| ssrContext?.modules, | ||
| ssrContext?.url || ssrContext?.event._path, | ||
| buildTimePagesData, | ||
| ); | ||
|
|
||
| if (routeInfo === null) { | ||
| return; | ||
| } | ||
|
|
||
| const activeSpan = getActiveSpan(); // In development mode, getActiveSpan() is always undefined | ||
|
|
||
| if (activeSpan && routeInfo.parametrizedRoute) { | ||
| const rootSpan = getRootSpan(activeSpan); | ||
|
|
||
| if (!rootSpan) { | ||
| return; | ||
| } | ||
|
|
||
| debug.log('Matched parametrized server route:', routeInfo.parametrizedRoute); | ||
|
|
||
| rootSpan.setAttributes({ | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', | ||
| 'http.route': routeInfo.parametrizedRoute, | ||
| }); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix PR missing regression test for virtual module
Low Severity
This is a
fixPR that introduces a new virtual module strategy for Nuxt v4+ and a new legacy plugin file, but doesn't include any unit, integration, or E2E test to validate the regression (SSR route parametrization issue #20010). Adding a test that verifies the virtual module approach works for Nuxt v4+ and the template fallback for Nuxt v3 would help prevent regressions.Additional Locations (1)
packages/nuxt/src/runtime/plugins/route-detector-legacy.server.ts#L1-L49Triggered by project rule: PR Review Guidelines for Cursor Bot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have Nuxt 4 tests already covering that