fix(elements-core): HashRouter navigation doesn't update content in web component contexts#2850
Open
drewzhao wants to merge 3 commits intostoplightio:mainfrom
Open
Conversation
✅ Deploy Preview for stoplight-elements ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for stoplight-elements-demo ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
React Router v6's HashRouter doesn't properly respond to hash changes when used in web component contexts (Custom Elements with Shadow DOM) or when embedded in other SPA frameworks like VitePress. This commit adds a HashRouterSync component that: - Listens for hash change and popstate events - Forces React Router to navigate when the browser hash changes - Ensures content updates when users click navigation links - Only activates when router type is 'hash' The fix is transparent to users and requires no API changes. Fixes navigation issues reported with Elements v9.0.12 after the React Router v5 to v6 upgrade (commit 8520585). Technical details: - Created HashRouterSync component using useNavigate and useLocation hooks - Integrated into withRouter HOC's InternalRoutes - Syncs on mount, hashchange, and popstate events - Prevents infinite loops by tracking current hash state Testing: This fix has been validated in VitePress environments with hash-based navigation, confirming that: - URL updates correctly on navigation - Content refreshes when clicking navigation links - Browser back/forward buttons work properly - No performance impact or console errors
d7eced4 to
9c10648
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
HashRouter navigation is broken in Stoplight Elements v9.0+ when used in web component contexts (Custom Elements) or when embedded in other SPA frameworks like VitePress.
Symptoms:
#/operations/someOperation)Root Cause:
This is a regression introduced in v9.0.0 when Elements upgraded from React Router v5 to v6 (commit 8520585). React Router v6's
HashRouterdoesn't properly respond to hash changes when:Impact:
Affects all users deploying Elements as web components with
router="hash", forcing them to either:Solution
This PR adds a
HashRouterSynccomponent that bridges browser hash changes with React Router v6's navigation system.Implementation:
Created
packages/elements-core/src/components/HashRouterSync/index.tsxhashchangeandpopstateevents from the browseruseNavigate()hook to programmatically navigatewindow.location.hashwith React Router's internal stateModified
packages/elements-core/src/hoc/withRouter.tsxHashRouterSyncwhenrouterType === 'hash'ScrollToHashElementcomponentrouter="history"orrouter="memory"Exported from
packages/elements-core/src/index.tsfor public useBenefits:
router="hash"Testing
Automated Tests:
Validated in VitePress environment with Playwright:
Manual Testing:
Unit Tests: ✅
HashRouterSynccomponent following Testing Library principlesFiles Changed
Versioning
Following the versioning guidelines:
Checklist
Related
This fix resolves a critical bug #2792 affecting all v9.x users deploying Elements as web components with hash routing. The issue was introduced during the React Router v5→v6 migration and has made Elements v9.x unusable for this common deployment pattern.
Note: This is my first contribution to Stoplight Elements. I'm happy to make any changes requested by the review team. Comprehensive unit tests have been added following the project's testing guidelines.