Skip to content

Let Turbo visit sign-in pages instead of reloading them - #55

Merged
henrikbjorn merged 1 commit into
mainfrom
turbo-visits
Nov 12, 2026
Merged

henrikbjorn merged 1 commit into
mainfrom
turbo-visits

Conversation

@henrikbjorn

@henrikbjorn henrikbjorn commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

The script tag had data-turbo-track="reload". Turbo compares the tracked tags in <head> on every visit and reloads the whole page when they differ, so any Turbo visit to or from a page with the widget became a full page load. Tracking is meant for assets whose URL changes on deploy. The Cloudflare URL never does, and Turbo already appends and runs a new head script on a visit.

Without the reload, two things change for the controller:

  • The script can arrive late. On a Turbo visit from a page without the script, the controller connects before the script has loaded, while turnstile is still undefined. Turbo 8's mergeHead waits only for stylesheets. The controller now waits for the script's load event, using the data-turnstiled attribute the tag now carries to find it.
  • Turbo's cache. A page restored from Turbo's cache still holds the old widget and its spent token, so render empties the element first.

Checked in Tavle with this branch as a path gem, Tavle's own controller replaced by this one, and the Turnstile script delayed by 1 s so it really arrives after connect:

Visit Before After
home → sign in full reload Turbo visit, widget drawn when the script loads, no errors
sign up → terms full reload Turbo visit
sign in ↔ sign up Turbo visit, one widget same
back to sign up one widget one widget

Alternatives tried for the cached page:

Approach Back to sign up
nothing 2 widgets
data-turbo-temporary on the widget no widget, form stuck
turbo-cache-control: no-cache in the app's layout 1 widget, but every app has to remember it
replaceChildren() (this PR) 1 widget

🤖 Generated with Claude Code

@henrikbjorn
henrikbjorn force-pushed the turbo-visits branch 2 times, most recently from 8950f85 to dd2d4bf Compare November 12, 2026 11:44
The script tag carried data-turbo-track="reload", so any Turbo visit to or
from a page with the widget became a full page load. Tracking is for assets
whose URL changes on deploy; this one never does, and Turbo already appends
and runs a new head script on a visit.

On such a visit the script arrives after the controller connects, so the
controller waits for the script's load event. A page restored from Turbo's
cache still holds the old widget and its spent token, so render clears the
element first.
@henrikbjorn
henrikbjorn merged commit a8a62f1 into main Nov 12, 2026
2 of 3 checks passed
@henrikbjorn henrikbjorn mentioned this pull request Nov 12, 2026
henrikbjorn added a commit that referenced this pull request Nov 12, 2026
Turbo visits to sign-in pages no longer reload (#55), and
verify_turnstile_request takes a with: handler or block (#56). Minor
rather than patch because #56 raises the Rails floor to 7.0.
@henrikbjorn
henrikbjorn deleted the turbo-visits branch November 12, 2026 13:11
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