Skip to content

Commit 878d13b

Browse files
committed
feat(nuxt): Use --import as the default installation method
1 parent f36916d commit 878d13b

File tree

4 files changed

+79
-34
lines changed

4 files changed

+79
-34
lines changed

docs/platforms/javascript/guides/nuxt/install/cli-import.mdx

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
11
---
2-
title: --import CLI flag
3-
sidebar_order: 2
4-
description: "Learn about using the node `--import` CLI flag."
2+
title: --import CLI flag (default)
3+
sidebar_order: 1
4+
description: "Learn about using the node --import CLI flag."
55
---
66

77
## Understanding the `--import` CLI Flag
88

9-
The [`--import` CLI flag](https://nodejs.org/api/cli.html#--importmodule) in Node allows you to preload modules before the application starts.
9+
The [`--import` CLI flag](https://nodejs.org/api/cli.html#--importmodule) in Node allows you to preload modules before the application starts. This is the default
10+
way in ESM to load a module before the application starts.
1011
If you cannot use the SDK's <PlatformLink to="/install/dynamic-import/">default dynamic import behaviour</PlatformLink>, setting
1112
this flag is crucial for ensuring proper server-side initialization, as Sentry needs to be initialized before the rest of the application runs.
1213

1314
## Scenarios where `--import` does not work
1415

1516
- You're not able to add Node CLI flags or environment variables that are scoped to the function runtime
16-
- As of now, Netlify only supports adding scoped environment variables in the paid plan
17-
- As of now, Vercel does not provide an option for scoping environment variables
17+
- As of now, **Netlify** only supports adding scoped environment variables in the paid plan
18+
- As of now, **Vercel** does not provide an option for scoping environment variables
1819

1920
If any of those points apply to you, you cannot use the `--import` flag to initialize Sentry on the server-side.
20-
Check out the guide for using <PlatformLink to="/install/dynamic-import/">dynamic import</PlatformLink> instead.
21+
Check out the guide for using <PlatformLink to="/install/top-level-import/">a top-level import</PlatformLink> or a <PlatformLink to="/install/dynamic-import/">dynamic import</PlatformLink> instead.
2122

2223
## Initializing Sentry with `--import`
2324

24-
To successfully use the `--import` flag, follow these steps:
25+
By default, the SDK will add the Sentry server config to the build output (typically `.output/server/sentry.server.config.mjs`).
2526

26-
### 1. Disable Dynamic Import
27-
28-
First, disable the dynamic import by setting `dynamicImportForServerEntry` to `false`:
29-
30-
```javascript {filename: nuxt.config.ts} {5}
31-
export default defineNuxtConfig({
32-
modules: ["@sentry/nuxt/module"],
33-
34-
sentry: {
35-
dynamicImportForServerEntry: false,
36-
},
37-
});
38-
```
39-
40-
By disabling the dynamic import, the SDK will add the Sentry server config to the build output (typically `.output/server/sentry.server.config.mjs`).
41-
42-
### 2. Adding `--import` to `node` command
27+
### 1. Adding `--import` to `node` command
4328

4429
After building your Nuxt app with `nuxi build`, add the `--import` flag followed by the Sentry server config path to the `node` command.
4530
With the default Nitro Node preset, this typically looks like this:
@@ -57,13 +42,13 @@ To make things easier, add a script in the `package.json`:
5742
```json {filename:package.json}
5843
{
5944
"scripts": {
60-
"preview": "NODE_OPTIONS='--import .//server/sentry.server.config.mjs' nuxt preview",
45+
"preview": "NODE_OPTIONS='--import ./server/sentry.server.config.mjs' nuxt preview",
6146
"start": "node --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs"
6247
}
6348
}
6449
```
6550

66-
### 3. Adding `--import` flag in production
51+
### 2. Adding `--import` flag in production
6752

6853
To be able to set up Sentry in production, the `--import` flag needs to be added wherever you run your application's production build output.
6954
Consult your hosting provider's documentation for specific implementation details.

docs/platforms/javascript/guides/nuxt/install/dynamic-import.mdx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
---
2-
title: Dynamic Import (default)
3-
sidebar_order: 1
2+
title: Dynamic Import (experimental)
3+
sidebar_order: 3
44
description: "Learn about how the Nuxt SDK leverages dynamic input() in the build output."
55
---
66

77
## Understanding the `import()` expression
88

99
The `import()` expression (also called "dynamic import") allows conditional and flexible module loading in ESM.
1010
For the Sentry Nuxt SDK, it provides an approach to initialize server-side configuration before application startup.
11+
12+
The Sentry Nuxt SDK will be initialized before any other code runs and the Nitro server runtime code will be loaded later because of `import()`ing it.
1113
This early initialization is required to set up the SDK's instrumentation of various libraries correctly.
1214

1315
## Initializing Sentry with Dynamic `import()`
1416

15-
By default, the Sentry Nuxt SDK includes a Rollup plugin which wraps the server entry file with a dynamic `import()`.
17+
Enable the dynamic `import()` by setting `autoInjectServerSentry`:
18+
19+
```typescript {filename:nuxt.config.ts} {3}
20+
export default defineNuxtConfig({
21+
sentry: {
22+
autoInjectServerSentry: 'experimental_dynamic-import'
23+
},
24+
})
25+
```
26+
27+
After setting this, the Sentry Nuxt SDK adds a Rollup plugin which wraps the server entry file with a dynamic `import()`.
1628
With this, Sentry can be initialized before all other modules of the application.
1729

1830
The server entry file will look something like this:
1931

2032
```javascript {filename:.output/server/index.mjs}
21-
// Note: file may have some imports and code related to debug IDs
33+
// Note: The file may have some imports and code related to debug IDs
2234
Sentry.init({
2335
dsn: "..."
2436
});
@@ -28,8 +40,11 @@ import('./chunks/nitro/nitro.mjs').then(function (n) { return n.r; });
2840

2941
## Scenarios where `import()` doesn't work
3042

31-
We are currently investigating an issue where the server-side is not correctly initialized with a recent update of Nitro (the server-side toolkit in Nuxt).
32-
We are working on figuring this out ([see issue here](https://github.com/getsentry/sentry-javascript/issues/14514)). As a temporary workaround, you can add the following overrides to your application:
43+
Depending on your setup and which version of Nitro is used, the server-side is sometimes not correctly initialized.
44+
The build output **must not** include a regular `import` of the Nitro runtime (e.g. `import './chunks/nitro/nitro.mjs'`).
45+
See the ([GitHub issue here](https://github.com/getsentry/sentry-javascript/issues/14514)) for more information.
46+
47+
As a temporary workaround, you can add the following overrides to your application:
3348

3449
```json {tabTitle:npm} {filename:package.json}
3550
"overrides": {
@@ -52,7 +67,7 @@ We are working on figuring this out ([see issue here](https://github.com/getsent
5267
}
5368
```
5469

55-
You can also check out the guide for using the <PlatformLink to="/install/cli-import//">CLI flag `--import`</PlatformLink> as this might be a better choice for you.
70+
You can also check out the guide for using the <PlatformLink to="/install/cli-import">CLI flag `--import`</PlatformLink> or <PlatformLink to="/install/top-level-import/">a top-level import</PlatformLink> as this might be a better choice for you.
5671

5772

5873
## Re-exporting serverless handler functions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
title: Top-level import
3+
sidebar_order: 2
4+
description: "Learn about how the Nuxt SDK adds a top-level import to the build output."
5+
---
6+
7+
## Understanding top-level `import`
8+
9+
Sentry needs to be initialized before the application starts.
10+
If the default way of adding an <PlatformLink to="/install/cli-import">`--import` CLI flag</PlatformLink> flag does not work for you,
11+
set up the SDK for adding a top-level `import`. This will import the Sentry server-side config at the top of the server entry file.
12+
In this scenario, Sentry is not initialized before application startup, but as early as otherwise possible.
13+
14+
<Alert level='warning' title='Restrictions of this installation method'>
15+
This installation method has a fundamental restriction: It only supports limited performance instrumentation. Only basic `http` instrumentation will work, and no DB or framework-specific instrumentation will be available.
16+
17+
Because of this, we recommend using this only if the `--import` flag is not an option for you.
18+
</Alert>
19+
20+
## Initializing Sentry with a top-level `import`
21+
22+
Enable the top-level `import` by setting `autoInjectServerSentry`:
23+
24+
```typescript {filename:nuxt.config.ts} {3}
25+
export default defineNuxtConfig({
26+
sentry: {
27+
autoInjectServerSentry: 'top-level-import'
28+
},
29+
})
30+
```
31+
32+
By default, the SDK will add the Sentry server config to the build output (typically `.output/server/sentry.server.config.mjs`).
33+
34+
With the top-level `import`, the server entry file will look something like this:
35+
36+
```javascript {filename:.output/server/index.mjs}
37+
import './sentry.server.config.mjs';
38+
// Note: The file may have some imports and code related to debug IDs
39+
```

docs/platforms/javascript/guides/nuxt/manual-setup.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ dotenv.config();
125125
// ... rest of the file
126126
```
127127

128+
#### Load Sentry config
129+
130+
Sentry can only be loaded on the server-side if it is explicitly added via `--import`.
131+
132+
Refer to the documentation of using <PlatformLink to="/install/cli-import">`--import` CLI flag</PlatformLink> for setup instructions.
133+
128134
<Alert level="warning">
129135
In the beta state of the Nuxt SDK, some features may not work with certain deployment providers. Check the progress on GitHub: [Compatibility with different Deployment Platforms](https://github.com/getsentry/sentry-javascript/issues/14029)
130136
</Alert>

0 commit comments

Comments
 (0)