Skip to content

fix(remix): Use import() to get react-router-dom in Express wrapper. #5810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/remix/src/utils/serverAdapters/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { getCurrentHub } from '@sentry/hub';
import { flush } from '@sentry/node';
import { hasTracingEnabled } from '@sentry/tracing';
import { Transaction } from '@sentry/types';
import { extractRequestData, isString, loadModule, logger } from '@sentry/utils';
import { extractRequestData, isString, logger } from '@sentry/utils';
import { cwd } from 'process';

import {
createRoutes,
Expand All @@ -22,12 +23,13 @@ import {
ServerBuild,
} from '../types';

let pkg: ReactRouterDomPkg;

function wrapExpressRequestHandler(
origRequestHandler: ExpressRequestHandler,
build: ServerBuild,
): ExpressRequestHandler {
const routes = createRoutes(build.routes);
const pkg = loadModule<ReactRouterDomPkg>('react-router-dom');

// If the core request handler is already wrapped, don't wrap Express handler which uses it.
if (isRequestHandlerWrapped) {
Expand All @@ -40,6 +42,10 @@ function wrapExpressRequestHandler(
res: ExpressResponse,
next: ExpressNextFunction,
): Promise<void> {
if (!pkg) {
pkg = await import(`${cwd()}/node_modules/react-router-dom`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will is break if folks are bundling stuff (like in a serverless function)?

Can we just call import directly on react-router-dom?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will is break if folks are bundling stuff (like in a serverless function)?

I think it depends on the packager used, but it seems there are some workarounds if such thing happens: https://www.serverless.com/plugins/serverless-jetpack#handling-dynamic-import-misses. It also applies to loadModule (using module.require) as far as I understand.

Can we just call import directly on react-router-dom?

Tried that and it's working, but while building @sentry/remix I got a warning from Rollup because, it actually tried to resolve the deps from sentry-javascript/node_modules on build time.

}

// eslint-disable-next-line @typescript-eslint/unbound-method
res.end = wrapEndMethod(res.end);

Expand Down