TwilightBundles
Watches the page for Twilight bundle elements and loads Salla's bundles runtime the first time one appears; mounted on every page.
import { TwilightBundles } from '@salla.sa/twilight-theme-engine/tracking';In plain words
Twilight bundles are components a merchant installs from outside the theme. They need a script from Salla to work, but most pages have none, so loading it everywhere would waste time.
TwilightBundles watches the page, and only when a bundle element shows up does it add that script. It draws nothing, and the engine already renders it on every page.
Signature
function TwilightBundles(): null
Try it live
import { TwilightBundles } from '@salla.sa/twilight-theme-engine/tracking';
// TwilightProvider already renders it at body:end on every page.
// A second instance does nothing once the runtime script is on the page.
export function Bundles() {
return <TwilightBundles />;
}
Example
import { TwilightBundles } from '@salla.sa/twilight-theme-engine/tracking';
// TwilightProvider already renders this at body:end on every page.
export function Bundles() {
return <TwilightBundles />;
}
How it behaves
In an effect it starts a
MutationObserverondocument.body(kept onwindow.twilightBundlesObserver). When asalla-custom-componentelement is added, or one is already there, it adds<script type="module" id="twilight-bundles-assets">loadingtwilight-bundles.js(thelatestbuild) fromcdn.assets.salla.network, then disconnects the observer.The default
body:endhandler renders it on every page (priority 100).BundleComponentis what puts such elements on the home page.
Gotchas
The observer looks only at the nodes that were added, not inside them. A bundle element that arrives inside a newly inserted wrapper (a section rendered in one go) is missed, and the runtime never loads.
BundleComponentavoids this by rendering its element one render after mounting, into a wrapper already on the page; render your own bundle elements the same way.If the script is already on the page when it mounts, it returns without installing an observer, so a second instance does nothing.
Its source comment says it uses an import map for Lit; it adds none.
Related
Hosts a home block that comes from an installed Twilight bundle (a component built outside the theme), rendered in the browser only.
registerDefaultHooksRegisters the engine's built-in slot handlers (GTM, bundles, store closed, fast checkout, price quote, pre-order); importing the hooks module already runs it.