Skip to content

fix(astro): Insert Clerk's tags once before the closing head tag (Core 2) - #10901

Merged
wobsoriano merged 1 commit into
release/core-2from
rob/core-2-astro-head-tags
Nov 18, 2026
Merged

wobsoriano merged 1 commit into
release/core-2from
rob/core-2-astro-head-tags

Conversation

@wobsoriano

Copy link
Copy Markdown
Member

Description

Backports two @clerk/astro fixes from main to Core 2.

Head tags. clerkMiddleware used to look for the bytes </head> in each streamed chunk and insert Clerk's tags before every match. It missed a </head> that was split across two chunks, and it could insert at a </head> that wasn't the document's real closing tag. It now uses createHeadInjector, which tracks the HTML tokenizer state across chunks and inserts once before the real </head>, or before <body> when the page has no </head>.

The inserted markup changes too:

<meta name=clerk-astro-data content=eyJ1c2VySWQiOm51bGwsLi4ufQ>
<meta name=clerk-astro-safe-vars content=eyJpc1NhdGVsbGl0ZSI6ZmFsc2UsLi4ufQ>
<link rel=preload as=script href=https://example.clerk.accounts.dev/npm/@clerk/clerk-js@5/dist/clerk.browser.js crossorigin=anonymous>

The integration's page script reads both <meta> tags and adds the clerk-js <script>. The browser reuses the preloaded response. The server used to decide the clerk-js URL, so getClientSafeEnv now also passes the publishable key and PUBLIC_CLERK_JS_URL, PUBLIC_CLERK_JS_VARIANT, and PUBLIC_CLERK_JS_VERSION. Env vars set only at runtime, like Cloudflare locals.runtime.env, still reach the client.

$billingStore. The store now emits clerk.billing only after clerk.load() resolves. Before, a subscriber could call getPlans() before the dev browser was set up and get dev_browser_unauthenticated on development instances. Same fix as #10894.

@clerk/astro had no unit tests on this branch, so this adds vitest.config.ts, vitest.setup.ts, and a test script, matching main.

How to test

  • pnpm --filter @clerk/astro test
  • I packed the package and ran it in an Astro 5 app with @astrojs/node. Clerk 5.128.2 loaded from the preloaded URL, Clerk.loaded was true, and the FAPI requests carried the dev browser JWT.

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

🤖 Generated with Claude under Rob's supervision.

…e 2)

Backport the head tag changes and the $billingStore load gate from main.

clerkMiddleware now scans the streamed HTML with createHeadInjector, which
tracks tokenizer state across chunks and inserts once before </head>, or
before <body> when </head> is missing. The SSR auth state and client env go
in base64url <meta> tags, and clerk-js is preloaded with a <link> tag. The
integration's page script adds the clerk-js script, so getClientSafeEnv now
also passes the publishable key and the clerk-js URL, variant, and version.

$billingStore now emits only after clerk.load() resolves.

Adds vitest to @clerk/astro on this branch so the new tests run in CI.
@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
clerk-js-sandbox Ready Ready Preview Nov 18, 2026 5:56pm UTC
mosaic-docs Error Error Nov 18, 2026 5:56pm UTC
swingset Error Error Nov 18, 2026 5:56pm UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6a0d6c8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@clerk/astro Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@pkg-pr-new

pkg-pr-new Bot commented Nov 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@10901

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@10901

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@10901

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@10901

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@10901

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@10901

@clerk/elements

npm i https://pkg.pr.new/@clerk/elements@10901

@clerk/clerk-expo

npm i https://pkg.pr.new/@clerk/clerk-expo@10901

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@10901

@clerk/express

npm i https://pkg.pr.new/@clerk/express@10901

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@10901

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@10901

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@10901

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@10901

@clerk/clerk-react

npm i https://pkg.pr.new/@clerk/clerk-react@10901

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@10901

@clerk/remix

npm i https://pkg.pr.new/@clerk/remix@10901

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@10901

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@10901

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@10901

@clerk/themes

npm i https://pkg.pr.new/@clerk/themes@10901

@clerk/types

npm i https://pkg.pr.new/@clerk/types@10901

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@10901

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@10901

commit: 6a0d6c8

@blacksmith-sh

blacksmith-sh Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Matches Tags inserted once before the real </head>

Without the PR, the tags land inside a script string and are missing on the split-chunk and no-</head> pages; with it, they appear once before the real </head>, or before <body> when there is none.

Merge-base

Middleware output

This PR 6a0d6c8c

Middleware output

Also checked · 2 match · 1 not run

Blocked Clerk loads against a real dev instance · Blocked
Matches clerk-js loads from the preload with runtime-only keys
Matches $billingStore waits for clerk.load()

Important

Blocked on missing secrets

  • Clerk loads against a real dev instance: Needs a real Clerk development instance's keys; the sandbox has none, so Clerk.load(), the dev browser JWT and getPlans() were not exercised.

Proof on 6a0d6c8c · mocked: Clerk Frontend/Backend API (needs PUBLIC_CLERK_PUBLISHABLE_KEY, CLERK_SECRET_KEY) · Did Proof get it right? Yes · No

View full Proof report · React 👎 or comment @codesmith dismiss this proof to dismiss

@wobsoriano
wobsoriano merged commit 06ddcd7 into release/core-2 Nov 18, 2026
44 of 47 checks passed
@wobsoriano
wobsoriano deleted the rob/core-2-astro-head-tags branch November 18, 2026 18:31

This branch had an error being deployed

1 failed and 1 active deployments
Preview – clerk-js-sandbox — 6a0d6c8c Deployed Nov 18, 2026 by vercel[bot]
Preview – swingset — 6a0d6c8c Deployed Nov 18, 2026 by vercel[bot]
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.

2 participants