Skip to content

Commit

Permalink
fix(react-router,start): ensure document is only used when available (#…
Browse files Browse the repository at this point in the history
…2120)

* fix(react-router,start): ensure document and window is only used when available

* Update packages/react-router/src/Transitioner.tsx

* fix: document not undefined in the transitioner

* fix(start): incorrect exit condition for document on Meta

---------

Co-authored-by: Sean Cassiere <33615041+SeanCassiere@users.noreply.github.com>
  • Loading branch information
lo1tuma and SeanCassiere committed Aug 19, 2024
1 parent c581d0d commit 0fb2785
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/react-router/src/Transitioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function Transitioner() {
// Try to load the initial location
useLayoutEffect(() => {
if (
window.__TSR__?.dehydrated ||
(typeof window !== 'undefined' && window.__TSR__?.dehydrated) ||
(mountLoadForRouter.current.router === router &&
mountLoadForRouter.current.mounted)
) {
Expand Down Expand Up @@ -111,7 +111,7 @@ export function Transitioner() {
resolvedLocation: s.location,
}))

if ((document as any).querySelector) {
if (typeof document !== 'undefined' && (document as any).querySelector) {
if (router.state.location.hash !== '') {
const el = document.getElementById(router.state.location.hash)
if (el) {
Expand Down
4 changes: 4 additions & 0 deletions 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 (typeof document === 'undefined') {
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 Down

0 comments on commit 0fb2785

Please sign in to comment.