Skip to content

Commit

Permalink
fix(react-router,start): ensure document and window is only used when…
Browse files Browse the repository at this point in the history
… available
  • Loading branch information
lo1tuma committed Aug 12, 2024
1 parent a6819bf commit 3ab6098
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 10 additions & 5 deletions packages/react-router/src/Transitioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export function Transitioner() {

// Try to load the initial location
useLayoutEffect(() => {
if (router.isServer) {
return
}
if (
window.__TSR__?.dehydrated ||
(mountLoadForRouter.current.router === router &&
Expand Down Expand Up @@ -111,11 +114,13 @@ export function Transitioner() {
resolvedLocation: s.location,
}))

if ((document as any).querySelector) {
if (router.state.location.hash !== '') {
const el = document.getElementById(router.state.location.hash)
if (el) {
el.scrollIntoView()
if (!router.isServer) {
if ((document as any).querySelector) {
if (router.state.location.hash !== '') {
const el = document.getElementById(router.state.location.hash)
if (el) {
el.scrollIntoView()
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/start/src/client/Meta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ export const Meta = ({ children }: { children?: React.ReactNode }) => {
React[
typeof document !== 'undefined' ? 'useLayoutEffect' : 'useEffect'
](() => {
if (router.isServer) {
return
}

// Remove all meta between the tsr meta tags
const start = document.head.querySelector('meta[name="tsr-meta"]')
const end = document.head.querySelector('meta[name="/tsr-meta"]')
Expand All @@ -190,7 +194,7 @@ export const Meta = ({ children }: { children?: React.ReactNode }) => {
end?.remove()

setMounted(true)
}, [])
}, [router.isServer])

const all = (
<>
Expand Down

0 comments on commit 3ab6098

Please sign in to comment.