-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'base' element to 'head' to normalize relative urls/references (#…
…2263) * Add 'base' element to 'head' to normalize relative urls/references * Try to format * Try to format * Prettier++ * Using document.head, wrapping closure to prevent global pollution (dunno if that happens anymore with all the preprocessing, but better to be safe) * Block-scoping instead of closure --------- Co-authored-by: Ole Martin Handeland <[email protected]>
- Loading branch information
1 parent
ab2a4f8
commit 2622dcd
Showing
2 changed files
with
18 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Apps are running in a subpath of `/{org}/{app}` both locally and in deployed environments | ||
// some features make use of relative paths that should resolve relative to this subpath. | ||
// * `image.src` on the `ImageComponent` might have `some-image.jpg` which we expect to then exist in the `wwwroot/` | ||
// folder of the app. | ||
// The `base` element in the HTML head will make relative references resolve from `base.href`. | ||
// Bugreport: https://github.com/Altinn/app-frontend-react/issues/2257 | ||
{ | ||
const base = document.createElement('base'); | ||
const { protocol, hostname, port: _port } = window.location; | ||
const { org, app } = window; | ||
const isDefaultHttpsPort = protocol === 'https:' && _port === '443'; | ||
const isDefaultHttpPort = protocol === 'http:' && _port === '80'; | ||
const isDefaultPort = isDefaultHttpsPort || isDefaultHttpPort; | ||
const port = _port && !isDefaultPort ? `:${_port}` : ''; | ||
base.href = `${protocol}//${hostname}${port}/${org}/${app}/`; | ||
document.head.appendChild(base); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters