Repository navigation
feat(nuxt): Detect the server API with @nuxt/kit 4.6
#25138
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
+401
−305
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
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
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
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
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,124 @@ | ||
| import { addNitroPlugin, createResolver } from '@nuxt/kit'; | ||
| import type { Nuxt } from '@nuxt/schema'; | ||
| import { consoleSandbox } from '@sentry/core'; | ||
| import type { Nitro } from 'nitropack/types'; | ||
| import type { SentryNuxtModuleOptions } from '../common/types'; | ||
| import { | ||
| addDynamicImportEntryFileWrapper, | ||
| addSentryTopImport, | ||
| addServerConfigPlugin, | ||
| addServerConfigShimWithWarning, | ||
| addServerConfigToBuild, | ||
| } from './addServerConfig'; | ||
| import { addDatabaseInstrumentation } from './databaseConfig'; | ||
| import { addMiddlewareImports, addMiddlewareInstrumentation } from './middlewareConfig'; | ||
| import { addStorageInstrumentation } from './storageConfig'; | ||
| import type { ServerApi } from './utils'; | ||
|
|
||
| type NitroModuleOptions = Omit<SentryNuxtModuleOptions, 'experimental_entrypointWrappedFunctions'> & | ||
| Required<Pick<SentryNuxtModuleOptions, 'experimental_entrypointWrappedFunctions'>>; | ||
|
|
||
| /** | ||
| * Sets up the server SDK on the Nitro side: the server config plugin, the server plugins, and the | ||
| * middleware, storage and database instrumentation. | ||
| * | ||
| * A host on the `nuxt` server API runs no Nitro, so it gets none of this. Kit would skip each | ||
| * Nitro-only registration there with a `NUXT_B8024` warning. | ||
| */ | ||
| export function setupNitroServer( | ||
| nuxt: Nuxt, | ||
| serverApi: ServerApi, | ||
| serverConfigFile: string, | ||
| moduleOptions: NitroModuleOptions, | ||
| ): void { | ||
| if (serverApi === 'nuxt') { | ||
| return; | ||
| } | ||
|
|
||
| const isNitroV3 = serverApi === 'nitro3'; | ||
| const moduleDirResolver = createResolver(import.meta.url); | ||
| // oxlint-disable-next-line typescript/no-deprecated -- supported until removal | ||
| const injectMode = moduleOptions.autoInjectServerSentry; | ||
| // The deprecated inject modes replace the default in-bundle initialization until their removal | ||
| const deprecatedInjectMode = | ||
| injectMode === 'top-level-import' || injectMode === 'experimental_dynamic-import' ? injectMode : undefined; | ||
|
|
||
| if (!deprecatedInjectMode) { | ||
| addServerConfigPlugin(nuxt, serverConfigFile, !isNitroV3); | ||
| } | ||
|
|
||
| if (isNitroV3) { | ||
| addNitroPlugin(moduleDirResolver.resolve('./runtime/plugins/handler.server')); | ||
| addNitroPlugin(moduleDirResolver.resolve('./runtime/plugins/update-route-name.server')); | ||
| } else { | ||
| addNitroPlugin(moduleDirResolver.resolve('./runtime/plugins/handler-legacy.server')); | ||
| addNitroPlugin(moduleDirResolver.resolve('./runtime/plugins/update-route-name-legacy.server')); | ||
| } | ||
|
|
||
| addNitroPlugin(moduleDirResolver.resolve('./runtime/plugins/sentry.server')); | ||
|
|
||
| // Preps the middleware instrumentation module. | ||
| addMiddlewareImports(); | ||
| addStorageInstrumentation(nuxt, !isNitroV3); | ||
| addDatabaseInstrumentation(nuxt.options.nitro, !isNitroV3, moduleOptions); | ||
|
|
||
| nuxt.hooks.hook('nitro:init', nitro => { | ||
| if (nuxt.options?._prepare) { | ||
| return; | ||
| } | ||
|
|
||
| addMiddlewareInstrumentation(nitro, isNitroV3); | ||
|
|
||
| if (deprecatedInjectMode) { | ||
| setupDeprecatedInjectMode(nitro, deprecatedInjectMode, isNitroV3, serverConfigFile, moduleOptions); | ||
| return; | ||
| } | ||
|
|
||
| addServerConfigShimWithWarning(nitro); | ||
|
|
||
| if (moduleOptions.debug) { | ||
| consoleSandbox(() => { | ||
| // eslint-disable-next-line no-console | ||
| console.log( | ||
| `[Sentry] Bundled \`${serverConfigFile}\` into the Nitro server build. The SDK initializes itself at server startup — no \`node --import\` preload needed.`, | ||
| ); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function setupDeprecatedInjectMode( | ||
| nitro: Nitro, | ||
| injectMode: 'top-level-import' | 'experimental_dynamic-import', | ||
| isNitroV3: boolean, | ||
| serverConfigFile: string, | ||
| moduleOptions: NitroModuleOptions, | ||
| ): void { | ||
| consoleSandbox(() => { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
| `[Sentry] \`autoInjectServerSentry: '${injectMode}'\` is deprecated and will be removed in a future major version. The Sentry server config is bundled into the Nitro server build by default now. Remove the option to use the default behavior.`, | ||
| ); | ||
| }); | ||
|
|
||
| if (injectMode === 'top-level-import') { | ||
| // Nitro 3 (in Nuxt 5) is not bundled in dev mode, so there is no build to emit into. | ||
| if (!(isNitroV3 && nitro.options.dev)) { | ||
| addServerConfigToBuild(moduleOptions, nitro, serverConfigFile); | ||
| } | ||
| addSentryTopImport(moduleOptions, nitro); | ||
| } | ||
|
|
||
| if (injectMode === 'experimental_dynamic-import') { | ||
| addDynamicImportEntryFileWrapper(nitro, serverConfigFile, moduleOptions); | ||
|
|
||
| if (moduleOptions.debug) { | ||
| consoleSandbox(() => { | ||
| // eslint-disable-next-line no-console | ||
| console.log( | ||
| '[Sentry] Wrapping the server entry file with a dynamic `import()`, so Sentry can be preloaded before the server initializes.', | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
| } |
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.
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.
This is just copy-pasted code from the
module.tsfile