Skip to content

fix: stop prerendering two browsers, never resend a failed origin request, fall back on 5xx - #8

Open
OpsBeaconCharles wants to merge 1 commit into
mainfrom
fix/worker-fail-open-and-browser-agents
Open

OpsBeaconCharles wants to merge 1 commit into
mainfrom
fix/worker-fail-open-and-browser-agents

Conversation

@OpsBeaconCharles

Copy link
Copy Markdown

Fixes #7.

Changes

  • Two human browsers removed from BOT_AGENTS: yabrowser (Yandex Browser) and sznprohlizec (Seznam's browser). Matching is by substring, so real visitors on those browsers were getting Prerender snapshots and using the site's render quota. The Yandex and Seznam crawlers are still matched, through yandexbot and seznambot.
  • A failed origin request is never sent twice. Only the Prerender call is now inside the try. The old .catch(() => fetch(request)) also wrapped the non-bot fetch(request). A rejected origin request, such as a POST on a connection reset, was therefore sent again, or threw "body already used". ctx.passThroughOnException() still covers anything thrown outside the try.
  • Fail open on 5xx too. When Prerender answers with a status of 500 or above, the Worker now serves the origin, as it already did when the fetch throws. A 4xx from Prerender is still returned as rendered.
  • Smaller changes:
    • BOT_AGENTS entries are already lowercase, so the per-request bot.toLowerCase() is gone. A comment says to keep the entries lowercase.
    • The decision and the outgoing request are split into shouldPrerender / prerenderRequest.
    • The forwarded-header allowlist is unchanged.

Deliberately not changed

  • Broad tokens: pinterest, tiktok, naver and ecosia are broad substrings that may also match in-app browsers or apps; for example, Pinterest's in-app browser UA contains "Pinterest". I didn't verify these and left them alone. Narrowing them to the crawler tokens (pinterestbot, tiktokspider, …) needs a check against real UAs first. That's also why the redundant specific entries stay: they're what's left if the broad ones go.
  • The bot list's contents: otherwise as before.

Testing

I ran the old and new worker.js against a stubbed fetch on 12 cases:

  • bot prerendered with only allowlisted headers;
  • Yandex Browser and Seznam browser visitors served from the origin, while YandexBot and SeznamBot are still prerendered;
  • a failed human POST sent to the origin once;
  • a Prerender 504 falling back to the origin;
  • a Prerender throw falling back to the origin;
  • a Prerender 404 returned as is;
  • static assets, X-Prerender requests and ordinary Chrome all going to the origin.

Results: the old Worker fails exactly the 4 bug cases (8/12), and the new one passes 12/12. The repo has no test harness, so the script isn't included.

After merge

The Prerender dashboard ships a verbatim copy of this file (views/dashboard/public/assets/markdown/onboarding/integrationContents/cloudflare.md), and the public docs show it too. Both get re-copied from main once this merges.

🤖 Generated with Claude Code

…uest, fall back on 5xx

- Drop yabrowser and sznprohlizec from BOT_AGENTS: they are the UA tokens
  of Yandex Browser and Seznam's browser, so human visitors got snapshots
  and used the customer's render quota.
- Guard only the Prerender call. The catch used to wrap the non-bot origin
  fetch too, so a rejected origin request (e.g. a POST) was sent again.
- Fall back to the origin when Prerender answers 5xx, not only when the
  fetch throws.
- BOT_AGENTS entries are already lowercase; stop lowercasing each one on
  every request.

Fixes #7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

worker.js: browser UA tokens in BOT_AGENTS, catch re-sends the origin request, 5xx not failed open

1 participant