Skip to content

Commit 117da55

Browse files
committed
chore(core): improve fallback url implementation
1 parent fb09bc6 commit 117da55

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

bin/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const startCommand = [
8383
// serve the app
8484
// See available options for http-server: https://github.com/http-party/http-server#available-options
8585
// Note: --proxy allows us to add fallback routes for SPA (https://github.com/http-party/http-server#catch-all-redirect)
86-
`"${httpServerBin} ${app_artifact_location} -p ${appUriPort} -c-1 --proxy http://localhost:${appUriPort}? --silent"`,
86+
`"${httpServerBin} ${app_artifact_location} -p ${appUriPort} -c-1 --proxy http://${program.host}:${program.port}/?"`,
8787

8888
// serve the api, if it's available
8989
`"[ -d '${api_location}' ] && (cd ${api_location}; func start --cors *) || echo 'No API found. Skipping.'"`,

src/proxy.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require("fs");
22
const path = require("path");
33
const http = require("http");
4+
const url = require("url");
45
const httpProxy = require("http-proxy");
56
const proxyApp = httpProxy.createProxyServer({ autoRewrite: true });
67
const proxyApi = httpProxy.createProxyServer({ autoRewrite: true });
@@ -13,6 +14,7 @@ const serveStatic = (file, res) => {
1314
res.end(JSON.stringify(err));
1415
return;
1516
}
17+
console.log("serving", file);
1618
res.writeHead(200);
1719
res.end(data);
1820
});
@@ -67,20 +69,33 @@ var server = http.createServer(function (req, res) {
6769
});
6870

6971
proxyApp.on("proxyRes", function (proxyRes, req, res) {
72+
console.log("app>>>", req.method, target + req.url, `[${proxyRes.statusCode}]`);
73+
74+
const uri = url.parse(req.url).pathname;
75+
// default to SWA 404 page
7076
const file404 = path.resolve(__dirname, "404.html");
77+
const fileIndex = path.join(process.env.SWA_EMU_APP_LOCATION, "index.html");
78+
let resource = path.join(process.env.SWA_EMU_APP_LOCATION, req.url);
79+
const isRouteRequest = (uri) => (uri.split("/").pop().indexOf(".") === -1 ? true : false);
80+
7181
if (proxyRes.statusCode === 404) {
7282
serveStatic(file404, res);
7383
} else {
74-
let file = path.join(process.env.SWA_EMU_APP_LOCATION, req.url);
75-
if (fs.existsSync(file)) {
76-
if (fs.lstatSync(file).isDirectory()) {
77-
file = `${file}index.html`;
84+
if (fs.existsSync(resource)) {
85+
if (fs.lstatSync(resource).isDirectory()) {
86+
resource = fileIndex;
7887
}
79-
serveStatic(file, res);
8088
} else {
81-
// URL/file not found on disk
82-
serveStatic(file404, res);
89+
if (isRouteRequest(uri)) {
90+
// route detected: fallback to index file
91+
resource = fileIndex;
92+
} else {
93+
// resource not found on disk
94+
resource = file404;
95+
}
8396
}
97+
98+
serveStatic(resource, res);
8499
}
85100
});
86101

0 commit comments

Comments
 (0)