Skip to content
Merged
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
26 changes: 23 additions & 3 deletions packages/server/core/src/plugins/render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ import { requestLatencyMiddleware } from '../monitors';

export * from './inject';

const DYNAMIC_ROUTE_REG = /\/:./;

/**
* Escape special regex characters in a path string.
* This is needed because Hono's router converts paths to regex patterns,
* and special characters like parentheses need to be escaped.
*/
function escapeRegexSpecialChars(path: string): string {
// Escape special regex characters: ( ) [ ] { } * + ? . ^ $ | \
return path.replace(/[()[\]{}*+?.^$|\\]/g, '\\$&');
}

export const renderPlugin = (): ServerPlugin => ({
name: '@modern-js/plugin-render',

Expand All @@ -35,9 +47,17 @@ export const renderPlugin = (): ServerPlugin => ({

for (const route of pageRoutes) {
const { urlPath: originUrlPath } = route;
const urlPath = originUrlPath.endsWith('/')
? `${originUrlPath}*`
: `${originUrlPath}/*`;
const isDynamic = DYNAMIC_ROUTE_REG.test(originUrlPath);

// For static routes, escape special regex characters to prevent regex syntax errors
// For dynamic routes, keep as-is since they contain route parameters
const escapedPath = isDynamic
? originUrlPath
: escapeRegexSpecialChars(originUrlPath);

const urlPath = escapedPath.endsWith('/')
? `${escapedPath}*`
: `${escapedPath}/*`;

// config.renderMiddlewares can register by server config and prepare hook
renderMiddlewares?.forEach(m => {
Expand Down
1 change: 1 addition & 0 deletions tests/integration/server-prod/config/public/test(bug.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
8 changes: 8 additions & 0 deletions tests/integration/server-prod/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,12 @@ describe('test basic usage', () => {
expect(status).toBe(successStatus);
expect(headers['content-type']).toBe('image/png');
});

test(`should serve static file with special characters in filename`, async () => {
const { status, data } = await axios.get(
`http://localhost:${appPort}/test(bug.txt`,
);
expect(status).toBe(successStatus);
expect(data.trim()).toBe('test');
});
});