Skip to content

Commit 33685cc

Browse files
authored
feat(nuxt): Use --import as the default installation method (#12070)
* feat(nuxt): Use --import as the default installation method * change to experimental_entrypointWrappedFunctions * review suggestions
1 parent f48a63e commit 33685cc

File tree

5 files changed

+88
-42
lines changed

5 files changed

+88
-42
lines changed

docs/platforms/javascript/common/install/esm-without-import.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ supported:
2020
When running your application in ESM mode, you will most likely want to <PlatformLink to="/install/esm">follow the ESM instructions</PlatformLink>. However, if you want to avoid using the `--import` command line option, for example if you have no way of configuring a CLI flag, you can also follow an alternative setup that involves importing the `instrument.mjs` file directly in your application.
2121

2222
<Alert level='warning' title='Restrictions of this installation method'>
23-
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.
24-
25-
Because of this, we recommend using this only if the `--import` flag is not an option for you.
23+
This installation method has fundamental restrictions:
24+
- It only supports limited performance instrumentation.
25+
- Only basic `http` instrumentation will work.
26+
- No DB or framework-specific instrumentation will be available.
2627

28+
We recommend using this only if the `--import` flag is not an option for you.
2729
</Alert>
2830

2931
You need to create a file named `instrument.mjs` that imports and initializes Sentry:

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

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,29 @@
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 how to use 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 is the default way in ESM to preload a module before the application starts.
1010
If you cannot use the SDK's <PlatformLink to="/install/dynamic-import/">default dynamic import behaviour</PlatformLink>, setting
1111
this flag is crucial for ensuring proper server-side initialization, as Sentry needs to be initialized before the rest of the application runs.
1212

1313
## Scenarios where `--import` does not work
1414

1515
- 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
16+
- **Netlify** only allows scoped environment variables on its paid plans at this time
17+
- **Vercel** doesn't currently provide an option for scoping environment variables
1818

1919
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.
20+
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.
2121

2222
## Initializing Sentry with `--import`
2323

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

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
26+
### 1. Adding `--import` to `node` command
4327

4428
After building your Nuxt app with `nuxi build`, add the `--import` flag followed by the Sentry server config path to the `node` command.
4529
With the default Nitro Node preset, this typically looks like this:
@@ -57,13 +41,12 @@ To make things easier, add a script in the `package.json`:
5741
```json {filename:package.json}
5842
{
5943
"scripts": {
60-
"preview": "NODE_OPTIONS='--import .//server/sentry.server.config.mjs' nuxt preview",
6144
"start": "node --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs"
6245
}
6346
}
6447
```
6548

66-
### 3. Adding `--import` flag in production
49+
### 2. Adding `--import` flag in production
6750

6851
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.
6952
Consult your hosting provider's documentation for specific implementation details.

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

Lines changed: 25 additions & 12 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 some build-time configuration to ensure your app is wrapped with `import()`.
1628
With this, Sentry can be initialized before all other modules of the application.
1729

18-
The server entry file will look something like this:
30+
The Nuxt 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 in your application:
3348

3449
```json {tabTitle:npm} {filename:package.json}
3550
"overrides": {
@@ -52,16 +67,14 @@ 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.
56-
70+
You can also check out the guide for installing the SDK with a <PlatformLink to="/install/cli-import">CLI flag `--import`</PlatformLink> or a <PlatformLink to="/install/top-level-import/">top-level import</PlatformLink>.
5771

5872
## Re-exporting serverless handler functions
5973

6074
Sentry automatically detects serverless handler functions in the build output and re-exports them from the server entry file.
6175

6276
By default, Sentry re-exports functions named `handler`, `server`, and `default` exports. This will work in most cases and no other action is required.
63-
In case your serverless function has another, custom name you can override this with `entrypointWrappedFunctions`:
64-
77+
If your serverless function has a custom name, you can override it with `experimental_entrypointWrappedFunctions`:
6578

6679
```javascript {filename: nuxt.config.ts} {7}
6780
export default defineNuxtConfig({
@@ -70,7 +83,7 @@ export default defineNuxtConfig({
7083
sentry: {
7184
// Customize detected function names
7285
// Default value: ['default', 'handler', 'server']
73-
entrypointWrappedFunctions: ['customFunctionName']
86+
experimental_entrypointWrappedFunctions: ['customFunctionName']
7487
},
7588
});
7689
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 Nuxt server entry file.
12+
In this case, Sentry isn’t initialized before the app starts, but is set up as early as possible.
13+
14+
<Alert level='warning' title='Restrictions of this installation method'>
15+
This installation method has fundamental restrictions:
16+
- It only supports limited performance instrumentation.
17+
- Only basic `http` instrumentation will work.
18+
- No DB or framework-specific instrumentation will be available.
19+
20+
We recommend using this only if the `--import` flag is not an option for you.
21+
</Alert>
22+
23+
## Initializing Sentry With a Top-level `import`
24+
25+
Enable the top-level `import` by setting `autoInjectServerSentry`:
26+
27+
```typescript {filename:nuxt.config.ts} {3}
28+
export default defineNuxtConfig({
29+
sentry: {
30+
autoInjectServerSentry: 'top-level-import'
31+
},
32+
})
33+
```
34+
35+
By default, the SDK will add the Sentry server config to the build output (typically, `.output/server/sentry.server.config.mjs`).
36+
37+
With the top-level `import`, the Nuxt server entry file will look something like this:
38+
39+
```javascript {filename:.output/server/index.mjs}
40+
import './sentry.server.config.mjs';
41+
// Note: The file may have some imports and code, related to debug IDs
42+
```

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 by being explicitly added via `--import`.
131+
132+
Check out the <PlatformLink to="/install/cli-import">`--import` CLI flag</PlatformLink> docs 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)