From 2b1ceb6d0845dd2335a7a36dfe3337d7270cd5a4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 2 Sep 2021 16:52:53 -0500 Subject: [PATCH] Build config: subdirectory support --- app/soapbox/build_config.js | 27 ++++++++++++++++++++++++++- app/soapbox/containers/soapbox.js | 3 ++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/soapbox/build_config.js b/app/soapbox/build_config.js index 7dc8cec99..b0fb99cf1 100644 --- a/app/soapbox/build_config.js +++ b/app/soapbox/build_config.js @@ -4,7 +4,13 @@ * @module soapbox/build_config */ -const { BACKEND_URL } = process.env; +const { trimEnd } = require('lodash'); + +const { + BACKEND_URL, + FE_BASE_PATH, + CI_PAGES_URL, +} = process.env; const sanitizeURL = url => { try { @@ -14,10 +20,29 @@ const sanitizeURL = url => { } }; +// Run Soapbox FE from a subdirectory. +// If FE_BASE_PATH (eg '/web') is provided, prefer it. +// For GitLab Pages builds, CI_PAGES_URL will be used. +const getFeBasePath = () => { + if (FE_BASE_PATH) { + return trimEnd(FE_BASE_PATH, '/'); + } else if (CI_PAGES_URL) { + try { + const { pathname } = new URL(CI_PAGES_URL); + return trimEnd(pathname, '/'); + } catch { + return '/'; + } + } else { + return '/'; + } +}; + // JSON.parse/stringify is to emulate what @preval is doing and avoid any // inconsistent behavior in dev mode const sanitize = obj => JSON.parse(JSON.stringify(obj)); module.exports = sanitize({ BACKEND_URL: sanitizeURL(BACKEND_URL), + FE_BASE_PATH: getFeBasePath(), }); diff --git a/app/soapbox/containers/soapbox.js b/app/soapbox/containers/soapbox.js index 8a0c327da..63fe783de 100644 --- a/app/soapbox/containers/soapbox.js +++ b/app/soapbox/containers/soapbox.js @@ -26,6 +26,7 @@ import { getSettings } from 'soapbox/actions/settings'; import { getSoapboxConfig } from 'soapbox/actions/soapbox'; import { generateThemeCss } from 'soapbox/utils/theme'; import messages from 'soapbox/locales/messages'; +import { FE_BASE_PATH } from 'soapbox/build_config'; const validLocale = locale => Object.keys(messages).includes(locale); @@ -142,7 +143,7 @@ class SoapboxMount extends React.PureComponent { ))} - + {!me && }