-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
ref(nuxt): Extract handler patching to extra plugin for Nitro v2/v3 #19915
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,6 +80,7 @@ export default defineNuxtModule<ModuleOptions>({ | |
| const serverConfigFile = findDefaultSdkInitFile('server', nuxt); | ||
|
|
||
| if (serverConfigFile) { | ||
| addServerPlugin(moduleDirResolver.resolve('./runtime/plugins/handler-legacy.server')); | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| addServerPlugin(moduleDirResolver.resolve('./runtime/plugins/sentry.server')); | ||
|
Comment on lines
+83
to
84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The new Suggested FixThe refactoring appears to be incomplete. To avoid double patching, the patching logic within Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews.
sentry[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| addPlugin({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type { EventHandler } from 'h3'; | ||
| import type { NitroAppPlugin } from 'nitropack'; | ||
| import { patchEventHandler } from '../utils/patchEventHandler'; | ||
|
|
||
| export default (nitroApp => { | ||
| nitroApp.h3App.handler = patchEventHandler<EventHandler>(nitroApp.h3App.handler); | ||
s1gr1d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) satisfies NitroAppPlugin; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import type { EventHandler } from 'nitro/h3'; | ||
| import type { NitroAppPlugin, NitroApp } from 'nitro/types'; | ||
| import { patchEventHandler } from '../utils/patchEventHandler'; | ||
|
|
||
| /** | ||
| * This plugin patches the h3 event handler for Nuxt v5+ (Nitro v3+). | ||
| */ | ||
| export default ((nitroApp: NitroApp) => { | ||
| if (nitroApp?.h3?.handler) { | ||
| // oxlint-disable-next-line @typescript-eslint/unbound-method | ||
| nitroApp.h3.handler = patchEventHandler<EventHandler>(nitroApp.h3.handler); | ||
| } | ||
| }) satisfies NitroAppPlugin; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { | ||
| debug, | ||
| flushIfServerless, | ||
| getDefaultIsolationScope, | ||
| getIsolationScope, | ||
| withIsolationScope, | ||
| } from '@sentry/core'; | ||
|
|
||
| /** | ||
| * Patches the H3 event handler of Nitro. | ||
| * | ||
| * Uses a TypeScript generic type to ensure the returned handler type fits different versions of Nitro. | ||
| */ | ||
| export function patchEventHandler<H3EventHandler extends Function>(handler: H3EventHandler): H3EventHandler { | ||
| return new Proxy(handler, { | ||
| async apply(handlerTarget, handlerThisArg, handlerArgs: unknown) { | ||
| const isolationScope = getIsolationScope(); | ||
| const newIsolationScope = isolationScope === getDefaultIsolationScope() ? isolationScope.clone() : isolationScope; | ||
|
|
||
| debug.log( | ||
| `Patched h3 event handler. ${ | ||
| isolationScope === newIsolationScope ? 'Using existing' : 'Created new' | ||
| } isolation scope.`, | ||
| ); | ||
|
|
||
| return withIsolationScope(newIsolationScope, async () => { | ||
| try { | ||
| return await handlerTarget.apply(handlerThisArg, handlerArgs); | ||
| } finally { | ||
| await flushIfServerless(); | ||
| } | ||
| }); | ||
| }, | ||
| }); | ||
| } |
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.
I installed the beta version as the
3.0.0version is using different types 😬