Skip to content

Commit 856a62f

Browse files
committed
fix: add support for SPA mode (aka. historyMode)
1 parent 4dc302a commit 856a62f

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

bin/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,8 @@ const appUriPort = appUriSegments[2] || APP_PORT;
4141

4242
// provide binaries
4343
const concurrentlyBin = path.resolve(__dirname, "..", "./node_modules/.bin/concurrently");
44-
const httpServerBin = path.resolve(__dirname, "..", "./node_modules/.bin/http-server");
4544
const { app_artifact_location, api_location } = readConfigFile();
4645

47-
if (program.build) {
48-
// run the app/api builds
49-
builder();
50-
}
51-
5246
const envVarsObj = {
5347
// set env vars for current command
5448
StaticWebAppsAuthCookie: 123,
@@ -74,7 +68,7 @@ const startCommand = [
7468
// run concurrent commands
7569
concurrentlyBin,
7670
`--restart-tries 3`,
77-
`--names emulator,auth,hosting,functions`,
71+
`--names x,emulator,auth,hosting,functions`,
7872
`-c 'bgYellow.bold,bgMagenta.bold,bgCyan.bold,bgGreen.bold'`,
7973

8074
// start the reverse proxy
@@ -96,6 +90,11 @@ if (process.env.DEBUG) {
9690
console.log(startCommand);
9791
}
9892

93+
if (program.build) {
94+
// run the app/api builds
95+
builder();
96+
}
97+
9998
if (program.ui) {
10099
// print the dashboard UI
101100

src/proxy.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const serveStatic = (file, res) => {
1515
res.end(JSON.stringify(err));
1616
return;
1717
}
18-
console.log("serving", file);
18+
console.log(">> serving", file);
1919
res.writeHead(200);
2020
res.end(data);
2121
});
@@ -128,12 +128,19 @@ const server = http.createServer(function (req, res) {
128128
}
129129

130130
// detected a proxy pass-through from http-server, so 404 it
131-
else if (req.url.startsWith("/?") || req.url.startsWith("/routes.json")) {
131+
else if (req.url.startsWith("/routes.json")) {
132132
console.log("proxy>", req.method, req.headers.host + req.url);
133133
const file404 = path.resolve(__dirname, "404.html");
134134
serveStatic(file404, res);
135135
}
136136

137+
// detected SPA mode
138+
else if (req.url.startsWith("/?")) {
139+
console.log("proxy>", req.method, req.headers.host + req.url);
140+
const fileIndex = path.join(process.env.SWA_EMU_APP_LOCATION, "index.html");
141+
serveStatic(fileIndex, res);
142+
}
143+
137144
// proxy APP request to local APP
138145
else {
139146
const target = process.env.SWA_EMU_APP_URI || "http://localhost:4200";

src/runtimeHost.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module.exports.createRuntimeHost = (port, proxyHost, proxyPort) => {
88
const { app_location, app_artifact_location } = readConfigFile();
99
const runtimeType = detectRuntime(app_location);
1010

11+
console.log(">> detected runtime:", runtimeType);
12+
1113
switch (runtimeType) {
1214
// .NET runtime
1315
case RuntimeType.dotnet:
@@ -20,7 +22,6 @@ module.exports.createRuntimeHost = (port, proxyHost, proxyPort) => {
2022
case RuntimeType.node:
2123
case RuntimeType.unknown:
2224
default:
23-
2425
// See available options for http-server: https://github.com/http-party/http-server#available-options
2526
// Note: --proxy allows us to add fallback routes for SPA (https://github.com/http-party/http-server#catch-all-redirect)
2627
const command = httpServerBin;

src/runtimes.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ module.exports.detectRuntime = (app_location) => {
2020
const files = fs.readdirSync(app_location);
2121

2222
if (files.some((file) => path.extname(file) === ".csproj")) {
23-
console.log(">> .NET detected.");
2423
return RuntimeType.dotnet;
2524
}
2625

2726
if (files.includes("package.json")) {
28-
console.log(">> Node.js detected.");
2927
return RuntimeType.node;
3028
}
3129

32-
console.log(">> Unknown runtime detected.");
3330
return RuntimeType.unknown;
3431
};

0 commit comments

Comments
 (0)