fix(elements-core): HashRouter navigation doesn't update content in web component contexts #2850
+71
−3
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.