Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 26 additions & 6 deletions packages/core/src/node/utils/reactAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ async function detectPackageMajorVersion(
return undefined;
}

export async function resolveReactRouterDomAlias(): Promise<
Record<string, string>
> {
const hasInstalled = await detectPackageMajorVersion('react-router-dom');
const basedir = hasInstalled ? process.cwd() : PACKAGE_ROOT;
export async function resolveReactRouterDomAlias(): Promise<{
[key: string]: string;
}> {
const majorVersion = await detectPackageMajorVersion('react-router-dom');
const basedir = majorVersion ? process.cwd() : PACKAGE_ROOT;

const alias: Record<string, string> = {};
const resolver = new Resolver({
Expand All @@ -49,8 +49,28 @@ export async function resolveReactRouterDomAlias(): Promise<
if (!resolved.path) {
throw Error(`'react-router-dom' resolved to empty path`);
}

const routerDomDir = path.dirname(resolved.path);

// In react-router-dom v6, StaticRouter is exported from
// 'react-router-dom/server', not the main entry. For SSR environments,
// we create a shim that re-exports everything from the main entry plus
// StaticRouter from the server subpath.
Comment on lines +57 to +58
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

The comment says “we create a shim that re-exports everything from the main entry plus StaticRouter from the server subpath”, but the implementation only creates an alias to server.mjs (no shim/re-export layer). Either implement the described shim, or update the comment to accurately describe the alias behavior so future readers aren’t misled.

Suggested change
// we create a shim that re-exports everything from the main entry plus
// StaticRouter from the server subpath.
// we resolve 'react-router-dom' to its package directory and add a
// separate alias pointing directly to the 'server.mjs' entry when present.

Copilot uses AI. Check for mistakes.
if (majorVersion && majorVersion < 7) {
const serverMjsPath = path.join(routerDomDir, 'server.mjs');
const hasServerMjs = await pathExists(serverMjsPath);

if (hasServerMjs) {
return {
'react-router-dom': routerDomDir,
REACT_ROUTER_DOM_SERVER: path.join(routerDomDir, 'server.mjs'),
};
}
}

return {
'react-router-dom': path.dirname(resolved.path),
'react-router-dom': routerDomDir,
REACT_ROUTER_DOM_SERVER: routerDomDir,
};
} catch (e) {
logger.warn('react-router-dom not found: \n', e);
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/runtime/ssrMdServerEntry.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// biome-ignore lint/suspicious/noTsIgnore: bundleless
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

The biome-ignore lint/suspicious/noTsIgnore comment no longer matches the code (this file now uses @ts-expect-error, not @ts-ignore). Either remove the Biome ignore or update it to the relevant rule to avoid carrying stale lint suppressions.

Suggested change
// biome-ignore lint/suspicious/noTsIgnore: bundleless
// biome-ignore lint/suspicious/noTsExpectError: bundleless

Copilot uses AI. Check for mistakes.
// @ts-ignore
// @ts-expect-error
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

There is a standalone // @ts-expect-error directive followed by a blank line, so it won’t apply to the import below and will likely trigger TypeScript’s “Unused '@ts-expect-error' directive” error. Remove this directive (or move it to directly precede the specific line that errors with no blank line).

Suggested change
// @ts-expect-error

Copilot uses AI. Check for mistakes.

// @ts-expect-error in v7, StaticRouter is exported from the main entry, but in v6 it's exported from 'react-router-dom/server'
import { StaticRouter } from 'REACT_ROUTER_DOM_SERVER';
import { renderToMarkdownString } from '@rspress/core/_private/react';
import {
PageContext,
Expand All @@ -9,7 +12,6 @@ import {
withBase,
} from '@rspress/core/runtime';
import { type Unhead, UnheadProvider } from '@unhead/react/server';
import { StaticRouter } from 'react-router-dom';
import { App } from './App';
import { initPageData } from './initPageData';

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/runtime/ssrServerEntry.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-expect-error in v7, StaticRouter is exported from the main entry, but in v6 it's exported from 'react-router-dom/server'
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

The @ts-expect-error explanation mentions v6/v7 export differences, but the actual TypeScript error being suppressed here is most likely that REACT_ROUTER_DOM_SERVER is a bundler alias/virtual module that TS can’t resolve. Consider adding an ambient module declaration for REACT_ROUTER_DOM_SERVER (preferred), or update the directive comment to reflect the real error being suppressed.

Suggested change
// @ts-expect-error in v7, StaticRouter is exported from the main entry, but in v6 it's exported from 'react-router-dom/server'
declare module 'REACT_ROUTER_DOM_SERVER' {
export { StaticRouter } from 'react-router-dom/server';
}
// Bundler alias for StaticRouter to handle react-router-dom v6/v7 differences

Copilot uses AI. Check for mistakes.
import { StaticRouter } from 'REACT_ROUTER_DOM_SERVER';
import { PassThrough } from 'node:stream';
import { text } from 'node:stream/consumers';
import {
Expand All @@ -10,7 +12,6 @@ import {
import { type Unhead, UnheadProvider } from '@unhead/react/server';
import type { ReactNode } from 'react';
import { renderToPipeableStream } from 'react-dom/server';
import { StaticRouter } from 'react-router-dom';
import { App } from './App';
import { initPageData } from './initPageData';

Expand Down
Loading