Skip to content

Instantly share code, notes, and snippets.

@TTy32
Created March 17, 2021 10:08
Show Gist options
  • Save TTy32/170e75e8fc2efd9b23fd925d9f30bed5 to your computer and use it in GitHub Desktop.
Save TTy32/170e75e8fc2efd9b23fd925d9f30bed5 to your computer and use it in GitHub Desktop.
Dynamic runtime baseURL for angular prod builds
<!-- Replace `<base baseHref="/">` in `index.html` with: -->
<!-- Dynamic base href -->
<base id="baseHref"> <!-- Don't fill baseHref otherwise you'll first get a request with invalid URLs -->
<script>
document.getElementById('baseHref').href = (function() {
// NOTE: URI in app module does not need a / suffix. URI in index.html does!
uri = window.location.pathname;
uri = uri.replace('index.html', '');
if (uri.charAt(uri.length-1) !== '/') {
uri += '/';
}
window['__baseurl'] = uri;
/*
// __baseurl can be used to communicate to app.module.ts, for example to make /api/ relative to this new baseURL
providers: [
{
provide: MY_BASE_PATH_FOR_API,
useValue: (function() {
return window['__baseurl'] + 'api';
})()
},
*/
return uri;
})();
</script>
<!-- Dynamic base href -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment