Skip to content

Commit e171f7a

Browse files
committed
fix: make prefetch do nothing in ssr
1 parent 1a7fdd7 commit e171f7a

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

src/index.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import prefetch from './prefetch'
2-
import { inBrowser } from './utils'
3-
4-
const supported = inBrowser && window.IntersectionObserver
2+
import { canPrefetch, supportIntersectionObserver } from './utils'
53

64
export { prefetch }
75

86
export default (Vue, { componentName = 'RouterLink' } = {}) => {
97
const observer =
10-
supported &&
8+
supportIntersectionObserver &&
119
new window.IntersectionObserver(entries => {
1210
entries.forEach(entry => {
1311
if (entry.isIntersecting) {
@@ -37,10 +35,6 @@ export default (Vue, { componentName = 'RouterLink' } = {}) => {
3735
}
3836
},
3937
mounted() {
40-
const conn = navigator.connection
41-
const canPrefetch =
42-
!conn ||
43-
((conn.effectiveType || '').indexOf('2g') === -1 && !conn.saveData)
4438
if (this.prefetch && observer && canPrefetch) {
4539
setTimeout(() => {
4640
this.observe()

src/prefetch.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
**/
19-
import { inBrowser } from './utils'
19+
import { inBrowser, canPrefetch } from './utils'
2020

2121
const preFetched = {}
2222

@@ -102,7 +102,7 @@ const supportedPrefetchStrategy = support('prefetch')
102102
* @return {Object} a Promise
103103
*/
104104
function prefetcher(url, isPriority) {
105-
if (preFetched[url]) {
105+
if (!canPrefetch || preFetched[url]) {
106106
return
107107
}
108108

src/utils.js

+8
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
export const inBrowser = typeof window !== 'undefined'
2+
3+
const conn = inBrowser && navigator.connection
4+
export const canPrefetch =
5+
inBrowser &&
6+
(!conn || ((conn.effectiveType || '').indexOf('2g') === -1 && !conn.saveData))
7+
8+
export const supportIntersectionObserver =
9+
inBrowser && window.IntersectionObserver

0 commit comments

Comments
 (0)