From d61573fb1b878180839959081a9f18ebe1879853 Mon Sep 17 00:00:00 2001 From: Jeff Wong Date: Mon, 10 May 2021 07:02:33 -1000 Subject: [PATCH] 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 --- .../discourse/config/environment.js | 2 +- .../discourse/lib/bootstrap-json/index.js | 21 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/discourse/config/environment.js b/app/assets/javascripts/discourse/config/environment.js index e004e1eaba2..f9672557d97 100644 --- a/app/assets/javascripts/discourse/config/environment.js +++ b/app/assets/javascripts/discourse/config/environment.js @@ -4,7 +4,7 @@ module.exports = function (environment) { let ENV = { modulePrefix: "discourse", environment, - rootURL: "/", + rootURL: process.env.DISCOURSE_RELATIVE_URL_ROOT || "/", locationType: "auto", historySupportMiddleware: false, EmberENV: { diff --git a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js index 0e8b503a223..3b97c65d419 100644 --- a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js +++ b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js @@ -155,26 +155,28 @@ function applyBootstrap(bootstrap, template, headers) { return template; } -function buildFromBootstrap(assetPath, proxy, req, headers) { +function buildFromBootstrap(assetPath, proxy, baseURL, req, headers) { // eslint-disable-next-line return new Promise((resolve, reject) => { fs.readFile( path.join(process.cwd(), "dist", assetPath), "utf8", (err, template) => { - getJSON(`${proxy}/bootstrap.json`, null, req.headers) + getJSON(`${proxy}${baseURL}bootstrap.json`, null, req.headers) .then((json) => { resolve(applyBootstrap(json.bootstrap, template, headers)); }) .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")) { return; } @@ -200,6 +202,7 @@ async function handleRequest(assetPath, proxy, req, res) { let json = await buildFromBootstrap( assetPath, proxy, + baseURL, req, response.headers ); @@ -264,7 +267,7 @@ to serve API requests. For example: if (!isFile) { assetPath = "index.html"; } - await handleRequest(assetPath, proxy, req, res); + await handleRequest(assetPath, proxy, baseURL, req, res); } } finally { if (!res.headersSent) { @@ -274,7 +277,7 @@ to serve API requests. For example: }); }, - shouldHandleRequest(req, options) { + shouldHandleRequest(req) { let acceptHeaders = req.headers.accept || []; let hasHTMLHeader = acceptHeaders.indexOf("text/html") !== -1; if (req.method !== "GET") { @@ -292,11 +295,7 @@ to serve API requests. For example: return false; } - let baseURL = - options.rootURL === "" - ? "/" - : cleanBaseURL(options.rootURL || options.baseURL); - let baseURLRegexp = new RegExp(`^${baseURL}`); + let baseURLRegexp = new RegExp(`^/`); return baseURLRegexp.test(req.path); }, };