Skip to content

Commit

Permalink
fix: compat invalid route (#3611)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Sep 30, 2024
1 parent 19920a5 commit 5ada980
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
5 changes: 5 additions & 0 deletions e2e/cases/server/html-fallback/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ test('should access / success and htmlFallback success by default', async ({

await expect(page.locator('#test')).toHaveText('Hello Rsbuild!');

// compat invalid route and fallback success
await page.goto(`http://localhost:${rsbuild.port}//aaaaa`);

await expect(page.locator('#test')).toHaveText('Hello Rsbuild!');

await rsbuild.close();
});

Expand Down
20 changes: 8 additions & 12 deletions packages/core/src/server/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { IncomingMessage } from 'node:http';
import path from 'node:path';
import { parse } from 'node:url';
import type Connect from 'connect';
import color from 'picocolors';
import { logger } from '../logger';
Expand Down Expand Up @@ -103,6 +102,12 @@ const maybeHTMLRequest = (req: IncomingMessage) => {
);
};

const postfixRE = /[?#].*$/;

const getUrlPathname = (url: string): string => {
return url.replace(postfixRE, '');
};

/**
* Support access HTML without suffix
*/
Expand All @@ -117,17 +122,8 @@ export const getHtmlCompletionMiddleware: (params: {
}

const url = req.url!;
let pathname: string;

// Handle invalid URLs
try {
pathname = parse(url, false, true).pathname!;
} catch (err) {
logger.error(
new Error(`Invalid URL: ${color.yellow(url)}`, { cause: err }),
);
return next();
}

const pathname = getUrlPathname(url);

const rewrite = (newUrl: string) => {
req.url = newUrl;
Expand Down

0 comments on commit 5ada980

Please sign in to comment.