Skip to content

Commit 46de071

Browse files
authored
fix: improve query params logic if dev server (#430)
Closes #429
1 parent 67fc595 commit 46de071

File tree

7 files changed

+72
-65
lines changed

7 files changed

+72
-65
lines changed

src/core/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ export const MIME_TYPE_LIST: { [key: string]: string } = {
7777
".jpeg": "image/jpeg",
7878
".jpg": "image/jpeg",
7979
".js": "text/javascript",
80+
".vue": "text/javascript",
8081
".json": "application/json",
82+
".map": "application/json",
8183
".jsonld": "application/ld+json",
8284
".mid": "audio/midi",
8385
".midi": "audio/x-midi",

src/msha/middlewares/request.middleware.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,23 +268,23 @@ export async function requestMiddleware(
268268

269269
logger.silly(`checking for query params`);
270270

271-
const { matchingRewriteRoutePath, sanitizedUrl, matchingRewriteRoute } = parseQueryParams(req, matchingRouteRule);
271+
const { urlPathnameWithoutQueryParams, url, urlPathnameWithQueryParams } = parseQueryParams(req, matchingRouteRule);
272272

273273
logger.silly(`checking rewrite auth login request`);
274-
if (matchingRewriteRoute && isLoginRequest(matchingRewriteRoutePath)) {
274+
if (urlPathnameWithQueryParams && isLoginRequest(urlPathnameWithoutQueryParams)) {
275275
logger.silly(` - auth login dectected`);
276276

277277
authStatus = AUTH_STATUS.HostNameAuthLogin;
278-
req.url = sanitizedUrl.toString();
278+
req.url = url.toString();
279279
return await handleAuthRequest(req, res, matchingRouteRule, userConfig);
280280
}
281281

282282
logger.silly(`checking rewrite auth logout request`);
283-
if (matchingRewriteRoute && isLogoutRequest(matchingRewriteRoutePath)) {
283+
if (urlPathnameWithQueryParams && isLogoutRequest(urlPathnameWithoutQueryParams)) {
284284
logger.silly(` - auth logout dectected`);
285285

286286
authStatus = AUTH_STATUS.HostNameAuthLogout;
287-
req.url = sanitizedUrl.toString();
287+
req.url = url.toString();
288288
return await handleAuthRequest(req, res, matchingRouteRule, userConfig);
289289
}
290290

@@ -293,7 +293,7 @@ export async function requestMiddleware(
293293
return serveStaticOrProxyResponse(req, res, proxyApp, target);
294294
}
295295

296-
if (authStatus != AUTH_STATUS.NoAuth && (authStatus != AUTH_STATUS.HostNameAuthLogin || !matchingRewriteRoute)) {
296+
if (authStatus != AUTH_STATUS.NoAuth && (authStatus != AUTH_STATUS.HostNameAuthLogin || !urlPathnameWithQueryParams)) {
297297
if (authStatus == AUTH_STATUS.HostNameAuthLogin && matchingRouteRule) {
298298
return getAuthBlockResponse(req, res, userConfig, matchingRouteRule);
299299
}

src/msha/middlewares/response.middleware.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import chalk from "chalk";
22
import type http from "http";
33
import { isHttpUrl, isSWAConfigFileUrl, logger } from "../../core";
4+
import { IS_APP_DEV_SERVER } from "../../core/constants";
45
import { handleErrorPage } from "../handlers/error-page.handler";
56
import { handleFunctionRequest, isFunctionRequest } from "../handlers/function.handler";
67
import {
@@ -100,19 +101,27 @@ export function getStorageContent(
100101
};
101102
}
102103

103-
const { matchingRewriteRoutePath } = parseQueryParams(req, matchedRoute);
104-
105-
let requestPath = matchingRewriteRoutePath;
106-
let decodedRequestPath = matchingRewriteRoutePath;
104+
let requestPath = req.url as string;
105+
let filePathFromRequest: string | null = null;
106+
let decodedRequestPath = req.url;
107+
const { urlPathnameWithoutQueryParams } = parseQueryParams(req, matchedRoute);
108+
109+
// we only process if the user is NOT connecting to a remote dev server.
110+
// if the user is connecting to a remote dev server, we skip the following logic.
111+
if (IS_APP_DEV_SERVER()) {
112+
logger.silly(`remote dev server detected.`);
113+
} else {
114+
requestPath = urlPathnameWithoutQueryParams;
115+
decodedRequestPath = urlPathnameWithoutQueryParams;
116+
117+
if (pathToServe) {
118+
requestPath = pathToServe;
119+
decodedRequestPath = decodeURI(pathToServe);
120+
}
107121

108-
if (pathToServe) {
109-
requestPath = pathToServe;
110-
decodedRequestPath = decodeURI(pathToServe);
122+
filePathFromRequest = tryFindFileForRequest(requestPath!);
111123
}
112124

113-
let filePathFromRequest = tryFindFileForRequest(requestPath!);
114-
logger.silly(` - filePathFromRequest: ${chalk.yellow(filePathFromRequest)}`);
115-
116125
if (!filePathFromRequest) {
117126
let shouldDisplayNotFoundPage = true;
118127
if (navigationFallbackRule?.rewrite) {

src/msha/routes-engine/route-processor.spec.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,71 @@ describe("parseQueryParams()", () => {
88
it("should match /?a=1&b=2 URL", () => {
99
req.url = "/?a=1&b=2";
1010
const matchingRouteRule = { route: "/" };
11-
const { matchingRewriteRoutePath, matchingRewriteRoute } = parseQueryParams(req, matchingRouteRule);
11+
const { urlPathnameWithoutQueryParams, urlPathnameWithQueryParams } = parseQueryParams(req, matchingRouteRule);
1212

13-
expect(matchingRewriteRoutePath).toBe("/");
14-
expect(matchingRewriteRoute).toBe(req.url);
13+
expect(urlPathnameWithoutQueryParams).toBe("/");
14+
expect(urlPathnameWithQueryParams).toBe(req.url);
1515
});
1616

1717
it("should match /index.html?a=1&b=2 URL", () => {
1818
req.url = "/index.html?a=1&b=2";
1919
const matchingRouteRule = { route: "/" };
20-
const { matchingRewriteRoutePath, matchingRewriteRoute } = parseQueryParams(req, matchingRouteRule);
20+
const { urlPathnameWithoutQueryParams, urlPathnameWithQueryParams } = parseQueryParams(req, matchingRouteRule);
2121

22-
expect(matchingRewriteRoutePath).toBe("/index.html");
23-
expect(matchingRewriteRoute).toBe(req.url);
22+
expect(urlPathnameWithoutQueryParams).toBe("/index.html");
23+
expect(urlPathnameWithQueryParams).toBe(req.url);
2424
});
2525

2626
it("should match /foo/bar/index.html?a=1&b=2 URL", () => {
2727
req.url = "/foo/bar/index.html?a=1&b=2";
2828
const matchingRouteRule = { route: "/" };
29-
const { matchingRewriteRoutePath, matchingRewriteRoute } = parseQueryParams(req, matchingRouteRule);
29+
const { urlPathnameWithoutQueryParams, urlPathnameWithQueryParams } = parseQueryParams(req, matchingRouteRule);
3030

31-
expect(matchingRewriteRoutePath).toBe("/foo/bar/index.html");
32-
expect(matchingRewriteRoute).toBe(req.url);
31+
expect(urlPathnameWithoutQueryParams).toBe("/foo/bar/index.html");
32+
expect(urlPathnameWithQueryParams).toBe(req.url);
3333
});
3434

3535
it("should match /foo/bar/?a=1&b=2 URL", () => {
3636
req.url = "/foo/bar/?a=1&b=2";
3737
const matchingRouteRule = { route: "/" };
38-
const { matchingRewriteRoutePath, matchingRewriteRoute } = parseQueryParams(req, matchingRouteRule);
38+
const { urlPathnameWithoutQueryParams, urlPathnameWithQueryParams } = parseQueryParams(req, matchingRouteRule);
3939

40-
expect(matchingRewriteRoutePath).toBe("/foo/bar/");
41-
expect(matchingRewriteRoute).toBe(req.url);
40+
expect(urlPathnameWithoutQueryParams).toBe("/foo/bar/");
41+
expect(urlPathnameWithQueryParams).toBe(req.url);
4242
});
4343
});
4444

4545
describe("with rewrite rule enabled", () => {
4646
it("should match /?a=1&b=2 URL", () => {
4747
const matchingRouteRule = { route: "/", rewrite: "/?a=1&b=2" };
48-
const { matchingRewriteRoutePath, matchingRewriteRoute } = parseQueryParams(req, matchingRouteRule);
48+
const { urlPathnameWithoutQueryParams, urlPathnameWithQueryParams } = parseQueryParams(req, matchingRouteRule);
4949

50-
expect(matchingRewriteRoutePath).toBe("/");
51-
expect(matchingRewriteRoute).toBe(matchingRouteRule.rewrite);
50+
expect(urlPathnameWithoutQueryParams).toBe("/");
51+
expect(urlPathnameWithQueryParams).toBe(matchingRouteRule.rewrite);
5252
});
5353

5454
it("should match /index.html?a=1&b=2 URL", () => {
5555
const matchingRouteRule = { route: "/", rewrite: "/index.html?a=1&b=2" };
56-
const { matchingRewriteRoutePath, matchingRewriteRoute } = parseQueryParams(req, matchingRouteRule);
56+
const { urlPathnameWithoutQueryParams, urlPathnameWithQueryParams } = parseQueryParams(req, matchingRouteRule);
5757

58-
expect(matchingRewriteRoutePath).toBe("/index.html");
59-
expect(matchingRewriteRoute).toBe(matchingRouteRule.rewrite);
58+
expect(urlPathnameWithoutQueryParams).toBe("/index.html");
59+
expect(urlPathnameWithQueryParams).toBe(matchingRouteRule.rewrite);
6060
});
6161

6262
it("should match /foo/bar/index.html?a=1&b=2 URL", () => {
6363
const matchingRouteRule = { route: "/", rewrite: "/foo/bar/index.html?a=1&b=2" };
64-
const { matchingRewriteRoutePath, matchingRewriteRoute } = parseQueryParams(req, matchingRouteRule);
64+
const { urlPathnameWithoutQueryParams, urlPathnameWithQueryParams } = parseQueryParams(req, matchingRouteRule);
6565

66-
expect(matchingRewriteRoutePath).toBe("/foo/bar/index.html");
67-
expect(matchingRewriteRoute).toBe(matchingRouteRule.rewrite);
66+
expect(urlPathnameWithoutQueryParams).toBe("/foo/bar/index.html");
67+
expect(urlPathnameWithQueryParams).toBe(matchingRouteRule.rewrite);
6868
});
6969

7070
it("should match /foo/bar/?a=1&b=2 URL", () => {
7171
const matchingRouteRule = { route: "/", rewrite: "/foo/bar/?a=1&b=2" };
72-
const { matchingRewriteRoutePath, matchingRewriteRoute } = parseQueryParams(req, matchingRouteRule);
72+
const { urlPathnameWithoutQueryParams, urlPathnameWithQueryParams } = parseQueryParams(req, matchingRouteRule);
7373

74-
expect(matchingRewriteRoutePath).toBe("/foo/bar/");
75-
expect(matchingRewriteRoute).toBe(matchingRouteRule.rewrite);
74+
expect(urlPathnameWithoutQueryParams).toBe("/foo/bar/");
75+
expect(urlPathnameWithQueryParams).toBe(matchingRouteRule.rewrite);
7676
});
7777
});
7878
});

src/msha/routes-engine/route-processor.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type http from "http";
21
import chalk from "chalk";
2+
import type http from "http";
33
import { DEFAULT_CONFIG } from "../../config";
44
import { logger } from "../../core";
55
import { AUTH_STATUS, SWA_CLI_APP_PROTOCOL } from "../../core/constants";
@@ -127,16 +127,23 @@ export function isCustomUrl(req: http.IncomingMessage) {
127127
}
128128

129129
export function parseQueryParams(req: http.IncomingMessage, matchingRouteRule: SWAConfigFileRoute | undefined) {
130-
const matchingRewriteRoute = matchingRouteRule?.rewrite || req.url;
131-
const sanitizedUrl = new URL(matchingRewriteRoute!, `${SWA_CLI_APP_PROTOCOL}://${req?.headers?.host}`);
132-
const matchingRewriteRouteQueryString = sanitizedUrl.searchParams.toString();
133-
const matchingRewriteRoutePath = sanitizedUrl.pathname;
134-
135-
if (matchingRewriteRouteQueryString !== "") {
136-
logger.silly(` - query: ${chalk.yellow(matchingRewriteRouteQueryString)}`);
137-
logger.silly(` - sanitizedUrl: ${chalk.yellow(sanitizedUrl)}`);
138-
logger.silly(` - matchingRewriteRoute: ${chalk.yellow(matchingRewriteRoute)}`);
139-
logger.silly(` - matchingRewriteRoutePath: ${chalk.yellow(matchingRewriteRoutePath)}`);
130+
const urlPathnameWithQueryParams = matchingRouteRule?.rewrite || req.url;
131+
const url = new URL(urlPathnameWithQueryParams!, `${SWA_CLI_APP_PROTOCOL}://${req?.headers?.host}`);
132+
const urlQueryString = url.searchParams.toString();
133+
const urlPathnameWithoutQueryParams = url.pathname;
134+
135+
if (urlQueryString !== "") {
136+
logger.silly(` - url: ${chalk.yellow(url)}`);
137+
logger.silly(` - urlQueryString: ${chalk.yellow(urlQueryString)}`);
138+
url.searchParams.forEach((value, key) => {
139+
logger.silly(` - ${key}: ${chalk.yellow(value || "<undefined>")}`);
140+
});
141+
logger.silly(` - urlPathnameWithQueryParams: ${chalk.yellow(urlPathnameWithQueryParams)}`);
142+
logger.silly(` - urlPathnameWithoutQueryParams: ${chalk.yellow(urlPathnameWithoutQueryParams)}`);
140143
}
141-
return { matchingRewriteRoutePath, sanitizedUrl, matchingRewriteRoute };
144+
return {
145+
url,
146+
urlPathnameWithoutQueryParams,
147+
urlPathnameWithQueryParams,
148+
};
142149
}

src/msha/routes-engine/rules/mime-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function getMimeTypeForExtension(filePathFromRequest: string | URL | null
2525
const extension = path.extname(filePathFromRequest!);
2626

2727
logger.silly(`checking mime types`);
28+
logger.silly(` - filePathFromRequest: ${chalk.yellow(filePathFromRequest)}`);
2829
logger.silly(` - extension: ${chalk.yellow(extension || "<empty>")}`);
2930

3031
let mimeType = MIME_TYPE_LIST[extension] || DEFAULT_MIME_TYPE;

src/msha/routes-engine/rules/routes.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,12 @@ import fs from "fs";
33
import type http from "http";
44
import path from "path";
55
import { decodeCookie, logger } from "../../../core";
6-
import {
7-
ALLOWED_HTTP_METHODS_FOR_STATIC_CONTENT,
8-
AUTH_STATUS,
9-
IS_APP_DEV_SERVER,
10-
SWA_CLI_APP_PROTOCOL,
11-
SWA_CLI_OUTPUT_LOCATION,
12-
} from "../../../core/constants";
6+
import { ALLOWED_HTTP_METHODS_FOR_STATIC_CONTENT, AUTH_STATUS, SWA_CLI_APP_PROTOCOL, SWA_CLI_OUTPUT_LOCATION } from "../../../core/constants";
137
import { isAuthRequest } from "../../handlers/auth.handler";
148
import { doesRequestPathMatchLegacyRoute, doesRequestPathMatchRoute } from "../route-processor";
159

1610
export function tryFindFileForRequest(rawRequestPath: string) {
1711
logger.silly(`finding file for request`);
18-
// if the user is connecting to a remote dev server, filePath will be an HTTP request pointing to the remote resource.
19-
// We exit here and let the remote server handle the request.
20-
if (IS_APP_DEV_SERVER()) {
21-
return rawRequestPath;
22-
}
23-
2412
// percent decode request path
2513
let requestPath = decodeURIComponent(rawRequestPath);
2614
if (requestPath.endsWith("/")) {

0 commit comments

Comments
 (0)