Skip to content
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

docs(nuxt): Add Nuxt wizard instructions #11984

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions docs/platforms/javascript/common/troubleshooting/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -470,21 +470,21 @@ Learn more about fixing these caching issues in the <PlatformLink to="/sourcemap

```json {tabTitle:npm} {filename:package.json}
"overrides": {
"@vercel/nft": "^0.27.4"
}
"@vercel/nft": "^0.27.4"
}
```

```json {tabTitle:yarn} {filename:package.json}
"resolutions": {
"@vercel/nft": "^0.27.4"
}
"@vercel/nft": "^0.27.4"
}
```

```json {tabTitle:pnpm} {filename:package.json}
"pnpm": {
"overrides": {
"@vercel/nft": "^0.27.4"
}
"overrides": {
"@vercel/nft": "^0.27.4"
}
}
```

Expand All @@ -501,6 +501,43 @@ Learn more about fixing these caching issues in the <PlatformLink to="/sourcemap
pnpm add import-in-the-middle
```
</Expandable>

<Expandable permalink title="Nuxt: Server-side Nitro is not sending events">
Nuxt builds the server-side Nitro application as ES Modules, which introduces limitations for server-side tracing during development.
Currently, trace collection is only supported when building and running the application. Development mode (`nuxt dev`) is currently not supported.

When running the build output, Sentry needs to be initialized before running the rest of the application. This is done automatically, but might not work for your use case.
Read more about this in <PlatformLink to="/install">installation methods</PlatformLink>.

---

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).

We are working on figuring this out ([see issue here](https://github.com/getsentry/sentry-javascript/issues/14514)). For the time being, you can add the following overrides to your application:

```json {tabTitle:npm} {filename:package.json}
"overrides": {
"nitropack": "2.9.7"
"@vercel/nft": "^0.27.4"
}
```

```json {tabTitle:yarn} {filename:package.json}
"resolutions": {
"nitropack": "2.9.7"
"@vercel/nft": "^0.27.4"
}
```

```json {tabTitle:pnpm} {filename:package.json}
"pnpm": {
"overrides": {
"nitropack": "2.9.7"
"@vercel/nft": "^0.27.4"
}
}
```
</Expandable>
</PlatformSection>

If you need additional help, you can [ask on GitHub](https://github.com/getsentry/sentry-javascript/issues/new/choose). Customers on a paid plan may also contact support.
28 changes: 27 additions & 1 deletion docs/platforms/javascript/guides/nuxt/manual-setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,33 @@ If you can't (or prefer not to) run the <PlatformLink to="/#install">automatic s

## Compatibility

The minimum supported Nuxt version is `3.7.0`.
The Sentry Nuxt SDK supports Nuxt version `3.7.0` and above. For best results, we recommend
using Nuxt `3.14.0` or later, which includes updated dependencies critical to the SDK's functionality.

In case you are using Nuxt before version `3.14.0`, add the following overrides:

```json {tabTitle:npm} {filename:package.json}
"overrides": {
"ofetch": "^1.4.0"
"@vercel/nft": "^0.27.4"
}
```

```json {tabTitle:yarn} {filename:package.json}
"resolutions": {
"ofetch": "^1.4.0"
"@vercel/nft": "^0.27.4"
}
```

```json {tabTitle:pnpm} {filename:package.json}
"pnpm": {
"overrides": {
"ofetch": "^1.4.0"
"@vercel/nft": "^0.27.4"
}
}
```

## Install

Expand Down
120 changes: 2 additions & 118 deletions platform-includes/getting-started-config/javascript.nuxt.mdx
Original file line number Diff line number Diff line change
@@ -1,118 +1,2 @@
To set up the Sentry SDK, register the Sentry Nuxt module and initialize the SDK for client and server. At build time, the Sentry Nuxt Module looks for the following two files:

- Client-Side: `sentry.client.config.ts` in the root containing `Sentry.init`
- Server-Side: `sentry.server.config.ts` in the root containing `Sentry.init`

In these files, you can specify the full range of <PlatformLink to="/configuration/options">Sentry SDK options</PlatformLink>.


### Nuxt Module Setup

Add the Sentry Nuxt Module to your `nuxt.config.ts` file:

```javascript {filename:nuxt.config.ts}
export default defineNuxtConfig({
modules: ["@sentry/nuxt/module"]
});
```

Adding this module enables the Sentry SDK in your Nuxt application. The Sentry Nuxt Module will then automatically look for the Sentry configuration files and initialize the SDK accordingly.

### Client-side Setup

Add a `sentry.client.config.ts` file to the root of your project (this is probably the same level as the `package.json`). In this file, import and initialize Sentry, specifying any SDK options for the client:

```javascript {filename:sentry.client.config.ts}
import * as Sentry from '@sentry/nuxt';

Sentry.init({
// If set up, you can use your runtime config here
// dsn: useRuntimeConfig().public.sentry.dsn,
dsn: "___PUBLIC_DSN___",

// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0
});
```

### Server-side Setup

1. Add a `sentry.server.config.ts` file to the root of your project:

```javascript {filename:sentry.server.config.ts}
import * as Sentry from '@sentry/nuxt';

Sentry.init({
dsn: "___PUBLIC_DSN___",

// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0
});
```

The Nuxt `useRuntimeConfig()` does not work in the Sentry server config due to technical reasons (the config file has to
be loaded before Nuxt is loaded). To be able to use `process.env` you either have to add `--env-file=.env` to your node command

```bash {tabTitle: node}
node --env-file=.env .output/server/index.mjs
```

or use the `dotenv` package:

```javascript {tabTitle: Server Config} {filename:sentry.server.config.ts} {1,3}
import dotenv from 'dotenv';

dotenv.config();

// ... rest of the file
```

<Alert level="warning">
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)
</Alert>

#### Troubleshoot Errors during Server Startup

After adding `sentry.server.config.ts` and building the project, you might get an error like this:
`Failed to register ESM hook import-in-the-middle/hook.mjs`. You can add an override (npm/pnpm) or a resolution (yarn)
for `@vercel/nft` to fix this. This will add the `hook.mjs` file to your build output. See the [underlying issue in the UnJS Nitro project](https://github.com/unjs/nitro/issues/2703).


Nitro updated `@vercel/nft` in Nitro version `2.10.0`, so you might not get this error anymore, and you don't need to
add this override/resolution.

```json {tabTitle:npm} {filename:package.json}
"overrides": {
"@vercel/nft": "^0.27.4"
}
```

```json {tabTitle:yarn} {filename:package.json}
"resolutions": {
"@vercel/nft": "^0.27.4"
}
```

```json {tabTitle:pnpm} {filename:package.json}
"pnpm": {
"overrides": {
"@vercel/nft": "^0.27.4"
}
}
```

**Pnpm and Import-In-The-Middle**

Sentry injects `import "import-in-the-middle/hook.mjs"` in your server entry. This import acts as a hint for node bundlers to really include this file.
As pnpm implements a strict dependency isolation, this import might cause problems.
Per default, `shamefully-hoist` is `false` ([pnpm docs here](https://pnpm.io/next/npmrc#shamefully-hoist)) and this prevents accessing non-declared dependencies.
You probably don't want to change this setting, so you have to explicitly add the dependency `import-in-the-middle`:

```json {tabTitle:pnpm} {filename:package.json}
// only when using pnpm
"dependencies": {
"import-in-the-middle": "^1.11.2"
}
```
To complete your configuration, add <PlatformLink to="/configuration/options/">options</PlatformLink> to your `Sentry.init()` calls.
Here, you'll also be able to set context data, which includes data about the <PlatformLink to="/enriching-events/identify-user/">user</PlatformLink>, <PlatformLink to="/enriching-events/tags/">tags</PlatformLink>, or even <PlatformLink to="/enriching-events/context/">arbitrary data</PlatformLink>, all of which will be added to every event sent to Sentry.
26 changes: 18 additions & 8 deletions platform-includes/getting-started-install/javascript.nuxt.mdx
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
```bash {tabTitle:npm}
npm install @sentry/nuxt --save
```
We recommend installing the SDK by running our installation wizard in the root directory of your project:

```bash {tabTitle:yarn}
yarn add @sentry/nuxt
```bash
npx @sentry/wizard@latest -i nuxt --org ___ORG_SLUG___ --project ___PROJECT_SLUG___
```

```bash {tabTitle:pnpm}
pnpm add @sentry/nuxt
```
The wizard will prompt you to log in to Sentry. It will then automatically do the following steps for you:

- create or update Nuxt files with the default Sentry configuration:
- `sentry.(client|server).config.ts` to initialize the SDK
- `nuxt.config.ts` to add build options to add source maps upload and auto-instrumentation via Vite plugins.
- create a `.env.sentry-build-plugin` file with an auth token to upload source maps (this file is automatically added to `.gitignore`)
- add an example page to your frontend app and your server to verify your Sentry setup

After the wizard setup is completed, the SDK will automatically capture unhandled errors, and monitor performance.

You can also <PlatformLink to="/usage/">manually capture errors</PlatformLink>.

<Note>
If the setup through the wizard doesn't work for you, you can also <PlatformLink to="/manual-setup/">set up the SDK manually</PlatformLink>.
</Note>
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## Next Steps

- Track your Vue Components or your Pinia store by [adding support for client features](/platforms/javascript/guides/nuxt/features/)
- In case you experience any issues during setup or startup, check out <PlatformLink to="/troubleshooting">Troubleshooting</PlatformLink>
or read through the different <PlatformLink to="/install">installation methods</PlatformLink>.
8 changes: 8 additions & 0 deletions platform-includes/getting-started-primer/javascript.nuxt.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
This SDK is currently in **beta**. Beta features are still in progress and may have bugs. Please reach out on
[GitHub](https://github.com/getsentry/sentry-javascript/issues/new/choose) if you have any feedback or concerns.
</Alert>


Sentry's Nuxt SDK enables automatic reporting of errors and performance data.

## Compatibility

The Sentry Nuxt SDK supports Nuxt version `3.7.0` and above. For best results, we recommend
using Nuxt `3.14.0` or later, which includes updated dependencies critical to the SDK's functionality.
36 changes: 0 additions & 36 deletions platform-includes/getting-started-sourcemaps/javascript.nuxt.mdx
Original file line number Diff line number Diff line change
@@ -1,37 +1 @@
## Add Readable Stack Traces to Errors

The Sentry Nuxt Module uses the [Sentry Vite Plugin](https://www.npmjs.com/package/@sentry/vite-plugin) to upload source maps for both server and client builds.
This means that when you run a production build (`nuxt build`), source maps will be generated and uploaded to Sentry, so that you get readable stack traces in your Sentry issues.

To upload source maps, specify your Sentry auth token as well as your org and project slugs. Set them in the `sourceMapsUploadOptions` option
inside the `sentry` options of your `nuxt.config.ts`.

<Alert level="info">
The module options inside `sentry` are only affecting the **build-time** of the SDK.
</Alert>

<OrgAuthTokenNote />

```javascript {filename:nuxt.config.ts} {3-9}
export default defineNuxtConfig({
modules: ["@sentry/nuxt/module"],
sentry: {
sourceMapsUploadOptions: {
org: "___ORG_SLUG___",
project: "___PROJECT_SLUG___",
authToken: "___ORG_AUTH_TOKEN___"
}
}
});
```

To upload source maps, the Sentry Nuxt Module will automatically enable source map generation in your project if it is not already enabled.
However, you need to explicitly enable source map generation on the client-side. To do this, add the following code to your Nuxt configuration:

```javascript {filename:nuxt.config.ts} {2}
export default defineNuxtConfig({
sourcemap: { client: true }
});
```

This step is necessary because Nuxt sets default values for source maps ([Nuxt docs](https://nuxt.com/docs/api/nuxt-config#sourcemap)), and the Sentry Nuxt Module keeps these settings when they are explicitly defined.
53 changes: 48 additions & 5 deletions platform-includes/getting-started-verify/javascript.nuxt.mdx
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
```html {tabTitle:Vue} {filename:ErrorButton.vue}
On the client-side:

```html {tabTitle:Vue} {filename:pages/example-error.vue}
<script setup>
const triggerError = () => {
throw new Error("Nuxt Button Error");
};
import * as Sentry from '@sentry/nuxt';
import { useFetch} from '#imports'

function triggerClientError() {
throw new Error("Nuxt Button Error");
};

function getSentryData() {
Sentry.startSpan(
{
name: 'Example Frontend Span',
op: 'test'
},
async () => {
await useFetch('/sentry-example-api');
}
)
}
</script>

<template>
<button id="errorBtn" @click="triggerError">Trigger Error</button>
<button id="errorBtn" @click="triggerClientError">
Throw Client Error
</button>
<button type="button" @click="getSentryData">
Throw Server Error
</button>
</template>
```

On the server-side:

```js {tabTitle:Nitro} {filename:server/sentry-example-api.ts}
import { defineEventHandler } from '#imports';

export default defineEventHandler(event => {
throw new Error("Sentry Example API Route Error");
});

```

<Alert level="warning">
Keep in mind, that the server-side monitoring **does not work in development mode!**

If you want to test server-side monitoring locally, build your project and run:
```
# Start your app after building your project with `nuxi build`
node .output/server/index.mjs
```

In case you experience any issues with the server-side setup, check out <PlatformLink to="/troubleshooting">Troubleshooting</PlatformLink>
or read through the different <PlatformLink to="/install">installation methods</PlatformLink>.
</Alert>