Best way to manage URL case sensitivity?
We are building a site where we want all URLs to be case insensitive. On the code side, we are using NextJS with middleware which redirects all router requests to a lowercase URL
import { NextResponse } from 'next/server';
const Middleware = (req) => {
if (req.nextUrl.pathname === req.nextUrl.pathname.toLowerCase()) return NextResponse.next();
return NextResponse.redirect(new URL(req.nextUrl.origin + req.nextUrl.pathname.toLowerCase()));
};
export default Middleware;
Using the JavaScript SDK, we are getting Entries from ContentStack with a URL query
getEntryByUrl(contentTypeUid, entryUrl, referenceFieldPath) {
return new Promise((resolve, reject) => {
const query = Stack.ContentType(contentTypeUid).Query();
if (referenceFieldPath) query.includeReference(referenceFieldPath);
const data = query.where('url', `${entryUrl}`).toJSON().find();
data.then(
(result) => {
resolve(result[0]);
},
(error) => {
reject(error);
}
);
});
},
The problem is that in ContentStack, it doesn’t seem like there is an option to enforce case inputs on the URL input field, and the query method in the SDK seems to be case sensitive. This means that the page will 404 if a content entry has capital letters in the URL. This current pattern also seems to allow for duplicate URL entries even if you set {"enforce_unique_urls": true} in the CMA if one entry has a capitalized URL and one is lowercase (ie “/About” vs “/about”)
What would be the best way to manage URL case if you want all entries to follow the same pattern (ie camelcase, lowercase etc)? Is there a way to make the Stack.query method case insensitive, or set case rules on the URL input field?
Thanks!