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

Open
#5,339 1 comment 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@imorland is already working on this.

Since Nov 16, 2026.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
javascript, php

Research direction

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.

Written by the indexing model from the issue text.

Description

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.

Dominant language
PHP
Stars
6.8k
Forks
891
Avg merge
1d 10h
Merged PRs (30d)
209

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from flarum/framework

All issues in flarum/framework

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.