summaryrefslogtreecommitdiffstats
path: root/app/javascript/entrypoints/public-path.ts
diff options
context:
space:
mode:
Diffstat (limited to 'app/javascript/entrypoints/public-path.ts')
-rw-r--r--app/javascript/entrypoints/public-path.ts23
1 files changed, 23 insertions, 0 deletions
diff --git a/app/javascript/entrypoints/public-path.ts b/app/javascript/entrypoints/public-path.ts
new file mode 100644
index 00000000000..ac4b9355b95
--- /dev/null
+++ b/app/javascript/entrypoints/public-path.ts
@@ -0,0 +1,23 @@
+// Dynamically set webpack's loading path depending on a meta header, in order
+// to share the same assets regardless of instance configuration.
+// See https://webpack.js.org/guides/public-path/#on-the-fly
+
+function removeOuterSlashes(string: string) {
+ return string.replace(/^\/*/, '').replace(/\/*$/, '');
+}
+
+function formatPublicPath(host = '', path = '') {
+ let formattedHost = removeOuterSlashes(host);
+ if (formattedHost && !/^http/i.test(formattedHost)) {
+ formattedHost = `//${formattedHost}`;
+ }
+ const formattedPath = removeOuterSlashes(path);
+ return `${formattedHost}/${formattedPath}/`;
+}
+
+const cdnHost = document.querySelector<HTMLMetaElement>('meta[name=cdn-host]');
+
+__webpack_public_path__ = formatPublicPath(
+ cdnHost ? cdnHost.content : '',
+ process.env.PUBLIC_OUTPUT_PATH,
+);