Fix slug auto-generation to preserve existing slugs on title edits #6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The slug field was automatically regenerating from the post title on every edit, which is detrimental to SEO because it breaks existing URLs and causes 404 errors for previously indexed content. This behavior differs from WordPress and other CMS platforms where slugs are set once during creation and preserved on subsequent edits.
Root Cause
The
SlugComponent.tsxcontained auseEffecthook that would update the slug whenever the title field changed, regardless of whether it was a new or existing post. While the backendformatSlugHookhad the correct logic to only generate slugs for new posts, the frontend component was overriding this behavior.Solution
Modified the slug component to implement WordPress-like behavior by:
hasSeenNonEmptySlugref to distinguish between new and existing documentsExample Behavior
Before (problematic):
After (fixed):
Testing
The fix has been validated through comprehensive test scenarios covering:
All existing functionality remains intact while fixing the core SEO issue.
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.