Skip to content

Commit af47de4

Browse files
committed
fix(dev-server): fix historyApiFallback disableDotRule
Closes #1108
1 parent ddf0b2d commit af47de4

5 files changed

Lines changed: 27 additions & 22 deletions

File tree

src/declarations/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export interface ConfigBundle {
298298

299299

300300
export interface ServiceWorkerConfig {
301-
// https://workboxjs.org/reference-docs/latest/module-workbox-build.html#.Configuration
301+
// https://developers.google.com/web/tools/workbox/modules/workbox-build#full_generatesw_config
302302
swDest?: string;
303303
swSrc?: string;
304304
globPatterns?: string[];

src/dev-server/request-handler.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function createRequestHandler(devServerConfig: d.DevServerConfig, fs: d.F
2222
return res.end();
2323
}
2424

25-
if (!req.url.startsWith(devServerConfig.baseUrl)) {
25+
if (!req.pathname.startsWith(devServerConfig.baseUrl)) {
2626
return serve404Content(res, `404 File Not Found, base url: ${devServerConfig.baseUrl}`);
2727
}
2828

@@ -92,9 +92,18 @@ function normalizeHttpRequest(devServerConfig: d.DevServerConfig, incomingReq: h
9292
}
9393

9494

95-
function isValidHistoryApi(devServerConfig: d.DevServerConfig, req: d.HttpRequest) {
96-
return !!devServerConfig.historyApiFallback &&
97-
req.method === 'GET' &&
98-
(!devServerConfig.historyApiFallback.disableDotRule && !req.pathname.includes('.')) &&
99-
req.acceptHeader.includes('text/html');
95+
export function isValidHistoryApi(devServerConfig: d.DevServerConfig, req: d.HttpRequest) {
96+
if (!devServerConfig.historyApiFallback) {
97+
return false;
98+
}
99+
if (req.method !== 'GET') {
100+
return false;
101+
}
102+
if (!req.acceptHeader.includes('text/html') && req.acceptHeader !== '*/*') {
103+
return false;
104+
}
105+
if (!devServerConfig.historyApiFallback.disableDotRule && req.pathname.includes('.')) {
106+
return false;
107+
}
108+
return true;
100109
}

src/dev-server/test/req-handler.spec.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ describe('request-handler', async () => {
7474
req.headers = {
7575
accept: '*/*'
7676
};
77-
req.url = '/about.us';
77+
req.url = 'http://stenciljs.com/about.us';
7878
req.method = 'GET';
7979

8080
await handler(req, res);
81-
expect(res.$statusCode).toBe(404);
81+
expect(res.$statusCode).toBe(200);
8282
});
8383

8484
it('should not load historyApiFallback index.html when dot in the url', async () => {
@@ -91,7 +91,7 @@ describe('request-handler', async () => {
9191
req.headers = {
9292
accept: '*/*'
9393
};
94-
req.url = '/about.us';
94+
req.url = 'http://stenciljs.com/about.us';
9595
req.method = 'GET';
9696

9797
await handler(req, res);
@@ -380,17 +380,6 @@ describe('request-handler', async () => {
380380
});
381381

382382

383-
function readResponse(res: http.ServerRequest) {
384-
let content = '';
385-
386-
res.on('data', chunk => {
387-
content += chunk;
388-
});
389-
390-
return content;
391-
}
392-
393-
394383
interface TestServerResponse extends http.ServerResponse {
395384
$statusCode: number;
396385
$headers: any;

test/karma/stencil.config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@ export const config: Config = {
2828
builtins(),
2929
globals(),
3030
sass()
31-
]
31+
],
32+
devServer: {
33+
historyApiFallback: {
34+
disableDotRule: true,
35+
index: 'index.html'
36+
}
37+
}
3238
};

test/karma/test-app/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
index

0 commit comments

Comments
 (0)