Skip to content

fix(stats): add CACHE_KV binding to fix login redirect loop#1010

Open
lane711 wants to merge 7 commits into
mainfrom
lane711/stats-login-redirect-loop
Open

fix(stats): add CACHE_KV binding to fix login redirect loop#1010
lane711 wants to merge 7 commits into
mainfrom
lane711/stats-login-redirect-loop

Conversation

@lane711

@lane711 lane711 commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • stats.sonicjs.com login succeeded but immediately redirected back to "Please login to access the admin area"
  • Root cause: packages/stats/wrangler.toml had no CACHE_KV binding — Better Auth's withCloudflare received kv: undefined, session writes silently failed, so c.get('user') was always undefined on the next request
  • Fix: adds CACHE_KV KV namespace binding (reusing a16f8246fc294d809c90b0fb2df6d363) to both default and [env.production] sections

Test plan

  • Log in to stats.sonicjs.com — should land in admin, not loop back to login
  • Verify BETTER_AUTH_SECRET wrangler secret is set on sonicjs-stats worker (run wrangler secret list --name sonicjs-stats)

🤖 Generated with Claude Code

Without CACHE_KV, withCloudflare had kv=undefined — session writes
silently failed, causing a login-success → redirect → "please login"
loop on stats.sonicjs.com.

Reuses the shared KV namespace (a16f8246fc294d809c90b0fb2df6d363)
for both default and production envs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…anges

Removes the path filter so stats.sonicjs.com always runs the latest
version of @sonicjs-cms/core after any merge to main.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
wrangler >=4 requires Node >=22. Lock file was also out of sync with
current esbuild/emnapi versions; npm install resolves fresh rather
than failing on the stale lock file.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…oard

- Removes LIMIT 20 / slice(15) so all collections appear in the table
- Sorts by installations desc (most-adopted first) — chart still capped at top 20
- Adds "Custom Collections" side-by-side card: filters to installations = 1
  (user-defined collections unique to one install), sorted by doc count
- Both tables are scrollable (max-h-96) so the page doesn't balloon

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ding

bootstrap.ts read c.env.KV but the binding is named CACHE_KV in wrangler.toml.
KV was always undefined → installationId defaulted to 'unknown' on every
project_snapshot event, making per-installation analytics useless.

Falls back to c.env.KV for any deployment that uses the legacy name.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
SQL bug: HAVING created_at = MAX(created_at) picked collection_counts
arbitrarily (SQLite GROUP BY semantics) — most snapshot rows were silently
dropped. Now reads collection_names from every snapshot row directly.

Logic bug: "installations = 1" as the custom filter was wrong since all
existing snapshots carry installation_id='unknown', collapsing them all
into one fake installation. Replaced with a SYSTEM_COLLECTIONS exclusion
set (menu_item, plugin, plugin_activity, rbac_role, rbac_verb,
rbac_user_roles, content, media) — everything else is user-defined.

System collections shown dimmed with [sys] label in the All Collections
table so they're still visible but visually deprioritized.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Captures the scheme+host of the first cold-start request so the stats
dashboard can show which domains are running SonicJS. No path, query
string, or user data is collected — only the origin (e.g.
"https://example.com").

Dashboard adds a "Deployed Sites" table showing all unique origins +
snapshot counts. Hidden when no deploy_url data exists yet.

Co-Authored-By: Claude Sonnet 4.6 <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.

1 participant