[package][router] fix: safely access window.location.origin in Sitemap #41175
+7
−4
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.
Why
I was facing the following error:
This PR fixes the "Render Error" in the Sitemap component where accessing
window.location.originwould cause a crash ifwindoworwindow.locationis undefinedHow
I modified the SystemInfo function in the Sitemap.tsx file to safely access
window.location.originPreviously, the code directly accessed
window.location.origin, which could cause a crash ifwindoworwindow.locationwas undefinedPrevious code
const locationOrigin = window.location.origin;I replaced the direct access with a conditional check
const locationOrigin = typeof window !== 'undefined' && window.location ? window.location.origin : null;This ensures that
windowexistswindow.locationexistsoriginnull, preventing the crashTest Plan
The reviewer can reproduce the bug by running this in a mobile app
router.navigate('/_sitemap')yarn buildin expo-router package and got 0 errorsyarn lintran successfullyChecklist
changelog.mdentry and rebuilt the package sources according to this short guide