Skip to content

Commit 4ca4ebd

Browse files
committed
ref(nextjs): Pass the route provider as the routeProvider option
1 parent 6df6d52 commit 4ca4ebd

4 files changed

Lines changed: 15 additions & 23 deletions

File tree

‎packages/nextjs/src/client/index.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
1212
import { browserTracingIntegration } from './browserTracingIntegration';
1313
import { nextjsClientStackFrameNormalizationIntegration } from './clientNormalizationIntegration';
1414
import { removeIsrSsgTraceMetaTags } from './routing/isrRoutingTracing';
15-
import { nextjsRouteProviderIntegration } from './routing/routeProvider';
15+
import { createNextRouteProvider } from './routing/routeProvider';
1616
import { applyTunnelRouteOption } from './tunnelRoute';
1717

1818
export * from '@sentry/react';
@@ -66,6 +66,9 @@ export function init(options: BrowserOptions): Client | undefined {
6666
environment: options.environment || process.env.SENTRY_ENVIRONMENT || getClientVercelEnv() || process.env.NODE_ENV,
6767
defaultIntegrations: getDefaultIntegrations(options),
6868
release: process.env._sentryRelease || globalWithInjectedValues._sentryRelease,
69+
// Both route manifests are injected at build time, so route parameterization works from `init` on,
70+
// including for the pageload span and with tracing disabled.
71+
routeProvider: createNextRouteProvider(),
6972
...options,
7073
} satisfies BrowserOptions;
7174

@@ -106,7 +109,6 @@ export function init(options: BrowserOptions): Client | undefined {
106109

107110
function getDefaultIntegrations(options: BrowserOptions): Integration[] {
108111
const customDefaultIntegrations = getReactDefaultIntegrations(options);
109-
customDefaultIntegrations.push(nextjsRouteProviderIntegration());
110112
// This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false",
111113
// in which case everything inside will get tree-shaken away
112114
if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) {
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { defineIntegration } from '@sentry/core';
21
import type { RouteProvider } from '@sentry/react';
3-
import { createUrlRouteProvider, setRouteProvider } from '@sentry/react';
2+
import { createUrlRouteProvider } from '@sentry/react';
43
import { maybeParameterizeRoute, stripBasePath, stripTrailingSlash } from './parameterization';
54
import { getNextRouteFromPathname } from './pagesRouterRoutingInstrumentation';
65

@@ -25,18 +24,3 @@ function resolveNextRoute(url: { pathname: string }): string | undefined {
2524
export function createNextRouteProvider(): RouteProvider {
2625
return createUrlRouteProvider(resolveNextRoute);
2726
}
28-
29-
/**
30-
* Registers the Next.js route provider.
31-
*
32-
* An integration rather than part of `browserTracingIntegration` so route parameterization does not depend
33-
* on tracing: the route manifests are injected at build time, so anything that needs a route name (bfcache
34-
* metrics, web vitals) can resolve one even with tracing disabled. Registered in `setup` because the pageload
35-
* span is named in `browserTracingIntegration`'s `afterAllSetup`.
36-
*/
37-
export const nextjsRouteProviderIntegration = defineIntegration(() => ({
38-
name: 'NextjsRouteProvider',
39-
setup(client) {
40-
setRouteProvider(createNextRouteProvider(), client);
41-
},
42-
}));

‎packages/nextjs/test/client/appRouterRoutingInstrumentation.test.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ async function setup(traceLifecycle: 'stream' | 'static'): Promise<{
7979
stackParser: () => [],
8080
tracesSampleRate: 1,
8181
traceLifecycle,
82-
integrations: [
83-
routeProvider.nextjsRouteProviderIntegration(),
84-
react.browserTracingIntegration({ instrumentPageLoad: false, instrumentNavigation: false }),
85-
],
82+
routeProvider: routeProvider.createNextRouteProvider(),
83+
integrations: [react.browserTracingIntegration({ instrumentPageLoad: false, instrumentNavigation: false })],
8684
});
8785
core.setCurrentClient(client);
8886
client.init();

‎packages/nextjs/test/clientSdk.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ describe('Client init()', () => {
210210
delete globalWithManifest._sentryRouteManifest;
211211
});
212212

213+
it('keeps a route provider passed by the user', () => {
214+
const routeProvider = { resolveRoute: () => '/custom', resolveCurrentRoute: () => '/custom' };
215+
216+
init({ dsn: TEST_DSN, routeProvider });
217+
218+
expect(reactInit).toHaveBeenCalledWith(expect.objectContaining({ routeProvider }));
219+
});
220+
213221
it("doesn't run Next.js router instrumentation for bot user agents", () => {
214222
Object.defineProperty(WINDOW, 'navigator', {
215223
value: {

0 commit comments

Comments
 (0)