-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(nuxt): Enable server-side Sentry during nuxt dev on Nitro v3 (Nuxt 5)
#23644
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
Open
s1gr1d
wants to merge
2
commits into
develop
Choose a base branch
from
sig/nuxt-dev-mode
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+232
−40
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { GLOBAL_OBJ } from '@sentry/core'; | ||
|
|
||
| /** Global flag set by the generated `<buildDir>/dev/sentry.server.config.mjs`. */ | ||
| export const NUXT_DEV_MODE_FLAG = '__SENTRY_NUXT_DEV_MODE__'; | ||
|
|
||
| /** Whether the SDK was preloaded by the generated `nuxt dev` server config file. */ | ||
| export function isNuxtDevRuntime(): boolean { | ||
| return NUXT_DEV_MODE_FLAG in GLOBAL_OBJ && GLOBAL_OBJ[NUXT_DEV_MODE_FLAG] === true; | ||
| } |
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
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,75 @@ | ||
| import type { Nuxt } from '@nuxt/schema'; | ||
| import * as path from 'path'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { addDevServerConfigFile, DEV_SERVER_CONFIG_PATH } from '../../src/vite/addServerConfig'; | ||
|
|
||
| const addTemplateMock = vi.hoisted(() => vi.fn()); | ||
|
|
||
| vi.mock('@nuxt/kit', () => ({ | ||
| addTemplate: addTemplateMock, | ||
| // `@nuxt/kit` resolves rather than joins, which is what lets an absolute layer path win over the base. | ||
| createResolver: (base: string) => ({ resolve: (input: string) => path.resolve(base, input) }), | ||
| })); | ||
|
|
||
| const APP_ROOT = '/my/monorepo/apps/web'; | ||
| // `findDefaultSdkInitFile` always returns an absolute path, built from the layer's own `cwd`. | ||
| const APP_CONFIG = `${APP_ROOT}/sentry.server.config.ts`; | ||
| const LAYER_CONFIG = '/my/monorepo/layers/base/sentry.server.config.ts'; | ||
|
|
||
| function generate(serverConfigFile: string): string { | ||
| const nuxt = { options: { rootDir: APP_ROOT, buildDir: path.join(APP_ROOT, '.nuxt') } } as Nuxt; | ||
|
|
||
| addDevServerConfigFile(nuxt, serverConfigFile); | ||
|
|
||
| return addTemplateMock.mock.calls[0]?.[0].getContents(); | ||
| } | ||
|
|
||
| describe('addDevServerConfigFile', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('writes the file into the build directory so `--import` can resolve it', () => { | ||
| generate(APP_CONFIG); | ||
|
|
||
| expect(addTemplateMock).toHaveBeenCalledWith({ | ||
| filename: DEV_SERVER_CONFIG_PATH, | ||
| write: true, | ||
| getContents: expect.any(Function), | ||
| }); | ||
| }); | ||
|
|
||
| it('imports the user config as a file URL so Node can load it directly', () => { | ||
| expect(generate(APP_CONFIG)).toContain(`await import("file://${APP_CONFIG}")`); | ||
| }); | ||
|
|
||
| it('sets the dev flag before importing the config', () => { | ||
| const contents = generate(APP_CONFIG); | ||
|
|
||
| // A static import would be hoisted above the assignment and `Sentry.init()` would then see no flag. | ||
| expect(contents).not.toMatch(/^import /m); | ||
| expect(contents.indexOf('__SENTRY_NUXT_DEV_MODE__')).toBeLessThan(contents.indexOf('await import(')); | ||
| }); | ||
|
|
||
| it('catches a config Node cannot load, so a broken config does not stop the dev server', () => { | ||
| const contents = generate(APP_CONFIG); | ||
|
|
||
| expect(contents).toMatch(/try \{[\s\S]*await import\([\s\S]*\} catch \(error\) \{[\s\S]*console\.warn\(/); | ||
| expect(contents).toContain('Could not load `sentry.server.config.ts`'); | ||
| }); | ||
|
|
||
| it('documents the command that preloads the file', () => { | ||
| expect(generate(APP_CONFIG)).toContain("NODE_OPTIONS='--import ./.nuxt/dev/sentry.server.config.mjs'"); | ||
| }); | ||
|
|
||
| describe('when the config comes from a layer outside the project root', () => { | ||
| it('imports the config from the layer it belongs to', () => { | ||
| expect(generate(LAYER_CONFIG)).toContain(`await import("file://${LAYER_CONFIG}")`); | ||
| }); | ||
|
|
||
| it('keeps the preload path relative to the project root', () => { | ||
| // The file we generate always lives in the app's own build directory, wherever the config came from. | ||
| expect(generate(LAYER_CONFIG)).toContain("NODE_OPTIONS='--import ./.nuxt/dev/sentry.server.config.mjs'"); | ||
| }); | ||
| }); | ||
| }); |
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.
nit: should this comment be above the
isDevBuild = !!import.meta.devline?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.
doesn't really matter actually 🤔 but this one is more related to
isNuxtDevRuntime()