Skip to content

Commit 8481d1b

Browse files
committed
ref(remix): Make @remix-run/router a dependency.
1 parent 22ebb3b commit 8481d1b

File tree

6 files changed

+23
-45
lines changed

6 files changed

+23
-45
lines changed

packages/remix/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"access": "public"
3535
},
3636
"dependencies": {
37+
"@remix-run/router": "1.x",
3738
"@sentry/cli": "^2.28.5",
3839
"@sentry/core": "7.100.0",
3940
"@sentry/node": "7.100.0",

packages/remix/src/utils/instrumentServer.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import type {
4646
EntryContext,
4747
FutureConfig,
4848
HandleDocumentRequestFunction,
49-
ReactRouterDomPkg,
5049
RemixRequest,
5150
RequestHandler,
5251
ServerBuild,
@@ -383,10 +382,6 @@ export function createRoutes(manifest: ServerRouteManifest, parentId?: string):
383382

384383
/**
385384
* Starts a new transaction for the given request to be used by different `RequestHandler` wrappers.
386-
*
387-
* @param request
388-
* @param routes
389-
* @param pkg
390385
*/
391386
export function startRequestHandlerTransaction(
392387
hub: Hub,
@@ -434,19 +429,14 @@ export function startRequestHandlerTransaction(
434429
/**
435430
* Get transaction name from routes and url
436431
*/
437-
export function getTransactionName(
438-
routes: ServerRoute[],
439-
url: URL,
440-
pkg?: ReactRouterDomPkg,
441-
): [string, TransactionSource] {
442-
const matches = matchServerRoutes(routes, url.pathname, pkg);
432+
export function getTransactionName(routes: ServerRoute[], url: URL): [string, TransactionSource] {
433+
const matches = matchServerRoutes(routes, url.pathname);
443434
const match = matches && getRequestMatch(url, matches);
444-
return match === null ? [url.pathname, 'url'] : [match.route.id, 'route'];
435+
return match === null ? [url.pathname, 'url'] : [match.route.id || 'no-route-id', 'route'];
445436
}
446437

447438
function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBuild): RequestHandler {
448439
const routes = createRoutes(build.routes);
449-
const pkg = loadModule<ReactRouterDomPkg>('react-router-dom');
450440

451441
return async function (this: unknown, request: RemixRequest, loadContext?: AppLoadContext): Promise<Response> {
452442
// This means that the request handler of the adapter (ex: express) is already wrapped.
@@ -469,7 +459,7 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
469459
}
470460

471461
const url = new URL(request.url);
472-
const [name, source] = getTransactionName(routes, url, pkg);
462+
const [name, source] = getTransactionName(routes, url);
473463

474464
isolationScope.setSDKProcessingMetadata({
475465
request: {

packages/remix/src/utils/serverAdapters/express.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { getClient, getCurrentHub, hasTracingEnabled, setHttpStatus, withIsolati
22
import { flush } from '@sentry/node';
33
import type { Transaction } from '@sentry/types';
44
import { extractRequestData, fill, isString, logger } from '@sentry/utils';
5-
import { cwd } from 'process';
65

76
import { DEBUG_BUILD } from '../debug-build';
87
import { createRoutes, getTransactionName, instrumentBuild, startRequestHandlerTransaction } from '../instrumentServer';
@@ -15,12 +14,9 @@ import type {
1514
ExpressRequestHandler,
1615
ExpressResponse,
1716
GetLoadContextFunction,
18-
ReactRouterDomPkg,
1917
ServerBuild,
2018
} from '../vendor/types';
2119

22-
let pkg: ReactRouterDomPkg;
23-
2420
function wrapExpressRequestHandler(
2521
origRequestHandler: ExpressRequestHandler,
2622
build: ServerBuild,
@@ -33,18 +29,6 @@ function wrapExpressRequestHandler(
3329
res: ExpressResponse,
3430
next: ExpressNextFunction,
3531
): Promise<void> {
36-
if (!pkg) {
37-
try {
38-
pkg = await import('react-router-dom');
39-
} catch (e) {
40-
pkg = await import(`${cwd()}/node_modules/react-router-dom`);
41-
} finally {
42-
if (!pkg) {
43-
DEBUG_BUILD && logger.error('Could not find `react-router-dom` package.');
44-
}
45-
}
46-
}
47-
4832
await withIsolationScope(async isolationScope => {
4933
// eslint-disable-next-line @typescript-eslint/unbound-method
5034
res.end = wrapEndMethod(res.end);
@@ -62,7 +46,7 @@ function wrapExpressRequestHandler(
6246

6347
const url = new URL(request.url);
6448

65-
const [name, source] = getTransactionName(routes, url, pkg);
49+
const [name, source] = getTransactionName(routes, url);
6650
const transaction = startRequestHandlerTransaction(hub, name, source, {
6751
headers: {
6852
'sentry-trace': (req.headers && isString(req.headers['sentry-trace']) && req.headers['sentry-trace']) || '',

packages/remix/src/utils/vendor/response.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
//
77
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
88

9-
import type { DeferredData, ErrorResponse, ReactRouterDomPkg, RouteMatch, ServerRoute } from './types';
9+
import { matchRoutes } from '@remix-run/router';
10+
import type { AgnosticRouteMatch, AgnosticRouteObject } from '@remix-run/router';
11+
import type { DeferredData, ErrorResponse, ServerRoute } from './types';
1012

1113
/**
1214
* Based on Remix Implementation
@@ -76,13 +78,9 @@ export const json: JsonFunction = (data, init = {}) => {
7678
export function matchServerRoutes(
7779
routes: ServerRoute[],
7880
pathname: string,
79-
pkg?: ReactRouterDomPkg,
80-
): RouteMatch<ServerRoute>[] | null {
81-
if (!pkg) {
82-
return null;
83-
}
81+
): AgnosticRouteMatch<string, AgnosticRouteObject>[] | null {
82+
const matches = matchRoutes(routes, pathname);
8483

85-
const matches = pkg.matchRoutes(routes, pathname);
8684
if (!matches) {
8785
return null;
8886
}
@@ -91,6 +89,7 @@ export function matchServerRoutes(
9189
params: match.params,
9290
pathname: match.pathname,
9391
route: match.route,
92+
pathnameBase: match.pathnameBase,
9493
}));
9594
}
9695

@@ -115,10 +114,13 @@ export function isIndexRequestUrl(url: URL): boolean {
115114
/**
116115
* https://github.com/remix-run/remix/blob/97999d02493e8114c39d48b76944069d58526e8d/packages/remix-server-runtime/server.ts#L588-L596
117116
*/
118-
export function getRequestMatch(url: URL, matches: RouteMatch<ServerRoute>[]): RouteMatch<ServerRoute> {
117+
export function getRequestMatch(
118+
url: URL,
119+
matches: AgnosticRouteMatch[],
120+
): AgnosticRouteMatch<string, AgnosticRouteObject> {
119121
const match = matches.slice(-1)[0];
120122

121-
if (!isIndexRequestUrl(url) && match.route.id.endsWith('/index')) {
123+
if (!isIndexRequestUrl(url) && match.route.id?.endsWith('/index')) {
122124
return matches.slice(-2)[0];
123125
}
124126

packages/remix/src/utils/vendor/types.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export type ExpressResponse = Express.Response;
7878
export type ExpressNextFunction = Express.NextFunction;
7979

8080
export interface Route {
81-
index?: boolean;
81+
index: false | undefined;
8282
caseSensitive?: boolean;
8383
id: string;
8484
parentId?: string;
@@ -210,10 +210,6 @@ export interface DataFunction {
210210
(args: DataFunctionArgs): Promise<Response> | Response | Promise<AppData> | AppData;
211211
}
212212

213-
export interface ReactRouterDomPkg {
214-
matchRoutes: (routes: ServerRoute[], pathname: string) => RouteMatch<ServerRoute>[] | null;
215-
}
216-
217213
// Taken from Remix Implementation
218214
// https://github.com/remix-run/remix/blob/97999d02493e8114c39d48b76944069d58526e8d/packages/remix-server-runtime/routeMatching.ts#L6-L10
219215
export interface RouteMatch<Route> {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5239,6 +5239,11 @@
52395239
resolved "https://registry.yarnpkg.com/@remix-run/router/-/router-1.0.2.tgz#1c17eadb2fa77f80a796ad5ea9bf108e6993ef06"
52405240
integrity sha512-GRSOFhJzjGN+d4sKHTMSvNeUPoZiDHWmRnXfzaxrqe7dE/Nzlc8BiMSJdLDESZlndM7jIUrZ/F4yWqVYlI0rwQ==
52415241

5242+
"@remix-run/[email protected]":
5243+
version "1.15.0"
5244+
resolved "https://registry.yarnpkg.com/@remix-run/router/-/router-1.15.0.tgz#461a952c2872dd82c8b2e9b74c4dfaff569123e2"
5245+
integrity sha512-HOil5aFtme37dVQTB6M34G95kPM3MMuqSmIRVCC52eKV+Y/tGSqw9P3rWhlAx6A+mz+MoX+XxsGsNJbaI5qCgQ==
5246+
52425247
"@remix-run/[email protected]":
52435248
version "1.5.1"
52445249
resolved "https://registry.yarnpkg.com/@remix-run/server-runtime/-/server-runtime-1.5.1.tgz#5272b01e6dce109dc10bd68447ceae2d039315b2"

0 commit comments

Comments
 (0)