@shuvi/platform-web 1.0.61 → 1.0.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -22,6 +22,7 @@ import * as React from 'react';
|
|
|
22
22
|
import { Link as LinkFromRouterReact, RouterContext } from '@shuvi/router-react';
|
|
23
23
|
import { getFilesOfRoute } from '@shuvi/platform-shared/shared';
|
|
24
24
|
import useIntersection from './utils/useIntersection';
|
|
25
|
+
import { awaitPageLoadAndIdle } from '@shuvi/utils/idleCallback';
|
|
25
26
|
const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/;
|
|
26
27
|
const prefetched = {};
|
|
27
28
|
function hasSupportPrefetch() {
|
|
@@ -64,7 +65,10 @@ function prefetchFn(router, to) {
|
|
|
64
65
|
return;
|
|
65
66
|
const canPrefetch = hasSupportPrefetch();
|
|
66
67
|
yield Promise.all(canPrefetch
|
|
67
|
-
? files.js.map((
|
|
68
|
+
? files.js.map((_a) => __awaiter(this, [_a], void 0, function* ({ url, id }) {
|
|
69
|
+
yield awaitPageLoadAndIdle({ remainingTime: 49, timeout: 10 * 1000 });
|
|
70
|
+
yield prefetchViaDom(url, id, 'script');
|
|
71
|
+
}))
|
|
68
72
|
: []);
|
|
69
73
|
});
|
|
70
74
|
}
|
|
@@ -77,7 +81,11 @@ export const Link = function LinkWithPrefetch(_a) {
|
|
|
77
81
|
const previousHref = React.useRef(to);
|
|
78
82
|
const [setIntersectionRef, isVisible, resetVisible] = useIntersection({});
|
|
79
83
|
const { router } = React.useContext(RouterContext);
|
|
80
|
-
const setRef = React.useCallback((el) => {
|
|
84
|
+
const setRef = React.useCallback((el) => __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
/**
|
|
86
|
+
* Lazy prefetching to avoid negative performance impact for the first page.
|
|
87
|
+
*/
|
|
88
|
+
yield awaitPageLoadAndIdle({ remainingTime: 49, timeout: 10 * 1000 });
|
|
81
89
|
// Before the link getting observed, check if visible state need to be reset
|
|
82
90
|
if (isHrefValid && previousHref.current !== to) {
|
|
83
91
|
resetVisible();
|
|
@@ -92,7 +100,7 @@ export const Link = function LinkWithPrefetch(_a) {
|
|
|
92
100
|
ref.current = el;
|
|
93
101
|
}
|
|
94
102
|
}
|
|
95
|
-
}, [to, isHrefValid, prefetch, resetVisible, setIntersectionRef, ref]);
|
|
103
|
+
}), [to, isHrefValid, prefetch, resetVisible, setIntersectionRef, ref]);
|
|
96
104
|
React.useEffect(() => {
|
|
97
105
|
const shouldPrefetch = isHrefValid && prefetch !== false && isVisible;
|
|
98
106
|
if (shouldPrefetch && !prefetched[to]) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
-
import { requestIdleCallback, cancelIdleCallback } from '
|
|
2
|
+
import { requestIdleCallback, cancelIdleCallback } from '@shuvi/utils/idleCallback';
|
|
3
3
|
const hasIntersectionObserver = typeof IntersectionObserver !== 'undefined';
|
|
4
4
|
export default function useIntersection({ rootRef, rootMargin = '0px', disabled }) {
|
|
5
5
|
const isDisabled = disabled || !hasIntersectionObserver;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shuvi/platform-web",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.62",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/shuvijs/shuvi.git",
|
|
@@ -72,15 +72,15 @@
|
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
74
|
"@next/react-refresh-utils": "12.1.6",
|
|
75
|
-
"@shuvi/error-overlay": "1.0.
|
|
76
|
-
"@shuvi/hook": "1.0.
|
|
77
|
-
"@shuvi/platform-shared": "1.0.
|
|
78
|
-
"@shuvi/router": "1.0.
|
|
79
|
-
"@shuvi/router-react": "1.0.
|
|
80
|
-
"@shuvi/runtime": "1.0.
|
|
81
|
-
"@shuvi/shared": "1.0.
|
|
82
|
-
"@shuvi/toolpack": "1.0.
|
|
83
|
-
"@shuvi/utils": "1.0.
|
|
75
|
+
"@shuvi/error-overlay": "1.0.62",
|
|
76
|
+
"@shuvi/hook": "1.0.62",
|
|
77
|
+
"@shuvi/platform-shared": "1.0.62",
|
|
78
|
+
"@shuvi/router": "1.0.62",
|
|
79
|
+
"@shuvi/router-react": "1.0.62",
|
|
80
|
+
"@shuvi/runtime": "1.0.62",
|
|
81
|
+
"@shuvi/shared": "1.0.62",
|
|
82
|
+
"@shuvi/toolpack": "1.0.62",
|
|
83
|
+
"@shuvi/utils": "1.0.62",
|
|
84
84
|
"content-type": "1.0.4",
|
|
85
85
|
"core-js": "3.6.5",
|
|
86
86
|
"doura": "0.0.13",
|
|
@@ -98,7 +98,7 @@
|
|
|
98
98
|
"whatwg-fetch": "3.0.0"
|
|
99
99
|
},
|
|
100
100
|
"peerDependencies": {
|
|
101
|
-
"@shuvi/service": "1.0.
|
|
101
|
+
"@shuvi/service": "1.0.62"
|
|
102
102
|
},
|
|
103
103
|
"devDependencies": {
|
|
104
104
|
"@shuvi/service": "workspace:*",
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export const requestIdleCallback = (typeof self !== 'undefined' &&
|
|
2
|
-
self.requestIdleCallback &&
|
|
3
|
-
self.requestIdleCallback.bind(window)) ||
|
|
4
|
-
function (cb) {
|
|
5
|
-
let start = Date.now();
|
|
6
|
-
return setTimeout(function () {
|
|
7
|
-
cb({
|
|
8
|
-
didTimeout: false,
|
|
9
|
-
timeRemaining: function () {
|
|
10
|
-
return Math.max(0, 50 - (Date.now() - start));
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
}, 1);
|
|
14
|
-
};
|
|
15
|
-
export const cancelIdleCallback = (typeof self !== 'undefined' &&
|
|
16
|
-
self.cancelIdleCallback &&
|
|
17
|
-
self.cancelIdleCallback.bind(window)) ||
|
|
18
|
-
function (id) {
|
|
19
|
-
return clearTimeout(id);
|
|
20
|
-
};
|