I'm trying to shorten the public URLs I use for my posts. Right now, I'm using UUIDs as primary keys, which result in long URLs like `/posts/550e8400-e29b-41d4-a716-446655440000`. I'm considering encoding the UUIDs in responses using Base64URL and decoding them in requests with middleware to achieve shorter URLs. Does anyone think this is an anti-pattern, or do you have better solutions?
5 Answers
No need to stress about public IDs; they shouldn't be typed in manually anyway. If you want shorter URLs, consider using something like Sqid for auto-increment IDs or converting to base 58 to reduce the length from 36 to 22 characters.
Check out NanoID! It's only 10 characters long, and it can generate IDs quickly with a low chance of collision. Some alternatives even keep IDs to just 7 characters, helping you create shorter URLs.
Whether it's an anti-pattern really depends on your needs. If URL length is a business concern, then maybe encoding makes sense. But adding complexity for the sake of a shorter URL might not be worth it. Perhaps consider a different identifier altogether?
Why not use slugs instead? If these are for blog posts, you could auto-generate slugs from the titles. It makes for much cleaner and more semantic URLs.
Encoding adds unnecessary complexity for both server and client. It’s simpler to just use numeric IDs and slugs for better SEO. Think about using an auto-incremented ID along with a slug, especially if you're only dealing with a few thousand entries!

Related Questions
How to Build a Custom GPT Journalist That Posts Directly to WordPress
Cloudflare Origin SSL Certificate Setup Guide
How To Effectively Monetize A Site With Ads