Skip to content

fix: sidebar hint enfin fonctionnel — injection dans parent.document.head - #32

Merged
SKOHscripts merged 1 commit into
mainfrom
claude/fix-sidebar-hint-v4
May 26, 2026
Merged

fix: sidebar hint enfin fonctionnel — injection dans parent.document.head#32
SKOHscripts merged 1 commit into
mainfrom
claude/fix-sidebar-hint-v4

Conversation

@SKOHscripts

Copy link
Copy Markdown
Owner

Diagnostic root cause

st.html() et st.components.v1.html() utilisent tous les deux des iframes. Les timers créés dans un iframe sont liés à son event loop — quand Streamlit re-render la page et recrée l'iframe, les timers s'arrêtent. C'est pourquoi la flèche n'apparaissait jamais : le setTimeout(20s) mourait avant d'avoir eu le temps de déclencher.

Fix

Le script dans l'iframe injecte un élément <script> directement dans window.parent.document.head :

var ps = p.document.createElement('script');
ps.textContent = /* script principal */;
p.document.head.appendChild(ps);  // ← exécute immédiatement dans le contexte de la page principale
ps.remove();

Les scripts inline ajoutés dynamiquement s'exécutent immédiatement et synchroniquement dans le contexte de la fenêtre principale. Leurs timers tournent dans l'event loop de la page principale et survivent aux re-renders.

Guards

  • window.parent._sbHintInit : le script iframe ne s'injecte qu'une fois
  • window._sbRunInit : le script principal ne tourne qu'une fois
  • window._sbShowing : le poll ne peut dismisser qu'après que le timer de 20s a effectivement affiché la flèche (évite le sbVisited prématuré si la sidebar était déjà ouverte au chargement)

Plan de test

  • App sans DB, sidebar fermée → attendre 20s → flèche ↖ « Ouvrir le menu » apparaît
  • Ouvrir la sidebar → flèche disparaît immédiatement
  • Recharger → flèche ne réapparaît plus
  • App sans DB, sidebar déjà ouverte → flèche n'apparaît jamais (correct)

https://claude.ai/code/session_01VTBKKYLiAQ6mDPdmVhpRAT


Generated by Claude Code

Both st.html() and st.components.v1.html() use iframes. Timers
created inside an iframe are tied to its event loop and stop when
the iframe is destroyed by a Streamlit re-render — which is why the
hint never appeared.

Fix: the iframe script injects a <script> element directly into
window.parent.document.head. Inline scripts appended to a document
execute immediately and synchronously in that document's window
context. Their timers run in the main page event loop and survive
re-renders.

The iframe script is guarded by window.parent._sbHintInit so it
only injects once. The main-page script is guarded by
window._sbRunInit. The poll only dismisses after window._sbShowing
is true (set by the 20 s timer) to prevent premature sbVisited.

https://claude.ai/code/session_01VTBKKYLiAQ6mDPdmVhpRAT
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@SKOHscripts
SKOHscripts marked this pull request as ready for review May 26, 2026 21:22
@SKOHscripts
SKOHscripts merged commit bc699ea into main May 26, 2026
5 checks passed
@SKOHscripts
SKOHscripts deleted the claude/fix-sidebar-hint-v4 branch May 26, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants