Skip to content

Commit 1fc7506

Browse files
authored
docs(solidstart): Add docs for installation methods (#12335)
* docs(solidstart): Add docs for installation methods * review suggestions
1 parent 8a06050 commit 1fc7506

File tree

7 files changed

+240
-21
lines changed

7 files changed

+240
-21
lines changed

docs/platforms/javascript/common/install/loader.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ notSupported:
1919
- javascript.wasm
2020
- javascript.remix
2121
- javascript.react-router
22+
- javascript.solidstart
2223
- javascript.svelte
2324
- javascript.sveltekit
2425
- javascript.node

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Most deployment platforms support this through two primary methods:
5555

5656
#### Option 1: Direct CLI Flag
5757
```bash
58-
node --import .output/server/sentry.server.config.mjs your-server-entry.mjs
58+
node --import ./.output/server/sentry.server.config.mjs your-server-entry.mjs
5959
```
6060

6161
#### Option 2: Environment Variable
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: --import CLI flag (default)
3+
sidebar_order: 1
4+
description: "Learn how to use the node --import CLI flag."
5+
---
6+
7+
## Understanding the `--import` CLI Flag
8+
9+
The [`--import` CLI flag](https://nodejs.org/api/cli.html#--importmodule) in Node is the default way in ESM to preload a specified module at startup.
10+
Setting this CLI flag is crucial for ensuring proper server-side initialization, as Sentry needs to be initialized before the application runs.
11+
This will register Sentry's [loader hook](https://nodejs.org/api/module.html#customization-hooks) and therefore enable proper instrumentation of ESM modules.
12+
13+
## Scenarios where `--import` does not work
14+
15+
- You're unable to add Node CLI flags or environment variables scoped to the function runtime, meaning these variables aren't applied in other scopes, such as build time.
16+
- You don't know the path to the SolidStart server folder in the build output
17+
- At this time, it isn't possible to properly configure `--import` in **Netlify**.
18+
- At this time, it isn't possible to properly configure `--import` in **Vercel**.
19+
20+
If any of those points apply to you, you cannot use the `--import` flag to initialize Sentry on the server-side.
21+
Check out the guide for using <PlatformLink to="/install/limited-server-tracing">limited server tracing</PlatformLink> instead.
22+
23+
## Initializing Sentry with `--import`
24+
25+
By default, the SDK will add the Sentry server instrumentation file to the build output (typically, `.output/server/instrument.server.mjs`).
26+
27+
### 1. Adding `--import` to `node` command
28+
29+
After building your SolidStart app with `vinxi build`, add the `--import` flag followed by the Sentry server instrumentation file path to the `node` command.
30+
With the default Vinxi Node preset, this typically looks like this:
31+
32+
```bash
33+
node --import ./.output/server/instrument.server.mjs .output/server/index.mjs
34+
```
35+
36+
Check the log at the very end of the console output after building the application.
37+
This will print the node command with the server entry path for your application (`node .output/server/index.mjs` with the Node preset).
38+
Make sure to update the paths accordingly, especially when using a different Vinxi preset.
39+
40+
To make things easier, add a script in the `package.json`:
41+
42+
```json {filename:package.json}
43+
{
44+
"scripts": {
45+
"start": "node --import ./.output/server/instrument.server.mjs .output/server/index.mjs"
46+
}
47+
}
48+
```
49+
50+
### 2. Adding `--import` flag in production
51+
52+
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.
53+
Consult your hosting provider's documentation for specific implementation details.
54+
Most deployment platforms support this through two primary methods:
55+
56+
#### Option 1: Direct CLI Flag
57+
```bash
58+
node --import ./.output/server/instrument.server.mjs your-server-entry.mjs
59+
```
60+
61+
#### Option 2: Environment Variable
62+
```bash
63+
NODE_OPTIONS='--import ./.output/server/instrument.server.mjs'
64+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: Dynamic Import (experimental)
3+
sidebar_order: 3
4+
description: "Learn about how the SolidStart SDK leverages dynamic input() in the build output."
5+
---
6+
7+
## Understanding the `import()` expression
8+
9+
<Alert level='warning'>
10+
This setting is experimental as it is not guaranteed to work with every setup and the underlying functionality could change.
11+
12+
We recommend reading the guide for installing the SDK with the <PlatformLink to="/install/cli-import">CLI flag `--import`</PlatformLink> or <PlatformLink to="/install/limited-server-tracing">limited server tracing</PlatformLink>
13+
</Alert>
14+
15+
The `import()` expression, or dynamic import, enables flexible, conditional module loading in ESM.
16+
Node.js will generate a separate module graph for any code wrapped in a dynamic `import()`. This separate graph is evaluated **after** all modules, which are statically `import`ed.
17+
18+
By using the Sentry SolidStart SDK, the server-side application will be wrapped in a dynamic `import()`, while the Sentry instrumentation file will be imported with a static `import`.
19+
This makes it possible to initialize the Sentry SolidStart SDK at startup, while the Nitro server runtime loads later because it is `import()`ed.
20+
This early initialization of Sentry is required to correctly set up the SDK's instrumentation of various libraries.
21+
22+
## Scenarios where `import()` doesn't work
23+
24+
Depending on your setup and which version of Vinxi is used (and respectively Nitro, as this runs under the hood), the server-side is sometimes not correctly initialized.
25+
The build output **must not** include a regular `import` of the Nitro runtime (e.g. `import './chunks/nitro/nitro.mjs'`).
26+
27+
You can also check out the guide for installing the SDK with the <PlatformLink to="/install/cli-import">CLI flag `--import`</PlatformLink> or <PlatformLink to="/install/limited-server-tracing">limited-server-tracing</PlatformLink>.
28+
29+
## Initializing Sentry with Dynamic `import()`
30+
31+
Enable the dynamic `import()` by setting `autoInjectServerSentry`:
32+
33+
```typescript {filename:app.config.ts} {8}
34+
import { defineConfig } from '@solidjs/start/config';
35+
import { withSentry } from '@sentry/solidstart';
36+
37+
export default defineConfig(withSentry(
38+
{},
39+
{
40+
autoInjectServerSentry: 'experimental_dynamic-import'
41+
})
42+
);
43+
```
44+
45+
After setting this, the Sentry SolidStart SDK will add build-time configuration so that your app will be wrapped with `import()`,
46+
ensuring that Sentry can be initialized before any other modules.
47+
48+
The SolidStart server entry file will look something like this:
49+
50+
```javascript {filename:.output/server/index.mjs}
51+
// Note: The file may have some imports and code, related to debug IDs
52+
Sentry.init({
53+
dsn: "..."
54+
});
55+
56+
import('./chunks/nitro/nitro.mjs').then(function (n) { return n.r; });
57+
```
58+
59+
## Re-exporting serverless handler functions
60+
61+
Sentry automatically detects serverless handler functions in the build output and re-exports them from the server entry file.
62+
63+
By default, Sentry re-exports functions named `handler`, `server`, and `default` exports. This will work in most cases and no other action is required.
64+
If your serverless function has a custom name, you can override it with `experimental_entrypointWrappedFunctions`:
65+
66+
```javascript {filename: app.config.ts} {11}
67+
68+
import { defineConfig } from '@solidjs/start/config';
69+
import { withSentry } from '@sentry/solidstart';
70+
71+
export default defineConfig(withSentry(
72+
{},
73+
{
74+
autoInjectServerSentry: 'experimental_dynamic-import',
75+
// Customize detected function names
76+
// Default value: ['default', 'handler', 'server']
77+
experimental_entrypointWrappedFunctions: ['customFunctionName']
78+
})
79+
);
80+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Installation Methods
3+
sidebar_order: 1.5
4+
description: "Review our alternate installation methods."
5+
---
6+
7+
SolidStart uses ES Modules for server-side builds, which requires Sentry to register Node [customization hooks](https://nodejs.org/api/module.html#customization-hooks).
8+
Those customization hooks need to be registered before the rest of the application.
9+
10+
To be able to run Sentry before the rest of the application and fully monitor the server-side, Sentry can be initialized using one of those two approaches:
11+
12+
<PageGrid />
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: Limited Server Tracing
3+
sidebar_order: 2
4+
description: "Learn how to set up the SolidStart SDK with limited server tracing by adding a top-level import to the build output."
5+
---
6+
7+
## Understanding Limited Server Tracing
8+
9+
Sentry needs to be initialized before the rest of the application runs.
10+
If the default way of adding an <PlatformLink to="/install/cli-import">`--import` CLI flag</PlatformLink> doesn't work for you,
11+
enable the SDK to add a top-level `import`.
12+
13+
The automatically added top-level `import` will then import the Sentry server-side config at the top of the SolidStart server entry file.
14+
In this case, Sentry isn’t initialized before the app starts, but is set up as early as possible.
15+
16+
<Alert level='warning' title='Restrictions of this installation method'>
17+
This installation method has fundamental restrictions:
18+
- Only basic `http` and `fetch` instrumentation will work.
19+
- No DB or framework-specific instrumentation will be available.
20+
21+
We recommend using this only if the `--import` flag is not an option for you.
22+
</Alert>
23+
24+
## Initializing Sentry With a Top-level `import`
25+
26+
Enable the top-level `import` by setting `autoInjectServerSentry`:
27+
28+
```typescript {filename:app.config.ts} {8}
29+
import { defineConfig } from '@solidjs/start/config';
30+
import { withSentry } from '@sentry/solidstart';
31+
32+
export default defineConfig(withSentry(
33+
{},
34+
{
35+
autoInjectServerSentry: 'top-level-import'
36+
})
37+
);
38+
```
39+
40+
By default, the SDK will add the Sentry server instrumentation file to the build output (typically, `.output/server/instrument.server.mjs`).
41+
The SDK will then automatically import this file at the top of the SolidStart server entry file in the build output.

platform-includes/getting-started-config/javascript.solidstart.mdx

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,9 @@ mount(() => <StartClient />, document.getElementById('app'));
2626

2727
### Server-side Setup
2828

29-
Create an instrument file `instrument.server.mjs`, initialize the Sentry SDK and deploy it alongside your application.
30-
For example by placing it in the `public` folder.
29+
Create an instrument file `instrument.server.ts` in your `src` folder. In this file, initialize the Sentry SDK for your server.
3130

32-
<Alert>
33-
34-
Placing `instrument.server.mjs` inside the `public` folder makes it accessible to the outside world.
35-
Consider blocking requests to this file or finding a more appropriate location which your backend can access.
36-
37-
</Alert>
38-
39-
40-
```javascript {filename:public/instrument.server.mjs}
31+
```javascript {filename:src/instrument.server.ts}
4132
import * as Sentry from '@sentry/solidstart';
4233

4334
Sentry.init({
@@ -46,13 +37,31 @@ Sentry.init({
4637
});
4738
```
4839

40+
Wrap your SolidStart config with `withSentry`, so this file gets added to your build output. With the default server preset, you can find the file here: `.output/server/instrument.server.mjs`.
41+
42+
```javascript {filename:app.config.ts} {5-12}
43+
import { withSentry } from '@sentry/solidstart';
44+
import { defineConfig } from '@solidjs/start/config';
45+
46+
export default defineConfig(
47+
withSentry(
48+
{
49+
/* Your SolidStart config */
50+
},
51+
{
52+
/* Your Sentry build-time config (such as source map upload options) */
53+
}
54+
),
55+
);
56+
```
57+
4958
### Instrumentation
5059

5160
The Sentry SDK provides [middleware lifecycle](https://docs.solidjs.com/solid-start/advanced/middleware) handlers to enhance data collected by Sentry on the server side by enabling distributed tracing between the client and server.
5261

5362
Complete the setup by adding `sentryBeforeResponseMiddleware` to your `src/middleware.ts` file. If you don't have a `src/middleware.ts` file yet, create one:
5463

55-
```typescript {filename:src/middleware.ts}
64+
```typescript {filename:src/middleware.ts} {6}
5665
import { sentryBeforeResponseMiddleware } from '@sentry/solidstart';
5766
import { createMiddleware } from '@solidjs/start/middleware';
5867

@@ -66,19 +75,22 @@ export default createMiddleware({
6675

6776
And specify `src/middleware.ts` in `app.config.ts`:
6877

69-
```typescript {filename:app.config.ts}
78+
```typescript {filename:app.config.ts} {7}
79+
import { withSentry } from '@sentry/solidstart';
7080
import { defineConfig } from '@solidjs/start/config';
7181

72-
export default defineConfig({
73-
// ...
74-
middleware: './src/middleware.ts',
75-
});
82+
export default defineConfig(
83+
withSentry({
84+
// other SolidStart config options...
85+
middleware: './src/middleware.ts',
86+
})
87+
);
7688
```
7789

7890
If you previously added the `solidRouterBrowserTracingIntegration` integration to `Sentry.init` in `src/entry-client.tsx`, wrap your Solid Router with `withSentryRouterRouting`.
7991
This creates a higher order component, which will enable Sentry to collect navigation spans.
8092

81-
```tsx {filename:app.tsx}
93+
```tsx {filename:app.tsx} {5,9,11}
8294
import { Router } from "@solidjs/router";
8395
import { FileRoutes } from "@solidjs/start/router";
8496
import { withSentryRouterRouting } from '@sentry/solidstart/solidrouter'
@@ -96,13 +108,22 @@ export default function App() {
96108

97109
### Run your application
98110

99-
Add an `--import` flag to the `NODE_OPTIONS` environment variable wherever you run your application to import `public/instrument.server.mjs`.
111+
Add an `--import` flag directly or to the `NODE_OPTIONS` environment variable wherever you run your application to import `.output/server/instrument.server.mjs `.
100112

101113
For example, update your scripts in `package.json`.
102114

103115
```json {filename:package.json}
104116
{
105117
"scripts": {
106-
"start": "NODE_OPTIONS='--import ./public/instrument.server.mjs' vinxi start",
118+
"start:vinxi": "NODE_OPTIONS='--import ./.output/server/instrument.server.mjs ' vinxi start",
119+
"start:node": "node --import ./.output/server/instrument.server.mjs .output/server/index.mjs"
107120
}
108121
}
122+
```
123+
124+
<Alert level="warning">
125+
If you experience any issues during the server-side setup, read through the different <PlatformLink to="/install">installation methods</PlatformLink>
126+
or check out <PlatformLink to="/troubleshooting">Troubleshooting</PlatformLink>.
127+
</Alert>
128+
129+

0 commit comments

Comments
 (0)