Repository navigation
fix(redirects): give overlapping pattern rules a stable precedence - #3693
Conversation
🦋 Changeset detectedLatest commit: 7dd2545 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
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 |
There was a problem hiding this comment.
This is the right fix for the reported instability: ordering enabled redirects by created_at and id makes pattern precedence deterministic across PostgreSQL row movement, and the change is applied consistently to both the cached load (findAllEnabled) and the direct matcher (findEnabledPatternRules). The regression test is well targeted — it runs on both SQLite and PostgreSQL, inserts overlapping patterns in reverse creation order, and checks both code paths, including created_at ties.
The main gap is index discipline. The new ORDER BY clauses land on the logged-out redirect middleware path (via loadCachedRedirects(() => repo.findAllEnabled())), but _emdash_redirects has no index on created_at or id — only source, enabled, and group_name. Without a supporting index, every cold isolate start now sorts the full enabled redirect set, and on PostgreSQL (the platform called out in the PR description) that sort executes against a table whose rows are rewritten by hit tracking. Add a forward-only migration covering the sort columns before this merges.
The changeset is accurate, proportional, and follows .changeset/README.md.
| @@ -458,6 +461,8 @@ export class RedirectRepository { | |||
| .selectAll() | |||
| .where("enabled", "=", 1) | |||
| .where("is_pattern", "=", 1) | |||
| .orderBy("created_at", (order) => order.asc().nullsFirst()) | |||
| .orderBy("id", "asc") | |||
There was a problem hiding this comment.
[needs fixing] Both findAllEnabled and findEnabledPatternRules now order by created_at ASC NULLS FIRST, id ASC, but _emdash_redirects has no index on created_at or id. The migration (029_redirects.ts) only indexes source, enabled, and group_name.
Because findAllEnabled feeds the logged-out redirect middleware cache (packages/core/src/astro/middleware/redirect.ts), this unindexed sort runs on every cold isolate start. On PostgreSQL — the platform explicitly called out in the PR description — that means sorting a table whose rows are rewritten each time hit tracking updates updated_at.
Add a forward-only migration that covers these order columns. For example:
// packages/core/src/database/migrations/091_redirects_precedence_index.ts
import type { Kysely } from "kysely";
export async function up(db: Kysely<unknown>): Promise<void> {
await db.schema
.createIndex("idx_redirects_enabled_created_at")
.on("_emdash_redirects")
.columns(["enabled", "created_at", "id"])
.execute();
await db.schema
.createIndex("idx_redirects_pattern_enabled_created_at")
.on("_emdash_redirects")
.columns(["is_pattern", "enabled", "created_at", "id"])
.execute();
}
export async function down(_db: Kysely<unknown>): Promise<void> {}If you prefer a single index, idx_redirects_created_at_id on (created_at, id) is still an improvement, though it will not push the enabled/is_pattern filters into the index.
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
docs | 21f08a3 | Sep 30 2026, 01:59 PM |
2095394 to
31badd9
Compare
Overlapping PRsThis PR modifies files that are also changed by other open PRs: This may cause merge conflicts or duplicated work. A maintainer will coordinate. |
Enabled redirects were loaded without an ORDER BY, and both the middleware cache and matchPath return the first matching pattern, so the winner followed physical row order. On PostgreSQL every update (including hit tracking) rewrites the row and can reorder rules. Load enabled rules oldest first, tie-broken by id. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
31badd9 to
38c9cd6
Compare
What does this PR do?
When several enabled pattern redirects match the same path, the winner depended on physical row order.
Enabled rules were loaded without an
ORDER BY, and both the middleware cache andmatchPathreturn the first matching pattern. On PostgreSQL every update, hit tracking included, rewrites the row and can change that order. As a result, which of two overlapping rules wins could change after an unrelated hit.Enabled rules are now loaded oldest first by
created_at, with ties broken byid. Cached matching and direct matching use that same order.This is stacked on #3692 and is the second of three PRs from work on #2288.
Closes # (no issue; found while working on #2288)
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runAI-generated code disclosure
Screenshots / test output
Not applicable (no UI change).
redirect-pattern-precedence.test.tsruns on SQLite and PostgreSQL. It inserts overlapping rules in the reverse of their creation order and checks that both cached and direct matching return the earliest-created rule, then thatcreated_atties are broken byid.🤖 Generated with Claude Code