Skip to content

Commit 1144e30

Browse files
authored
Address Luca's comments in micro-frontend-support.mdx
1 parent 8954cd2 commit 1144e30

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

docs/platforms/javascript/common/configuration/micro-frontend-support.mdx

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ keywords:
1414
If your frontend includes JavaScript bundles from multiple sources with
1515
different release cycles, you may want to identify these or route events to specific projects. This is especially useful if you've set up [module federation](https://module-federation.github.io/) or a similar frontend architecture.
1616

17-
Below we offer two approaches. Please note that `Sentry.init()` must be called only once, doing otherwise will result in undefined behavior.
17+
Below we offer two approaches.
18+
19+
<Note>
20+
In all cases `Sentry.init()` must never be called more than once, doing so will result in undefined behavior.
21+
</Note>
1822

1923
## Automatically Route Errors to Different Projects Depending on Module
2024

@@ -36,7 +40,8 @@ Requires SDK version `7.59.0` or higher.
3640

3741
First, to identify the source of an error, you must inject metadata that helps identify
3842
which bundles were responsible for the error. You can do this with any of the
39-
Sentry bundler plugins by enabling the `_experiments.moduleMetadata` option.
43+
Sentry bundler plugins by enabling the `_experiments.moduleMetadata` option. Example
44+
below is for Webpack, but this is also supported in Vite, Rollup, and esbuild.
4045

4146
```javascript
4247
// webpack.config.js
@@ -66,26 +71,42 @@ for the multiplexed transport to reference for routing.
6671
In practice, here is what your Sentry initialization should look like:
6772

6873
```javascript
69-
import { init, makeFetchTransport, moduleMetadataIntegration, makeSimpleMultiplexedTransport, MULTIPLEXED_TRANSPORT_EXTRA_KEY } from "@sentry/browser";
74+
import { init, makeFetchTransport, moduleMetadataIntegration, makeMultiplexedTransport } from "@sentry/browser";
75+
76+
const EXTRA_KEY = "ROUTE_TO";
77+
78+
const transport = makeMultiplexedTransport(makeFetchTransport, (args) => {
79+
const event = args.getEvent();
80+
if (
81+
event &&
82+
event.extra &&
83+
EXTRA_KEY in event.extra &&
84+
Array.isArray(event.extra[EXTRA_KEY])
85+
) {
86+
return event.extra[EXTRA_KEY];
87+
}
88+
return [];
89+
});
7090

7191
init({
7292
dsn: "__DEFAULT_DSN__",
7393
integrations: [moduleMetadataIntegration()],
74-
makeMultiplexedTransport(makeFetchTransport),
94+
transport,
7595
beforeSend: (event) => {
7696
if (event?.exception?.values?.[0].stacktrace.frames) {
7797
const frames = event.exception.values[0].stacktrace.frames;
78-
7998
// Find the last frame with module metadata containing a DSN
80-
const route_to = frames
99+
const routeTo = frames
81100
.filter((frame) => frame.module_metadata && frame.module_metadata.dsn)
82101
.map((v) => v.module_metadata)
83102
.slice(-1); // using top frame only - you may want to customize this according to your needs
84103

85-
event.extra = {
86-
...event.extra,
87-
[MULTIPLEXED_TRANSPORT_EXTRA_KEY]: route_to,
88-
};
104+
if (routeTo.length) {
105+
event.extra = {
106+
...event.extra,
107+
[EXTRA_KEY]: routeTo,
108+
};
109+
}
89110
}
90111

91112
return event;

0 commit comments

Comments
 (0)