Skip to content

Commit

Permalink
fix: add support for SPA mode (aka. historyMode)
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko committed Sep 15, 2020
1 parent 4dc302a commit 856a62f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
13 changes: 6 additions & 7 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ const appUriPort = appUriSegments[2] || APP_PORT;

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

if (program.build) {
// run the app/api builds
builder();
}

const envVarsObj = {
// set env vars for current command
StaticWebAppsAuthCookie: 123,
Expand All @@ -74,7 +68,7 @@ const startCommand = [
// run concurrent commands
concurrentlyBin,
`--restart-tries 3`,
`--names emulator,auth,hosting,functions`,
`--names x,emulator,auth,hosting,functions`,
`-c 'bgYellow.bold,bgMagenta.bold,bgCyan.bold,bgGreen.bold'`,

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

if (program.build) {
// run the app/api builds
builder();
}

if (program.ui) {
// print the dashboard UI

Expand Down
11 changes: 9 additions & 2 deletions src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const serveStatic = (file, res) => {
res.end(JSON.stringify(err));
return;
}
console.log("serving", file);
console.log(">> serving", file);
res.writeHead(200);
res.end(data);
});
Expand Down Expand Up @@ -128,12 +128,19 @@ const server = http.createServer(function (req, res) {
}

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

// detected SPA mode
else if (req.url.startsWith("/?")) {
console.log("proxy>", req.method, req.headers.host + req.url);
const fileIndex = path.join(process.env.SWA_EMU_APP_LOCATION, "index.html");
serveStatic(fileIndex, res);
}

// proxy APP request to local APP
else {
const target = process.env.SWA_EMU_APP_URI || "http://localhost:4200";
Expand Down
3 changes: 2 additions & 1 deletion src/runtimeHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module.exports.createRuntimeHost = (port, proxyHost, proxyPort) => {
const { app_location, app_artifact_location } = readConfigFile();
const runtimeType = detectRuntime(app_location);

console.log(">> detected runtime:", runtimeType);

switch (runtimeType) {
// .NET runtime
case RuntimeType.dotnet:
Expand All @@ -20,7 +22,6 @@ module.exports.createRuntimeHost = (port, proxyHost, proxyPort) => {
case RuntimeType.node:
case RuntimeType.unknown:
default:

// See available options for http-server: https://github.com/http-party/http-server#available-options
// Note: --proxy allows us to add fallback routes for SPA (https://github.com/http-party/http-server#catch-all-redirect)
const command = httpServerBin;
Expand Down
3 changes: 0 additions & 3 deletions src/runtimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ module.exports.detectRuntime = (app_location) => {
const files = fs.readdirSync(app_location);

if (files.some((file) => path.extname(file) === ".csproj")) {
console.log(">> .NET detected.");
return RuntimeType.dotnet;
}

if (files.includes("package.json")) {
console.log(">> Node.js detected.");
return RuntimeType.node;
}

console.log(">> Unknown runtime detected.");
return RuntimeType.unknown;
};

0 comments on commit 856a62f

Please sign in to comment.