Skip to content

Commit 91c7f07

Browse files
authored
ref(remix): Make @remix-run/router a dependency (v7) (#10779)
Backports #10479 to v7 branch Original PR Description: > 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 046422b commit 91c7f07

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.0",
3839
"@sentry/core": "7.102.0",
3940
"@sentry/node": "7.102.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.
@@ -471,7 +461,7 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
471461
}
472462

473463
const url = new URL(request.url);
474-
const [name, source] = getTransactionName(routes, url, pkg);
464+
const [name, source] = getTransactionName(routes, url);
475465

476466
scope.setSDKProcessingMetadata({
477467
request: {

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import { flush } from '@sentry/node';
1010
import type { Transaction } from '@sentry/types';
1111
import { extractRequestData, fill, isString, logger } from '@sentry/utils';
12-
import { cwd } from 'process';
1312

1413
import { DEBUG_BUILD } from '../debug-build';
1514
import { createRoutes, getTransactionName, instrumentBuild, startRequestHandlerTransaction } from '../instrumentServer';
@@ -22,12 +21,9 @@ import type {
2221
ExpressRequestHandler,
2322
ExpressResponse,
2423
GetLoadContextFunction,
25-
ReactRouterDomPkg,
2624
ServerBuild,
2725
} from '../vendor/types';
2826

29-
let pkg: ReactRouterDomPkg;
30-
3127
function wrapExpressRequestHandler(
3228
origRequestHandler: ExpressRequestHandler,
3329
build: ServerBuild,
@@ -40,18 +36,6 @@ function wrapExpressRequestHandler(
4036
res: ExpressResponse,
4137
next: ExpressNextFunction,
4238
): Promise<void> {
43-
if (!pkg) {
44-
try {
45-
pkg = await import('react-router-dom');
46-
} catch (e) {
47-
pkg = await import(`${cwd()}/node_modules/react-router-dom`);
48-
} finally {
49-
if (!pkg) {
50-
DEBUG_BUILD && logger.error('Could not find `react-router-dom` package.');
51-
}
52-
}
53-
}
54-
5539
await runWithAsyncContext(async () => {
5640
// eslint-disable-next-line @typescript-eslint/unbound-method
5741
res.end = wrapEndMethod(res.end);
@@ -70,7 +54,7 @@ function wrapExpressRequestHandler(
7054

7155
const url = new URL(request.url);
7256

73-
const [name, source] = getTransactionName(routes, url, pkg);
57+
const [name, source] = getTransactionName(routes, url);
7458
const transaction = startRequestHandlerTransaction(hub, name, source, {
7559
headers: {
7660
'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)