-
Notifications
You must be signed in to change notification settings - Fork 0
Add short URL redirect with type prefix routing #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
just-be-dev | a541bba | Dec 23 2025, 08:27 PM |
src/middleware.ts
Outdated
| 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.tsandsrc/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 --rebasein 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay @claude, try again
There was a problem hiding this comment.
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:
- Short code pattern (line 9): Extracts keys from
KIND_TO_COLLECTION→"BRPT"→ lowercases to"brpt"→ builds regex[brpt] - Collection path pattern (line 36): Extracts values from
KIND_TO_COLLECTION→"blog|projects|research|talks"→ builds regex^/(blog|projects|research|talks)/ - Type assertion (line 39): Uses
Collectiontype derived fromKIND_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.
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>
4f51097 to
5d81775
Compare
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>
| 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"); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
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.
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.