Skip to content

Commit 1e8dd4d

Browse files
committed
make sure window and document is defined
1 parent 4757604 commit 1e8dd4d

File tree

6 files changed

+63
-36
lines changed

6 files changed

+63
-36
lines changed

packages/core/src/eventHandler.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ class EventHandler {
1414
}[] = []
1515

1616
public init() {
17-
window.addEventListener('popstate', this.handlePopstateEvent.bind(this))
18-
document.addEventListener('scroll', debounce(Scroll.onScroll.bind(Scroll), 100), true)
17+
if (typeof window !== 'undefined') {
18+
window.addEventListener('popstate', this.handlePopstateEvent.bind(this))
19+
}
20+
21+
if (typeof document !== 'undefined') {
22+
document.addEventListener('scroll', debounce(Scroll.onScroll.bind(Scroll), 100), true)
23+
}
1924
}
2025

2126
public onGlobalEvent<TEventName extends GlobalEventNames>(

packages/core/src/history.ts

+34-29
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,24 @@ class History {
2929
}
3030

3131
public pushState(page: Page): void {
32-
if (!this.preserveUrl) {
33-
this.current = page
34-
35-
this.addToQueue(() => {
36-
return this.getPageData(page).then((data) => {
37-
window.history.pushState(
38-
{
39-
page: data,
40-
timestamp: Date.now(),
41-
},
42-
'',
43-
page.url,
44-
)
45-
})
46-
})
32+
if (isServer || this.preserveUrl) {
33+
return
4734
}
35+
36+
this.current = page
37+
38+
this.addToQueue(() => {
39+
return this.getPageData(page).then((data) => {
40+
window.history.pushState(
41+
{
42+
page: data,
43+
timestamp: Date.now(),
44+
},
45+
'',
46+
page.url,
47+
)
48+
})
49+
})
4850
}
4951

5052
protected getPageData(page: Page): Promise<Page | ArrayBuffer> {
@@ -84,21 +86,24 @@ class History {
8486
public replaceState(page: Page): void {
8587
currentPage.merge(page)
8688

87-
if (!this.preserveUrl) {
88-
this.current = page
89-
this.addToQueue(() => {
90-
return this.getPageData(page).then((data) => {
91-
window.history.replaceState(
92-
{
93-
page: data,
94-
timestamp: Date.now(),
95-
},
96-
'',
97-
page.url,
98-
)
99-
})
100-
})
89+
if (isServer || this.preserveUrl) {
90+
return
10191
}
92+
93+
this.current = page
94+
95+
this.addToQueue(() => {
96+
return this.getPageData(page).then((data) => {
97+
window.history.replaceState(
98+
{
99+
page: data,
100+
timestamp: Date.now(),
101+
},
102+
'',
103+
page.url,
104+
)
105+
})
106+
})
102107
}
103108

104109
protected addToQueue(fn: () => Promise<void>): void {

packages/core/src/navigationType.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class NavigationType {
22
protected type: NavigationTimingType
33

44
public constructor() {
5-
if (window?.performance.getEntriesByType('navigation').length > 0) {
5+
if (typeof window !== 'undefined' && window?.performance.getEntriesByType('navigation').length > 0) {
66
this.type = (window.performance.getEntriesByType('navigation')[0] as PerformanceNavigationTiming).type
77
} else {
88
this.type = 'navigate'

packages/core/src/poll.ts

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export class Poll {
2828
}
2929

3030
public start() {
31+
if (typeof window === 'undefined') {
32+
return
33+
}
34+
3135
this.stop()
3236

3337
this.id = window.setInterval(() => {

packages/core/src/polls.ts

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class Polls {
3333
}
3434

3535
protected setupVisibilityListener() {
36+
if (typeof document === 'undefined') {
37+
return
38+
}
39+
3640
document.addEventListener(
3741
'visibilitychange',
3842
() => {

packages/core/src/progress.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import NProgress from 'nprogress'
22
import { GlobalEvent } from './types'
33

4-
const hideProgressStyleEl = document.createElement('style')
5-
hideProgressStyleEl.innerHTML = '#nprogress { display: none; }'
4+
const hideProgressStyleEl = (() => {
5+
if (typeof document === 'undefined') {
6+
return null
7+
}
8+
9+
const el = document.createElement('style')
10+
11+
el.innerHTML = '#nprogress { display: none; }'
12+
13+
return el
14+
})()
615

716
let hideCount = 0
817

918
const removeStyle = () => {
10-
if (!document.querySelector('#nprogress')) {
19+
if (hideProgressStyleEl && !document.querySelector('#nprogress')) {
1120
return document.head.removeChild(hideProgressStyleEl)
1221
}
1322

@@ -25,7 +34,7 @@ export const reveal = (force = false) => {
2534
export const hide = () => {
2635
hideCount++
2736

28-
if (!document.head.contains(hideProgressStyleEl)) {
37+
if (hideProgressStyleEl && !document.head.contains(hideProgressStyleEl)) {
2938
document.head.appendChild(hideProgressStyleEl)
3039
}
3140
}

0 commit comments

Comments
 (0)