I’m building a Next.js blog with the Notion API and want to replace page IDs with slugs derived from titles. Avoiding 404 errors. Tried this slugging approach:
function makeSlug(title) {
return title.trim().toLowerCase().replace(/\s+/g, '-');
}
I’m building a Next.js blog with the Notion API and want to replace page IDs with slugs derived from titles. Avoiding 404 errors. Tried this slugging approach:
function makeSlug(title) {
return title.trim().toLowerCase().replace(/\s+/g, '-');
}
In my work converting Notion page IDs to title-based slugs in Next.js, I realized that relying solely on a simple function often leads to inconsistencies, especially with titles containing special characters or accented letters. I ended up using a dedicated library that not only handles these edge cases but also normalizes text to ASCII where appropriate. This approach was beneficial when dealing with pages that have similar titles, as it allowed me to apply a consistent transformation and include checks for duplicates, thereby reducing the likelihood of encountering routing errors or unexpected 404 responses.
Working with Notion and Next.js, I found that simply converting titles to lower-case and hyphens isn’t always enough, especially when titles include punctuation or uncommon characters. In my projects, I improved the slug generation process by incorporating a regular expression that filters out unexpected symbols and validates the slug format before using it. Additionally, I learned that edge cases occur when multiple pages have similar titles, so appending a timestamp or a unique identifier helps ensure that every slug remains unique, preventing any chance of page conflicts.