Skip to content

Commit e62e166

Browse files
committed
Read Remix version from dependency module.
1 parent 30f98a0 commit e62e166

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

packages/remix/src/client/performance.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import type { ErrorBoundaryProps } from '@sentry/react';
2-
import { getCurrentHub, WINDOW, withErrorBoundary } from '@sentry/react';
2+
import { WINDOW, withErrorBoundary } from '@sentry/react';
33
import type { Transaction, TransactionContext } from '@sentry/types';
44
import { isNodeEnv, logger } from '@sentry/utils';
55
import * as React from 'react';
66

7-
import { getFutureFlagsBrowser } from '../utils/futureFlags';
8-
import type { RemixOptions } from '../utils/remixOptions';
7+
import { getRemixVersion } from '../utils/futureFlags';
98

109
const DEFAULT_TAGS = {
1110
'routing.instrumentation': 'remix-router',
@@ -41,13 +40,6 @@ let _useMatches: UseMatches;
4140
let _customStartTransaction: (context: TransactionContext) => Transaction | undefined;
4241
let _startTransactionOnLocationChange: boolean;
4342

44-
function isRemixV2(): boolean {
45-
const client = getCurrentHub().getClient();
46-
const opt = client && (client.getOptions() as RemixOptions);
47-
48-
return (opt && opt.isRemixV2) || getFutureFlagsBrowser()?.v2_errorBoundary || false;
49-
}
50-
5143
function getInitPathName(): string | undefined {
5244
if (WINDOW && WINDOW.location) {
5345
return WINDOW.location.pathname;
@@ -109,6 +101,9 @@ export function withSentry<P extends Record<string, unknown>, R extends React.FC
109101
errorBoundaryOptions: {},
110102
},
111103
): R {
104+
// This part runs on the server-side so it's safe to use `getRemixVersion` here.
105+
const remixVersion = getRemixVersion();
106+
112107
const SentryRoot: React.FC<P> = (props: P) => {
113108
// Early return when any of the required functions is not available.
114109
if (!_useEffect || !_useLocation || !_useMatches || !_customStartTransaction) {
@@ -162,7 +157,7 @@ export function withSentry<P extends Record<string, unknown>, R extends React.FC
162157

163158
isBaseLocation = false;
164159

165-
if (!isRemixV2() && options.wrapWithErrorBoundary) {
160+
if (remixVersion >= 2 && options.wrapWithErrorBoundary) {
166161
// @ts-ignore Setting more specific React Component typing for `R` generic above
167162
// will break advanced type inference done by react router params
168163
return withErrorBoundary(OrigApp, options.errorBoundaryOptions)(props);

packages/remix/src/index.client.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-disable import/export */
2-
import type { BrowserOptions } from '@sentry/react';
32
import { configureScope, init as reactInit } from '@sentry/react';
43

54
import { buildMetadata } from './utils/metadata';
@@ -12,7 +11,7 @@ export function init(options: RemixOptions): void {
1211
buildMetadata(options, ['remix', 'react']);
1312
options.environment = options.environment || process.env.NODE_ENV;
1413

15-
reactInit(options as BrowserOptions);
14+
reactInit(options);
1615

1716
configureScope(scope => {
1817
scope.setTag('runtime', 'browser');

packages/remix/src/index.server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { NodeOptions } from '@sentry/node';
33
import { configureScope, getCurrentHub, init as nodeInit } from '@sentry/node';
44
import { logger } from '@sentry/utils';
55

6+
import { getRemixVersion } from './utils/futureFlags';
67
import { instrumentServer } from './utils/instrumentServer';
78
import { buildMetadata } from './utils/metadata';
89
import type { RemixOptions } from './utils/remixOptions';
@@ -76,7 +77,7 @@ export function init(options: RemixOptions): void {
7677
return;
7778
}
7879

79-
instrumentServer(options.isRemixV2);
80+
instrumentServer(getRemixVersion() >= 2);
8081

8182
nodeInit(options as NodeOptions);
8283

packages/remix/src/utils/futureFlags.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GLOBAL_OBJ } from '@sentry/utils';
1+
import { GLOBAL_OBJ, loadModule, parseSemver } from '@sentry/utils';
22

33
import type { FutureConfig, ServerBuild } from './vendor/types';
44

@@ -33,3 +33,14 @@ export function getFutureFlagsBrowser(): FutureConfig | undefined {
3333
export function getFutureFlagsServer(build: ServerBuild): FutureConfig | undefined {
3434
return build.future;
3535
}
36+
37+
/**
38+
* Read Remix version from module package.json
39+
*
40+
* @returns The version string
41+
*/
42+
export function getRemixVersion(): number {
43+
const pkg = loadModule<{ version: string }>('@remix-run/dev/package.json');
44+
const version = pkg ? pkg.version : '0.0.0';
45+
return Number(parseSemver(version).major) || 0;
46+
}

packages/remix/src/utils/remixOptions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ import type { NodeOptions } from '@sentry/node';
22
import type { BrowserOptions } from '@sentry/react';
33
import type { Options } from '@sentry/types';
44

5-
export type RemixOptions = (Options | BrowserOptions | NodeOptions) & {
6-
isRemixV2?: boolean;
7-
};
5+
export type RemixOptions = Options | BrowserOptions | NodeOptions;

0 commit comments

Comments
 (0)