chore: #ENABLING-567 redirect old path before rendering - #8
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the URL redirection logic for handling old hash-based routes by moving it from the root route loader to the router initialization phase. This ensures redirections occur before the router is created rather than during route loading.
Key Changes:
- Extracted redirection logic into a dedicated
manageRedirectionsfunction in a new redirections module - Moved redirection execution from route loader to router creation in
routes/index.tsx - Changed from
location.replace()towindow.history.replaceState()for smoother navigation
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| frontend/src/routes/root/index.tsx | Removed URL redirection logic and unused imports from the root route loader |
| frontend/src/routes/redirections/index.tsx | Added new module with manageRedirections function to check and return redirect paths for old hash-based URLs |
| frontend/src/routes/index.tsx | Integrated manageRedirections to handle URL redirections before router creation using window.history.replaceState |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| let redirectPath = ''; | ||
| const isPath = matchPath('/view/:id', hashLocation); | ||
|
|
||
| if (isPath) { | ||
| // Redirect to the new format | ||
| redirectPath = `/id/${isPath?.params.id}`; | ||
| } | ||
|
|
||
| return redirectPath; |
There was a problem hiding this comment.
When hashLocation exists but doesn't match the pattern (isPath is falsy), this function returns an empty string instead of null. This is inconsistent with the documented return type and could cause unexpected behavior in the calling code where an empty string is truthy. The function should return null when no redirection is needed, regardless of whether a hash exists.
| let redirectPath = ''; | |
| const isPath = matchPath('/view/:id', hashLocation); | |
| if (isPath) { | |
| // Redirect to the new format | |
| redirectPath = `/id/${isPath?.params.id}`; | |
| } | |
| return redirectPath; | |
| const isPath = matchPath('/view/:id', hashLocation); | |
| if (isPath) { | |
| // Redirect to the new format | |
| return `/id/${isPath?.params.id}`; | |
| } | |
| // No redirection needed for this hash | |
| return null; |
|
|
||
| if (isPath) { | ||
| // Redirect to the new format | ||
| redirectPath = `/id/${isPath?.params.id}`; |
There was a problem hiding this comment.
The optional chaining operator is redundant here since the code is already inside an if statement that checks isPath is truthy. This can be simplified to isPath.params.id for better clarity and consistency.
| redirectPath = `/id/${isPath?.params.id}`; | |
| redirectPath = `/id/${isPath.params.id}`; |
Description
This PR refactors the URL redirection logic for handling old hash-based routes by moving it from the root route loader to the router initialization phase. This ensures redirections occur before the router is created rather than during route loading.
Key Changes:
Type of change
Please check options that are relevant.
Checklist: