Skip to content

Commit 200ba3a

Browse files
committed
feat(nuxt): Make dynamic import() wrapping default
1 parent 8782af8 commit 200ba3a

File tree

3 files changed

+32
-69
lines changed

3 files changed

+32
-69
lines changed

packages/nuxt/src/common/types.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ export type SentryNuxtModuleOptions = {
103103
debug?: boolean;
104104

105105
/**
106-
* Enabling basic server tracing can be used for environments where modifying the node option `--import` is not possible.
107-
* However, enabling this option only supports limited tracing instrumentation. Only http traces will be collected (but no database-specific traces etc.).
106+
* If this option is `false`, the Sentry SDK won't wrap the server entry file with `import()`. Not wrapping the
107+
* server entry file will disable Sentry on the server-side. When you set this option to `true`, make sure
108+
* to add the sentry server config with the node `--import` CLI flag to enable Sentry on the server-side.
108109
*
109-
* If this option is `true`, the Sentry SDK will import the Sentry server config at the top of the server entry file to load the SDK on the server.
110-
*
111-
* **DO NOT** enable this option if you've already added the node option `--import` in your node start script. This would initialize Sentry twice on the server-side and leads to unexpected issues.
110+
* **DO NOT** add the node CLI flag `--import` in your node start script, when `disableDynamicImportWrapping` is set to `false`.
111+
* This would initialize Sentry twice on the server-side and this leads to unexpected issues.
112112
*
113113
* @default false
114114
*/
115-
experimental_basicServerTracing?: boolean;
115+
disableDynamicImportWrapping?: boolean;
116116

117117
/**
118118
* Options to be passed directly to the Sentry Rollup Plugin (`@sentry/rollup-plugin`) and Sentry Vite Plugin (`@sentry/vite-plugin`) that ship with the Sentry Nuxt SDK.

packages/nuxt/src/module.ts

+26-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from 'path';
22
import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
33
import { consoleSandbox } from '@sentry/utils';
44
import type { SentryNuxtModuleOptions } from './common/types';
5-
import { addSentryTopImport, addServerConfigToBuild } from './vite/addServerConfig';
5+
import { addDynamicImportEntryFileWrapper, addServerConfigToBuild } from './vite/addServerConfig';
66
import { setupSourceMaps } from './vite/sourceMaps';
77
import { findDefaultSdkInitFile } from './vite/utils';
88

@@ -48,17 +48,19 @@ export default defineNuxtModule<ModuleOptions>({
4848
const serverConfigFile = findDefaultSdkInitFile('server');
4949

5050
if (serverConfigFile) {
51-
// Inject the server-side Sentry config file with a side effect import
52-
addPluginTemplate({
53-
mode: 'server',
54-
filename: 'sentry-server-config.mjs',
55-
getContents: () =>
56-
`import "${buildDirResolver.resolve(`/${serverConfigFile}`)}"\n` +
57-
'import { defineNuxtPlugin } from "#imports"\n' +
58-
'export default defineNuxtPlugin(() => {})',
59-
});
51+
if (moduleOptions.disableDynamicImportWrapping) {
52+
// Inject the server-side Sentry config file with a side effect import
53+
addPluginTemplate({
54+
mode: 'server',
55+
filename: 'sentry-server-config.mjs',
56+
getContents: () =>
57+
`import "${buildDirResolver.resolve(`/${serverConfigFile}`)}"\n` +
58+
'import { defineNuxtPlugin } from "#imports"\n' +
59+
'export default defineNuxtPlugin(() => {})',
60+
});
6061

61-
addServerPlugin(moduleDirResolver.resolve('./runtime/plugins/sentry.server'));
62+
addServerPlugin(moduleDirResolver.resolve('./runtime/plugins/sentry.server'));
63+
}
6264
}
6365

6466
if (clientConfigFile || serverConfigFile) {
@@ -67,11 +69,9 @@ export default defineNuxtModule<ModuleOptions>({
6769

6870
nuxt.hooks.hook('nitro:init', nitro => {
6971
if (serverConfigFile && serverConfigFile.includes('.server.config')) {
70-
addServerConfigToBuild(moduleOptions, nuxt, nitro, serverConfigFile);
72+
if (moduleOptions.disableDynamicImportWrapping) {
73+
addServerConfigToBuild(moduleOptions, nuxt, nitro, serverConfigFile);
7174

72-
if (moduleOptions.experimental_basicServerTracing) {
73-
addSentryTopImport(moduleOptions, nitro);
74-
} else {
7575
if (moduleOptions.debug) {
7676
const serverDirResolver = createResolver(nitro.options.output.serverDir);
7777
const serverConfigPath = serverDirResolver.resolve('sentry.server.config.mjs');
@@ -86,6 +86,17 @@ export default defineNuxtModule<ModuleOptions>({
8686
);
8787
});
8888
}
89+
} else {
90+
addDynamicImportEntryFileWrapper(nitro, serverConfigFile);
91+
92+
if (moduleOptions.debug) {
93+
consoleSandbox(() => {
94+
// eslint-disable-next-line no-console
95+
console.log(
96+
'[Sentry] Wrapping the server entry file with a dynamic `import()`, so Sentry can be preloaded before the server initializes.',
97+
);
98+
});
99+
}
89100
}
90101
}
91102
});

packages/nuxt/src/vite/addServerConfig.ts

-48
Original file line numberDiff line numberDiff line change
@@ -74,54 +74,6 @@ export function addServerConfigToBuild(
7474
});
7575
}
7676

77-
/**
78-
* Adds the Sentry server config import at the top of the server entry file to load the SDK on the server.
79-
* This is necessary for environments where modifying the node option `--import` is not possible.
80-
* However, only limited tracing instrumentation is supported when doing this.
81-
*/
82-
export function addSentryTopImport(moduleOptions: SentryNuxtModuleOptions, nitro: Nitro): void {
83-
nitro.hooks.hook('close', () => {
84-
// other presets ('node-server' or 'vercel') have an index.mjs
85-
const presetsWithServerFile = ['netlify'];
86-
const entryFileName =
87-
typeof nitro.options.rollupConfig?.output.entryFileNames === 'string'
88-
? nitro.options.rollupConfig?.output.entryFileNames
89-
: presetsWithServerFile.includes(nitro.options.preset)
90-
? 'server.mjs'
91-
: 'index.mjs';
92-
93-
const serverDirResolver = createResolver(nitro.options.output.serverDir);
94-
const entryFilePath = serverDirResolver.resolve(entryFileName);
95-
96-
try {
97-
fs.readFile(entryFilePath, 'utf8', (err, data) => {
98-
const updatedContent = `import './${SERVER_CONFIG_FILENAME}.mjs';\n${data}`;
99-
100-
fs.writeFile(entryFilePath, updatedContent, 'utf8', () => {
101-
if (moduleOptions.debug) {
102-
consoleSandbox(() => {
103-
// eslint-disable-next-line no-console
104-
console.log(
105-
`[Sentry] Successfully added the Sentry import to the server entry file "\`${entryFilePath}\`"`,
106-
);
107-
});
108-
}
109-
});
110-
});
111-
} catch (err) {
112-
if (moduleOptions.debug) {
113-
consoleSandbox(() => {
114-
// eslint-disable-next-line no-console
115-
console.warn(
116-
`[Sentry] An error occurred when trying to add the Sentry import to the server entry file "\`${entryFilePath}\`":`,
117-
err,
118-
);
119-
});
120-
}
121-
}
122-
});
123-
}
124-
12577
/**
12678
* This function modifies the Rollup configuration to include a plugin that wraps the entry file with a dynamic import (`import()`)
12779
* and adds the Sentry server config with the static `import` declaration.

0 commit comments

Comments
 (0)