Skip to content

Commit 674a8f0

Browse files
authored
ref(remix): Make @remix-run/router a dependency. (#10479)
Fixes: #10349 Related: #5860 Related: #10458 Removes dynamic loading of `react-router-dom` and makes `@remix-run/router` a peer dependency. We don't need to dynamically load `react-router-dom` as our TypeScript version is now up-to-date.
1 parent c4acfe9 commit 674a8f0

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.6",
3839
"@sentry/core": "7.100.0",
3940
"@sentry/node-experimental": "7.100.0",

packages/remix/src/utils/instrumentServer.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import type {
4747
EntryContext,
4848
FutureConfig,
4949
HandleDocumentRequestFunction,
50-
ReactRouterDomPkg,
5150
RemixRequest,
5251
RequestHandler,
5352
ServerBuild,
@@ -384,10 +383,6 @@ export function createRoutes(manifest: ServerRouteManifest, parentId?: string):
384383

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

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

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

472462
const url = new URL(request.url);
473-
const [name, source] = getTransactionName(routes, url, pkg);
463+
const [name, source] = getTransactionName(routes, url);
474464

475465
isolationScope.setSDKProcessingMetadata({
476466
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-experimental';
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
@@ -5285,6 +5285,11 @@
52855285
resolved "https://registry.yarnpkg.com/@remix-run/router/-/router-1.0.2.tgz#1c17eadb2fa77f80a796ad5ea9bf108e6993ef06"
52865286
integrity sha512-GRSOFhJzjGN+d4sKHTMSvNeUPoZiDHWmRnXfzaxrqe7dE/Nzlc8BiMSJdLDESZlndM7jIUrZ/F4yWqVYlI0rwQ==
52875287

5288+
"@remix-run/[email protected]":
5289+
version "1.15.0"
5290+
resolved "https://registry.yarnpkg.com/@remix-run/router/-/router-1.15.0.tgz#461a952c2872dd82c8b2e9b74c4dfaff569123e2"
5291+
integrity sha512-HOil5aFtme37dVQTB6M34G95kPM3MMuqSmIRVCC52eKV+Y/tGSqw9P3rWhlAx6A+mz+MoX+XxsGsNJbaI5qCgQ==
5292+
52885293
"@remix-run/[email protected]":
52895294
version "1.5.1"
52905295
resolved "https://registry.yarnpkg.com/@remix-run/server-runtime/-/server-runtime-1.5.1.tgz#5272b01e6dce109dc10bd68447ceae2d039315b2"

0 commit comments

Comments
 (0)