Skip to content

Commit

Permalink
feat: support core-injected base url (#836)
Browse files Browse the repository at this point in the history
* feat: support core-injected base url

* style: format

* feat: add base URL injection point to plugins

* chore: comment for context

* chore: fix comment

---------

Co-authored-by: Kai Vandivier <[email protected]>
  • Loading branch information
amcgee and KaiVandivier authored Mar 25, 2024
1 parent e666f1f commit 3f1ca71
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions shell/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#ffffff" />

<!-- Injection point for base URL from backend -->
<meta name="dhis2-base-url" content="__DHIS2_BASE_URL__" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
Expand Down
2 changes: 2 additions & 0 deletions shell/public/plugin.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<!-- Injection point for base URL from backend -->
<meta name="dhis2-base-url" content="__DHIS2_BASE_URL__" />
<title>Plugin</title>
</head>
<body>
Expand Down
14 changes: 13 additions & 1 deletion shell/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ const D2App = React.lazy(() =>
import(/*webpackChunkName: 'app'*/ './D2App/app')
) // Automatic bundle splitting!

// Injected by backend when serving index or plugin HTML
// https://github.com/dhis2/dhis2-core/pull/16703
const getInjectedBaseUrl = () => {
const baseUrl = document
.querySelector('meta[name="dhis2-base-url"]')
?.getAttribute('content')
if (baseUrl && baseUrl !== '__DHIS2_BASE_URL__') {
return baseUrl
}
return null
}

const parseRequiredProps = (propsEnvVariable) => {
if (!propsEnvVariable || propsEnvVariable === '') {
return []
Expand All @@ -22,7 +34,7 @@ const requiredPluginProps = parseRequiredProps(
)

const appConfig = {
url: process.env.REACT_APP_DHIS2_BASE_URL,
url: getInjectedBaseUrl() || process.env.REACT_APP_DHIS2_BASE_URL,
appName: process.env.REACT_APP_DHIS2_APP_NAME || '',
appVersion: process.env.REACT_APP_DHIS2_APP_VERSION || '',
apiVersion: parseInt(process.env.REACT_APP_DHIS2_API_VERSION),
Expand Down

0 comments on commit 3f1ca71

Please sign in to comment.