FIX: ember-cli proxy subfolder fix (#12996)

* FIX: ember-cli proxy subfolder fix

* REFACTOR: put rootURL setup in environment, update baseURL logic for subfolder

Correctly have ember understand and parse relative_url_root and use it in
the dev server
This commit is contained in:
Jeff Wong 2021-05-10 07:02:33 -10:00 committed by GitHub
parent 81616fbdc4
commit d61573fb1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 12 deletions

View File

@ -4,7 +4,7 @@ module.exports = function (environment) {
let ENV = { let ENV = {
modulePrefix: "discourse", modulePrefix: "discourse",
environment, environment,
rootURL: "/", rootURL: process.env.DISCOURSE_RELATIVE_URL_ROOT || "/",
locationType: "auto", locationType: "auto",
historySupportMiddleware: false, historySupportMiddleware: false,
EmberENV: { EmberENV: {

View File

@ -155,26 +155,28 @@ function applyBootstrap(bootstrap, template, headers) {
return template; return template;
} }
function buildFromBootstrap(assetPath, proxy, req, headers) { function buildFromBootstrap(assetPath, proxy, baseURL, req, headers) {
// eslint-disable-next-line // eslint-disable-next-line
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.readFile( fs.readFile(
path.join(process.cwd(), "dist", assetPath), path.join(process.cwd(), "dist", assetPath),
"utf8", "utf8",
(err, template) => { (err, template) => {
getJSON(`${proxy}/bootstrap.json`, null, req.headers) getJSON(`${proxy}${baseURL}bootstrap.json`, null, req.headers)
.then((json) => { .then((json) => {
resolve(applyBootstrap(json.bootstrap, template, headers)); resolve(applyBootstrap(json.bootstrap, template, headers));
}) })
.catch((e) => { .catch((e) => {
reject(`Could not get ${proxy}/bootstrap.json\n\n${e.toString()}`); reject(
`Could not get ${proxy}${baseURL}bootstrap.json\n\n${e.toString()}`
);
}); });
} }
); );
}); });
} }
async function handleRequest(assetPath, proxy, req, res) { async function handleRequest(assetPath, proxy, baseURL, req, res) {
if (assetPath.endsWith("tests/index.html")) { if (assetPath.endsWith("tests/index.html")) {
return; return;
} }
@ -200,6 +202,7 @@ async function handleRequest(assetPath, proxy, req, res) {
let json = await buildFromBootstrap( let json = await buildFromBootstrap(
assetPath, assetPath,
proxy, proxy,
baseURL,
req, req,
response.headers response.headers
); );
@ -264,7 +267,7 @@ to serve API requests. For example:
if (!isFile) { if (!isFile) {
assetPath = "index.html"; assetPath = "index.html";
} }
await handleRequest(assetPath, proxy, req, res); await handleRequest(assetPath, proxy, baseURL, req, res);
} }
} finally { } finally {
if (!res.headersSent) { if (!res.headersSent) {
@ -274,7 +277,7 @@ to serve API requests. For example:
}); });
}, },
shouldHandleRequest(req, options) { shouldHandleRequest(req) {
let acceptHeaders = req.headers.accept || []; let acceptHeaders = req.headers.accept || [];
let hasHTMLHeader = acceptHeaders.indexOf("text/html") !== -1; let hasHTMLHeader = acceptHeaders.indexOf("text/html") !== -1;
if (req.method !== "GET") { if (req.method !== "GET") {
@ -292,11 +295,7 @@ to serve API requests. For example:
return false; return false;
} }
let baseURL = let baseURLRegexp = new RegExp(`^/`);
options.rootURL === ""
? "/"
: cleanBaseURL(options.rootURL || options.baseURL);
let baseURLRegexp = new RegExp(`^${baseURL}`);
return baseURLRegexp.test(req.path); return baseURLRegexp.test(req.path);
}, },
}; };