-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1149 from navikt/new_devserver
New devserver
- Loading branch information
Showing
10 changed files
with
116 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ | |
!tsconfig.json | ||
!package*.json | ||
!public/ | ||
!node_modules | ||
!node_modules | ||
!dev-server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,43 @@ | ||
import * as fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
import express, { Express } from "express"; | ||
import express, { Express, Response } from "express"; | ||
|
||
const indexHtmlExists = fs.existsSync("./public/index.html"); | ||
const publicFolderPath = indexHtmlExists ? "./public" : "./publicLocal"; | ||
// When deployed the built frontend is copied into the public directory. If running BFF locally the directory will not exist. | ||
const productionBuildExists = fs.existsSync("./public/index.html"); | ||
|
||
const setupStaticRoutes = (app: Express) => { | ||
app.use(express.static(publicFolderPath)); | ||
export function setupStaticRoutes(app: Express) { | ||
app.use(express.static("./public")); | ||
|
||
app.get("/toggle-devserver", (request, response) => { | ||
const developmentServerShouldBeEnabled = | ||
request.cookies["use-local-devserver"] !== "true"; | ||
response.cookie("use-local-devserver", developmentServerShouldBeEnabled, { | ||
httpOnly: false, | ||
secure: false, | ||
sameSite: "lax", | ||
}); | ||
|
||
developmentServerShouldBeEnabled | ||
? serveDevserver(response) | ||
: serveProductionBuild(response); | ||
}); | ||
|
||
app.get("*", (request, response) => { | ||
response.sendFile(path.resolve(publicFolderPath) + "/index.html"); | ||
const developmentServerIsEnabled = | ||
request.cookies["use-local-devserver"] === "true"; | ||
|
||
if (developmentServerIsEnabled || !productionBuildExists) { | ||
return serveDevserver(response); | ||
} | ||
return serveProductionBuild(response); | ||
}); | ||
}; | ||
} | ||
|
||
function serveDevserver(response: Response) { | ||
response.sendFile(path.resolve("./dev-server") + "/index.html"); | ||
} | ||
|
||
export default setupStaticRoutes; | ||
function serveProductionBuild(response: Response) { | ||
response.sendFile(path.resolve("./public") + "/index.html"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.