Skip to content

Conversation

@just-be-dev
Copy link
Owner

Support /[code] redirects to full URLs (e.g., /b232e -> /blog/b232e/slug/). The middleware now detects 5-character codes at the root level and redirects to the canonical URL using a 301 permanent redirect.

  • Add KIND_TO_COLLECTION mapping to code.ts
  • Add getCollection() method to Code class
  • Update middleware to handle root-level short code URLs
  • Fix test for error message to include T kind

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Dec 23, 2025

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
just-be-dev a541bba Dec 23 2025, 08:27 PM

const url = new URL(context.request.url);

// Handle short code URLs like /b232e -> /blog/b232e/slug/
const shortCodeMatch = url.pathname.match(/^\/([brpt][0-9a-f]{4})\/?$/i);
Copy link
Owner Author

Choose a reason for hiding this comment

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

@claude the brpt shouldn't be hard coded here either. Pull those from KIND_TO_COLLECTION too.

Copy link

@claude claude bot Dec 23, 2025

Choose a reason for hiding this comment

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

Claude finished @just-be-dev's task in 1m 52s —— View job


Done! ✓

  • Read src/middleware.ts and src/utils/code.ts
  • Replace hardcoded "brpt" with dynamic value from KIND_TO_COLLECTION
  • Commit changes locally

What I Changed

Replaced the hardcoded "brpt" string in the regex pattern at src/middleware.ts:9 with a dynamic pattern derived from KIND_TO_COLLECTION keys:

Before:

const shortCodeMatch = url.pathname.match(/^\/([brpt][0-9a-f]{4})\/?$/i);

After:

const kindChars = Object.keys(KIND_TO_COLLECTION).join("").toLowerCase();
const shortCodePattern = new RegExp(`^\\/([${kindChars}][0-9a-f]{4})\\/?$`, "i");
const shortCodeMatch = url.pathname.match(shortCodePattern);

Now the short code pattern dynamically includes all kinds defined in KIND_TO_COLLECTION without any hardcoded values. The pattern extracts the keys from KIND_TO_COLLECTION (B, R, P, T), joins them, and lowercases them to create the character class [brpt] in the regex.

Push Status

The changes are committed locally but there are conflicting updates on the remote branch. To complete this, you'll need to either:

  • Approve git pull --rebase in my allowed tools, or
  • Manually pull and push the changes

The commit is ready and waiting: 88c2be1 Use KIND_TO_COLLECTION for dynamic short code pattern


| Branch: claude/short-url-redirects-U4Gs0

Copy link
Owner Author

Choose a reason for hiding this comment

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

Okay @claude, try again

Copy link

@claude claude bot Dec 23, 2025

Choose a reason for hiding this comment

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

Claude finished @just-be-dev's task in 1m 21s —— View job


Done! ✓

  • Pull latest changes from remote
  • Read current middleware.ts and code.ts
  • Replace hardcoded "brpt" with dynamic KIND_TO_COLLECTION pattern
  • Commit and push changes

What I Changed

Replaced the hardcoded [brpt] character class in the short code regex pattern at src/middleware.ts:9 with a dynamic pattern derived from KIND_TO_COLLECTION keys:

Before:

const shortCodeMatch = url.pathname.match(/^\/([brpt][0-9a-f]{4})\/?$/i);

After:

const kindChars = Object.keys(KIND_TO_COLLECTION).join("").toLowerCase();
const shortCodePattern = new RegExp(`^\\/([${kindChars}][0-9a-f]{4})\\/?$`, "i");
const shortCodeMatch = url.pathname.match(shortCodePattern);

Why This Is Better

Now the middleware has zero hardcoded collection names or kind characters. Everything is derived from KIND_TO_COLLECTION:

  1. Short code pattern (line 9): Extracts keys from KIND_TO_COLLECTION"BRPT" → lowercases to "brpt" → builds regex [brpt]
  2. Collection path pattern (line 36): Extracts values from KIND_TO_COLLECTION"blog|projects|research|talks" → builds regex ^/(blog|projects|research|talks)/
  3. Type assertion (line 39): Uses Collection type derived from KIND_TO_COLLECTION

If you ever add a new collection type (e.g., W: "workshops"), you only need to update KIND_TO_COLLECTION in src/utils/code.ts and both URL handlers will automatically support it.


| Branch: claude/short-url-redirects-U4Gs0

claude and others added 4 commits December 23, 2025 15:24
Support /[code] redirects to full URLs (e.g., /b232e -> /blog/b232e/slug/).
The middleware now detects 5-character codes at the root level and
redirects to the canonical URL using a 301 permanent redirect.

- Add KIND_TO_COLLECTION mapping to code.ts
- Add getCollection() method to Code class
- Update middleware to handle root-level short code URLs
- Fix test for error message to include T kind
Replace hardcoded type assertion with Collection type for better
maintainability and consistency with the rest of the codebase.

Co-authored-by: Justin Bennett <just-be-dev@users.noreply.github.com>
Replace hardcoded collection names in regex with dynamic pattern
derived from KIND_TO_COLLECTION mapping.

Co-authored-by: Justin Bennett <just-be-dev@users.noreply.github.com>
Co-authored-by: Justin Bennett <just-be-dev@users.noreply.github.com>
@just-be-dev just-be-dev force-pushed the claude/short-url-redirects-U4Gs0 branch from 4f51097 to 5d81775 Compare December 23, 2025 20:24
Replace hardcoded 'brpt' regex pattern with dynamic pattern derived from KIND_TO_COLLECTION keys. This ensures the short code pattern stays in sync with the collection mappings.

Co-authored-by: Justin Bennett <just-be-dev@users.noreply.github.com>
Comment on lines +419 to +435
describe("Code.getCollection()", () => {
it("should return 'blog' for kind B", () => {
expect(Code.fromCode("B2392").getCollection()).toBe("blog");
});

it("should return 'research' for kind R", () => {
expect(Code.fromCode("R2392").getCollection()).toBe("research");
});

it("should return 'projects' for kind P", () => {
expect(Code.fromCode("P2392").getCollection()).toBe("projects");
});

it("should return 'talks' for kind T", () => {
expect(Code.fromCode("T2392").getCollection()).toBe("talks");
});
});
Copy link
Owner Author

Choose a reason for hiding this comment

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

These tests aren't really that high value, but meh.

@just-be-dev just-be-dev merged commit ffbf3c3 into main Dec 23, 2025
3 checks passed
@just-be-dev just-be-dev deleted the claude/short-url-redirects-U4Gs0 branch December 23, 2025 20:28
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.

3 participants