-
Notifications
You must be signed in to change notification settings - Fork 9
feat!: make external scripts configurable per-app #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,122 @@ | ||
| Per-App External Scripts | ||
| ######################### | ||
|
|
||
| Status | ||
| ====== | ||
|
|
||
| Proposed | ||
|
|
||
|
|
||
| Context | ||
| ======= | ||
|
|
||
| frontend-base previously loaded external scripts during the ``initialize()`` | ||
| call, with a hardcoded default. The list of external scripts was not | ||
| configurable by operators, meaning there was no way to add, remove, or replace | ||
| scripts without modifying the platform itself. The hardcoded default also | ||
| meant every site paid the cost of bundling it whether it was used or not. | ||
|
|
||
|
|
||
| Decision | ||
| ======== | ||
|
|
||
| Add an optional ``externalScripts`` field to the ``App`` interface:: | ||
|
|
||
| export interface App { | ||
| appId: string, | ||
| routes?: RoleRouteObject[], | ||
| providers?: AppProvider[], | ||
| slots?: SlotOperation[], | ||
| externalScripts?: ExternalScriptLoaderClass[], | ||
| config?: AppConfig, | ||
| provides?: Record<string, unknown>, | ||
| } | ||
|
|
||
| Each entry is a class that implements ``ExternalScriptLoader``:: | ||
|
|
||
| export interface ExternalScriptLoader { | ||
| loadScript(): void, | ||
| } | ||
|
|
||
| export type ExternalScriptLoaderClass = | ||
| new (data: { config: AppConfig }) => ExternalScriptLoader; | ||
|
|
||
| During initialization, the runtime iterates all apps and instantiates each | ||
| app's external scripts with that app's merged config (``commonAppConfig`` + | ||
| per-app ``config``). Scripts from different apps accumulate without clobbering | ||
| each other, following the same pattern as ``routes`` and ``slots``. | ||
|
|
||
| frontend-base itself ships no external script loaders. Operators write their | ||
| own (or pull them from a third-party package) and attach them to an app in | ||
| their ``site.config``. | ||
|
|
||
|
|
||
| Consequences | ||
| ============ | ||
|
|
||
| Operators who want to load an external script author an ``ExternalScriptLoader`` | ||
| class and attach it to one of their apps via ``externalScripts``. The | ||
| configuration the loader reads (e.g. an analytics ID) lives in that app's | ||
| ``config`` or in ``commonAppConfig``, not as a top-level ``SiteConfig`` key. | ||
|
|
||
| Because loaders are per-app, multiple apps can each contribute their own | ||
| scripts without conflict, and an app that doesn't need any scripts doesn't | ||
| pay the cost of loaders it doesn't use. | ||
|
|
||
|
|
||
| Example | ||
| ======= | ||
|
|
||
| An operator-authored loader (for instance, a third-party consent banner) and | ||
| its wiring in ``site.config``:: | ||
|
|
||
| class ConsentBannerLoader { | ||
| constructor({ config }) { | ||
| this.siteId = config.consentBannerSiteId; | ||
| } | ||
|
|
||
| loadScript() { | ||
| if (!this.siteId) return; | ||
| const script = document.createElement('script'); | ||
| script.id = 'consent-banner'; | ||
| script.src = `https://example.com/client_data/${this.siteId}/script.js`; | ||
| document.head.appendChild(script); | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| apps: [ | ||
| { | ||
| appId: 'org.example.app.consentBanner', | ||
| externalScripts: [ConsentBannerLoader], | ||
| config: { | ||
| consentBannerSiteId: 'YOUR_SITE_ID', | ||
| }, | ||
|
arbrandes marked this conversation as resolved.
|
||
| }, | ||
| // ...other apps | ||
| ], | ||
| }; | ||
|
|
||
| The ``consentBannerSiteId`` value shown above is a static default. Operators | ||
| can override it at runtime without modifying ``site.config``. | ||
|
|
||
|
|
||
| Rejected alternatives | ||
| ===================== | ||
|
|
||
| Site-level ``externalScripts`` | ||
| ------------------------------ | ||
|
|
||
| An earlier iteration added ``externalScripts`` to ``OptionalSiteConfig``, | ||
| letting operators override scripts at the site level. This was rejected | ||
| because a site-level array replaces rather than composes: there is no way for | ||
| multiple apps to each contribute their own scripts independently. | ||
|
|
||
| A ``contrib/`` directory for pre-built loaders | ||
| ----------------------------------------------- | ||
|
|
||
| An earlier iteration moved ``GoogleAnalyticsLoader`` to a new top-level | ||
| ``contrib/`` directory for optional, pre-built apps. This was rejected | ||
| because a single loader does not justify a new directory, its build wiring, | ||
| and a category of code; operators can author their own loaders with no loss | ||
| of functionality. | ||
This file contains hidden or 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
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,12 @@ | ||
| export { default as GoogleAnalyticsLoader } from './GoogleAnalyticsLoader'; | ||
| import { getAppConfig, getSiteConfig } from '../config'; | ||
|
|
||
| export function loadExternalScripts() { | ||
| const apps = getSiteConfig().apps ?? []; | ||
| apps.forEach(app => { | ||
| const config = getAppConfig(app.appId) ?? {}; | ||
| (app.externalScripts ?? []).forEach(ExternalScript => { | ||
| const script = new ExternalScript({ config }); | ||
|
brian-smith-tcril marked this conversation as resolved.
|
||
| script.loadScript(); | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.