Skip to content

Defer the Turnstile api.js head script (parser-blocking on every page)#6

Open
ekumanov wants to merge 1 commit into
flectar:2.xfrom
ekumanov:defer-turnstile-api-script
Open

Defer the Turnstile api.js head script (parser-blocking on every page)#6
ekumanov wants to merge 1 commit into
flectar:2.xfrom
ekumanov:defer-turnstile-api-script

Conversation

@ekumanov

@ekumanov ekumanov commented Jul 3, 2026

Copy link
Copy Markdown

One-line fix for #5: add defer to the injected api.js script tag so it no longer parser-blocks every page render behind a third-party fetch (~150–400 ms at mobile p75; flagged by PSI's render-blocking audit and it delays FCP/LCP directly).

defer downloads in parallel and executes right after DOM parsing — still well before a user can open an auth modal, so window.turnstile is available when TurnstileState.render() runs. render=explicit semantics are unchanged.

Running in production on a Flarum 2.0.0-rc.4 forum: pages no longer block on challenges.cloudflare.com, and the sign-up widget renders and completes normally.

If you'd also like the ?onload= + pending-render-queue hardening described in #5 (covers the theoretical modal-open-before-DOM-parsed race), happy to follow up with that separately.

A classic <script> in <head> parser-blocks every page render behind a
third-party fetch (DNS + TLS + ~30 KB from challenges.cloudflare.com),
roughly 150-400 ms at mobile p75, on all pages including ones that never
show an auth modal. With defer the script downloads in parallel and runs
right after DOM parsing - still well before a user can open an auth
modal, so window.turnstile is ready when TurnstileState.render() runs.

Fixes flectar#5
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.

1 participant