diff --git a/packages/remix/package.json b/packages/remix/package.json index fdb216e483df..b6962cc8b86f 100644 --- a/packages/remix/package.json +++ b/packages/remix/package.json @@ -34,6 +34,7 @@ "access": "public" }, "dependencies": { + "@remix-run/router": "1.x", "@sentry/cli": "^2.28.0", "@sentry/core": "7.102.0", "@sentry/node": "7.102.0", diff --git a/packages/remix/src/utils/instrumentServer.ts b/packages/remix/src/utils/instrumentServer.ts index 94e8090ac433..1283f98fec1a 100644 --- a/packages/remix/src/utils/instrumentServer.ts +++ b/packages/remix/src/utils/instrumentServer.ts @@ -47,7 +47,6 @@ import type { EntryContext, FutureConfig, HandleDocumentRequestFunction, - ReactRouterDomPkg, RemixRequest, RequestHandler, ServerBuild, @@ -384,10 +383,6 @@ export function createRoutes(manifest: ServerRouteManifest, parentId?: string): /** * Starts a new transaction for the given request to be used by different `RequestHandler` wrappers. - * - * @param request - * @param routes - * @param pkg */ export function startRequestHandlerTransaction( hub: Hub, @@ -435,19 +430,14 @@ export function startRequestHandlerTransaction( /** * Get transaction name from routes and url */ -export function getTransactionName( - routes: ServerRoute[], - url: URL, - pkg?: ReactRouterDomPkg, -): [string, TransactionSource] { - const matches = matchServerRoutes(routes, url.pathname, pkg); +export function getTransactionName(routes: ServerRoute[], url: URL): [string, TransactionSource] { + const matches = matchServerRoutes(routes, url.pathname); const match = matches && getRequestMatch(url, matches); - return match === null ? [url.pathname, 'url'] : [match.route.id, 'route']; + return match === null ? [url.pathname, 'url'] : [match.route.id || 'no-route-id', 'route']; } function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBuild): RequestHandler { const routes = createRoutes(build.routes); - const pkg = loadModule('react-router-dom'); return async function (this: unknown, request: RemixRequest, loadContext?: AppLoadContext): Promise { // This means that the request handler of the adapter (ex: express) is already wrapped. @@ -471,7 +461,7 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui } const url = new URL(request.url); - const [name, source] = getTransactionName(routes, url, pkg); + const [name, source] = getTransactionName(routes, url); scope.setSDKProcessingMetadata({ request: { diff --git a/packages/remix/src/utils/serverAdapters/express.ts b/packages/remix/src/utils/serverAdapters/express.ts index 8f05de780381..db38c351c90e 100644 --- a/packages/remix/src/utils/serverAdapters/express.ts +++ b/packages/remix/src/utils/serverAdapters/express.ts @@ -9,7 +9,6 @@ import { import { flush } from '@sentry/node'; import type { Transaction } from '@sentry/types'; import { extractRequestData, fill, isString, logger } from '@sentry/utils'; -import { cwd } from 'process'; import { DEBUG_BUILD } from '../debug-build'; import { createRoutes, getTransactionName, instrumentBuild, startRequestHandlerTransaction } from '../instrumentServer'; @@ -22,12 +21,9 @@ import type { ExpressRequestHandler, ExpressResponse, GetLoadContextFunction, - ReactRouterDomPkg, ServerBuild, } from '../vendor/types'; -let pkg: ReactRouterDomPkg; - function wrapExpressRequestHandler( origRequestHandler: ExpressRequestHandler, build: ServerBuild, @@ -40,18 +36,6 @@ function wrapExpressRequestHandler( res: ExpressResponse, next: ExpressNextFunction, ): Promise { - if (!pkg) { - try { - pkg = await import('react-router-dom'); - } catch (e) { - pkg = await import(`${cwd()}/node_modules/react-router-dom`); - } finally { - if (!pkg) { - DEBUG_BUILD && logger.error('Could not find `react-router-dom` package.'); - } - } - } - await runWithAsyncContext(async () => { // eslint-disable-next-line @typescript-eslint/unbound-method res.end = wrapEndMethod(res.end); @@ -70,7 +54,7 @@ function wrapExpressRequestHandler( const url = new URL(request.url); - const [name, source] = getTransactionName(routes, url, pkg); + const [name, source] = getTransactionName(routes, url); const transaction = startRequestHandlerTransaction(hub, name, source, { headers: { 'sentry-trace': (req.headers && isString(req.headers['sentry-trace']) && req.headers['sentry-trace']) || '', diff --git a/packages/remix/src/utils/vendor/response.ts b/packages/remix/src/utils/vendor/response.ts index 5def5cd28d99..8a8421ab06fd 100644 --- a/packages/remix/src/utils/vendor/response.ts +++ b/packages/remix/src/utils/vendor/response.ts @@ -6,7 +6,9 @@ // // 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. -import type { DeferredData, ErrorResponse, ReactRouterDomPkg, RouteMatch, ServerRoute } from './types'; +import { matchRoutes } from '@remix-run/router'; +import type { AgnosticRouteMatch, AgnosticRouteObject } from '@remix-run/router'; +import type { DeferredData, ErrorResponse, ServerRoute } from './types'; /** * Based on Remix Implementation @@ -76,13 +78,9 @@ export const json: JsonFunction = (data, init = {}) => { export function matchServerRoutes( routes: ServerRoute[], pathname: string, - pkg?: ReactRouterDomPkg, -): RouteMatch[] | null { - if (!pkg) { - return null; - } +): AgnosticRouteMatch[] | null { + const matches = matchRoutes(routes, pathname); - const matches = pkg.matchRoutes(routes, pathname); if (!matches) { return null; } @@ -91,6 +89,7 @@ export function matchServerRoutes( params: match.params, pathname: match.pathname, route: match.route, + pathnameBase: match.pathnameBase, })); } @@ -115,10 +114,13 @@ export function isIndexRequestUrl(url: URL): boolean { /** * https://github.com/remix-run/remix/blob/97999d02493e8114c39d48b76944069d58526e8d/packages/remix-server-runtime/server.ts#L588-L596 */ -export function getRequestMatch(url: URL, matches: RouteMatch[]): RouteMatch { +export function getRequestMatch( + url: URL, + matches: AgnosticRouteMatch[], +): AgnosticRouteMatch { const match = matches.slice(-1)[0]; - if (!isIndexRequestUrl(url) && match.route.id.endsWith('/index')) { + if (!isIndexRequestUrl(url) && match.route.id?.endsWith('/index')) { return matches.slice(-2)[0]; } diff --git a/packages/remix/src/utils/vendor/types.ts b/packages/remix/src/utils/vendor/types.ts index c3041dd4805e..e3a3fa42fbea 100644 --- a/packages/remix/src/utils/vendor/types.ts +++ b/packages/remix/src/utils/vendor/types.ts @@ -78,7 +78,7 @@ export type ExpressResponse = Express.Response; export type ExpressNextFunction = Express.NextFunction; export interface Route { - index?: boolean; + index: false | undefined; caseSensitive?: boolean; id: string; parentId?: string; @@ -210,10 +210,6 @@ export interface DataFunction { (args: DataFunctionArgs): Promise | Response | Promise | AppData; } -export interface ReactRouterDomPkg { - matchRoutes: (routes: ServerRoute[], pathname: string) => RouteMatch[] | null; -} - // Taken from Remix Implementation // https://github.com/remix-run/remix/blob/97999d02493e8114c39d48b76944069d58526e8d/packages/remix-server-runtime/routeMatching.ts#L6-L10 export interface RouteMatch { diff --git a/yarn.lock b/yarn.lock index ddf84ad0e320..4eba7bf0fa1f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5239,6 +5239,11 @@ resolved "https://registry.yarnpkg.com/@remix-run/router/-/router-1.0.2.tgz#1c17eadb2fa77f80a796ad5ea9bf108e6993ef06" integrity sha512-GRSOFhJzjGN+d4sKHTMSvNeUPoZiDHWmRnXfzaxrqe7dE/Nzlc8BiMSJdLDESZlndM7jIUrZ/F4yWqVYlI0rwQ== +"@remix-run/router@1.x": + version "1.15.0" + resolved "https://registry.yarnpkg.com/@remix-run/router/-/router-1.15.0.tgz#461a952c2872dd82c8b2e9b74c4dfaff569123e2" + integrity sha512-HOil5aFtme37dVQTB6M34G95kPM3MMuqSmIRVCC52eKV+Y/tGSqw9P3rWhlAx6A+mz+MoX+XxsGsNJbaI5qCgQ== + "@remix-run/server-runtime@1.5.1": version "1.5.1" resolved "https://registry.yarnpkg.com/@remix-run/server-runtime/-/server-runtime-1.5.1.tgz#5272b01e6dce109dc10bd68447ceae2d039315b2"