[2.x] bbcode: highlight.js loader runs once per code block and adds ~1.1 s TBT on mobile

Aperta
#5,339 1 commento 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@imorland ci sta già lavorando.

Dal 16/11/2026.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
javascript, php

Direzione di ricerca

Start with extensions/bbcode/src/Configure.php and its adaptHighlightJs() entry point, then inspect the s9e CODE template behavior described in the issue. Compare the loader’s repeated script requests and Lighthouse measurements against the suggested approaches; the issue does not choose a solution, so agree on the design with maintainers before implementation. Done means an agreed approach addresses the performance cost and the opt-out or configuration needs.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

On any discussion with code blocks, the bbcode extension's CODE template makes the browser load s9e/hljs-loader from cdn.jsdelivr.net, and running that loader is a large share of the page's main-thread blocking time.

How it gets there: extensions/bbcode/src/Configure.php adds the CODE BBCode from the s9e repository and adaptHighlightJs() appends a small inline script to the template. The s9e template itself emits <script async data-hljs-style="github" src="https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js"> inside each <pre>. The rendered HTML therefore contains one loader <script> per code block (10 on the page below), and the loader then fetches highlight.min.js 11.11.0 and a github.min.css from the same CDN.

Measurement: Lighthouse 13.5.0, mobile emulation, 4x CPU slowdown, https://linkrobins.com/d/61-support (Flarum 2.0.2, 10 code blocks), run twice:

  • Total Blocking Time 2,790 ms and 2,670 ms.
  • Lighthouse "bootup time": [email protected]/loader.min.js is the second largest script after forum.js, about 1,159 ms total, 1,117 ms of it scripting. highlight.min.js adds another ~81 ms.
  • Performance score 0.36 and 0.38. The page has no other third-party script of significance.

I have not profiled inside the loader, so I can't say how much of the cost is the per-block repeated script elements versus the loader itself.

Suggestions, in no particular order:

  1. Emit the loader once per post (or once per page) instead of once per code block.
  2. Load it after load or on idle (requestIdleCallback), and only highlight blocks when they are near the viewport.
  3. Offer a smaller build with common languages instead of whatever the loader pulls in.
  4. Provide a config/setting or extender hook to disable syntax highlighting or point it at a self-hosted copy, so forums that care about TBT, CSP or third-party requests can opt out.

I'm not proposing a code change, as this looks like a design decision for the maintainers. Related earlier discussion in s9e/TextFormatter: #193 (customize the hljs load URL) and #206.

Lingua principale
PHP
Stelle
6.8k
Fork
891
Merge medio
1g 10h
PR unite (30g)
209

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di flarum/framework

Tutte le issue di flarum/framework

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.