Skip to content

Load newsletter Turnstile only after form submission - #117

Merged
Savjee merged 1 commit into
masterfrom
fix/newsletter-turnstile-on-submit
Oct 18, 2026
Merged

Savjee merged 1 commit into
masterfrom
fix/newsletter-turnstile-on-submit

Conversation

@Savjee

@Savjee Savjee commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Newsletter forms currently load Turnstile on every page view. This change loads Cloudflare's script only after a visitor submits a valid email address, obtains a verification token, and then posts the form to the existing Worker for server-side verification.

The form shows verification progress, prevents duplicate submissions, and allows retries after script or challenge failures. Returning with Back restores the form and clears the spent token. Multiple forms share one script load and verify independently.

Validation:

  • All 22 tests pass, including new client tests for lazy loading, retries, duplicate submissions, multiple forms, and browser cache restoration.
  • Production build passes.
  • Browser checks confirm no Turnstile requests on page load, focus, typing, or invalid submissions. Cloudflare's real script and a public test key generated a token that reached an intercepted local signup POST.

Note

Medium Risk
Signup bot protection still depends on Turnstile tokens reaching the existing server verifier; regressions could block legitimate subscribers or weaken abuse resistance if the deferred flow fails in some browsers.

Overview
Newsletter signup no longer pulls Cloudflare Turnstile on every page view. The inline Turnstile script and auto-render widget are removed from newsletter-box.html; forms that have a site key are wired with data-newsletter-signup and a placeholder widget container instead.

A new bundled client module loads Turnstile only after a valid submit: it shows verifying/submitting status, blocks duplicate attempts, renders an explicit interaction-only challenge, appends cf-turnstile-response, then posts once via native form submit (same /newsletter/signup Worker path). Failures are retryable; pageshow resets state when the page is restored from bfcache after Back.

npm test now runs all test/*.test.mjs, including new VM-based tests for lazy load, shared script across multiple forms, failure/retry paths, post-verification validity recheck, and cache restore.

Reviewed by Cursor Bugbot for commit 0870e55. Bugbot is set up for automated code reviews on this repo. Configure here.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying savjee-be with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0870e55
Status: ✅  Deploy successful!
Preview URL: https://9349d464.savjee-be.pages.dev
Branch Preview URL: https://fix-newsletter-turnstile-on.savjee-be.pages.dev

View logs

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is ON, but none of the reported issues met your autofix severity threshold.

Reviewed by Cursor Bugbot for commit 0870e55. Configure here.

// A page restored with Back can retain the disabled button and the spent token.
window.addEventListener('pageshow', (event) => {
if (event.persisted) reset();
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Back restore leaves verification running

Low Severity

reset on pageshow clears pending and the spent token but does not cancel the in-flight submit handler. After Back from bfcache during verification, that handler can still receive a token and call HTMLFormElement.prototype.submit, or a later failure can surface on a form that already looks reset. A second Subscribe click is also allowed while the first attempt is still running.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0870e55. Configure here.

@Savjee
Savjee merged commit 8ea3aaa into master Oct 18, 2026
6 checks passed
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