Skip to content
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

fix(server): access page error when url with query or hash #1265

Merged
merged 2 commits into from
Jan 12, 2024
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
30 changes: 30 additions & 0 deletions e2e/cases/server/htmlFallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ test('should return 404 when htmlFallback false', async ({ page }) => {
await rsbuild.server.close();
});

test('should access /main with query or hash success', async ({ page }) => {
const rsbuild = await dev({
cwd: join(fixtures, 'basic'),
rsbuildConfig: {
source: {
entry: {
main: join(fixtures, 'basic', 'src/index.ts'),
},
},
},
});

const url = new URL(`http://localhost:${rsbuild.port}/main?aa=1`);

const res = await page.goto(url.href);

expect(res?.status()).toBe(200);

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

const url1 = new URL(`http://localhost:${rsbuild.port}/main#aa=1`);

const res1 = await page.goto(url1.href);

expect(res1?.status()).toBe(200);

await rsbuild.server.close();
});

test('should access /main.html success when entry is main', async ({
page,
}) => {
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/server/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type HtmlFallback,
type RequestHandler as Middleware,
} from '@rsbuild/shared';
import { parse } from 'url';
import path from 'path';
import fs from 'fs';

Expand Down Expand Up @@ -55,7 +56,7 @@ export const getHtmlFallbackMiddleware: (params: {

// Handle invalid URLs
try {
pathname = decodeURIComponent(url);
pathname = parse(url, false, true).pathname!;
} catch (err) {
logger.error(
new Error(`Invalid URL: ${color.yellow(url)}`, { cause: err }),
Expand Down Expand Up @@ -89,7 +90,7 @@ export const getHtmlFallbackMiddleware: (params: {

// '/' => '/index.html'
if (pathname.endsWith('/')) {
const newUrl = `${url}index.html`;
const newUrl = `${pathname}index.html`;
const filePath = path.join(distPath, pathname, 'index.html');

if (outputFileSystem.existsSync(filePath)) {
Expand All @@ -99,7 +100,7 @@ export const getHtmlFallbackMiddleware: (params: {
// '/main' => '/main.html'
!pathname.endsWith('.html')
) {
const newUrl = `${url}.html`;
const newUrl = `${pathname}.html`;
const filePath = path.join(distPath, `${pathname}.html`);

if (outputFileSystem.existsSync(filePath)) {
Expand Down