Docs: ensure stackblitz.js loads conditionally as intended#41482
Docs: ensure stackblitz.js loads conditionally as intended#41482julien-deramond merged 2 commits intomainfrom
stackblitz.js loads conditionally as intended#41482Conversation
| <script src="../assets/search.js"></script> | ||
|
|
||
| {layout === 'docs' && <script src="../assets/stackblitz.js" />} | ||
| {layout === 'docs' && <DocsScripts />} |
There was a problem hiding this comment.
Good catch @louismaximepiton — and nice patch here Orange-OpenSource/Orange-Boosted-Bootstrap@a91f7d3 @vprothais! I'll add both of you in the final commit as co-authors.
I can confirm that prior to your change, adding a simple console.log() in assets/stackblitz.js shows it being loaded on the homepage, docs, and examples — which it shouldn't. That confirms the script is being included globally, and the conditional logic isn't working as intended.
I haven't reviewed the entire Astro issues, but this one seems closely related: withastro/astro#13550. It mentions:
When you put it in a separate component, the script belongs permanently with that component. Then you are conditionally rendering the component here. That aligns with how Astro works.
This lines up well with the behavior we're seeing — so yes, moving this into a component makes sense.
Could you please extract the other fix @louismaximepiton into a separate PR? That way I can merge this one right away, and review the second fix independently later on. 🙏
3b787f2 to
bf49aa4
Compare
stackblitz.js loads conditionally as intended
There was a problem hiding this comment.
const eventRegistry = {};
let uidCounter = 1;
function makeEventUid(element) {
return element.uidEvent || (element.uidEvent = `uid-${uidCounter++}`);
}
function getElementEvents(element) {
const uid = makeEventUid(element);
element.uidEvent = uid;
eventRegistry[uid] = eventRegistry[uid] || {};
return eventRegistry[uid];
}
function hydrateObj(obj, props) {
for (const key in props) {
obj[key] = props[key];
}
}
const EventHandler = {
off: function (element, eventType, fn) {
element.removeEventListener(eventType, fn);
}
};
function bootstrapHandler(element, fn) {
return function handler(event) {
hydrateObj(event, {
delegateTarget: element
});
if (handler.oneOff) {
EventHandler.off(element, event.type, fn);
}
return fn.apply(element, [event]);
};
}
Description & Motivation & Context
During backporting the Astro commit to our doc, there was an issue regarding the Scripts bundled by Astro. I couldn't spot it on Bootstrap side but on our side. The
stackblitz.jsis loaded on the landing page (main branch) and not theapplication.jscontrary at what is specified inScripts.astro.Special thanks to @vprothais !
Type of changes
Checklist
npm run lint)Live previews
Related issues
NA