@shuvi/platform-web 1.0.61 → 1.0.63

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.
@@ -1,3 +1,4 @@
1
+ import { getAppData } from '@shuvi/platform-shared/shared';
1
2
  let source;
2
3
  const eventCallbacks = [];
3
4
  let lastActivity = Date.now();
@@ -9,7 +10,8 @@ function getSocketProtocol(assetPublicPath) {
9
10
  protocol = new URL(assetPublicPath).protocol;
10
11
  }
11
12
  catch (_) { }
12
- return protocol === 'http:' ? 'ws' : 'wss';
13
+ // use ws by default
14
+ return protocol === 'https:' ? 'wss' : 'ws';
13
15
  }
14
16
  export function addMessageListener(cb) {
15
17
  eventCallbacks.push(cb);
@@ -34,12 +36,17 @@ export function connectHMR(options) {
34
36
  function init() {
35
37
  if (source)
36
38
  source.close();
37
- const { hostname, port } = window.location;
39
+ let { devServer } = getAppData();
40
+ if (!devServer) {
41
+ // fallback case
42
+ const { hostname, port } = window.location;
43
+ devServer = `${hostname}:${port}`;
44
+ }
38
45
  const protocol = getSocketProtocol(options.assetPublicPath);
39
46
  const assetPublicPath = options.assetPublicPath
40
47
  .replace(/^\/+/, '')
41
48
  .replace(/\/+$/, '');
42
- let url = `${protocol}://${hostname}:${port}${assetPublicPath ? `/${assetPublicPath}` : ''}`;
49
+ let url = `${protocol}://${devServer}${assetPublicPath ? `/${assetPublicPath}` : ''}`;
43
50
  if (assetPublicPath.startsWith('http')) {
44
51
  url = `${protocol}://${assetPublicPath.split('://')[1]}`;
45
52
  }
@@ -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(({ url, id }) => prefetchViaDom(url, id, 'script'))
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 './requestIdleCallback';
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;
@@ -68,7 +68,9 @@ class BaseRenderer {
68
68
  }
69
69
  _getInlineAppData(app, appData) {
70
70
  const routes = app.router.routes || [];
71
- const data = JSON.stringify(Object.assign(Object.assign({}, appData), { filesByRoutId: (0, generateFilesByRoutId_1.default)(resources_1.default.clientManifest, routes), publicPath: this._serverPluginContext.assetPublicPath }));
71
+ const data = JSON.stringify(Object.assign(Object.assign({}, appData), { filesByRoutId: (0, generateFilesByRoutId_1.default)(resources_1.default.clientManifest, routes), publicPath: this._serverPluginContext.assetPublicPath,
72
+ // process.env.DEV_SERVER is undefined in production, and the key devServer will be removed
73
+ devServer: process.env.DEV_SERVER }));
72
74
  return (0, htmlTag_1.tag)('script', {
73
75
  id: constants_1.CLIENT_APPDATA_ID,
74
76
  type: 'application/json'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/platform-web",
3
- "version": "1.0.61",
3
+ "version": "1.0.63",
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.61",
76
- "@shuvi/hook": "1.0.61",
77
- "@shuvi/platform-shared": "1.0.61",
78
- "@shuvi/router": "1.0.61",
79
- "@shuvi/router-react": "1.0.61",
80
- "@shuvi/runtime": "1.0.61",
81
- "@shuvi/shared": "1.0.61",
82
- "@shuvi/toolpack": "1.0.61",
83
- "@shuvi/utils": "1.0.61",
75
+ "@shuvi/error-overlay": "1.0.63",
76
+ "@shuvi/hook": "1.0.63",
77
+ "@shuvi/platform-shared": "1.0.63",
78
+ "@shuvi/router": "1.0.63",
79
+ "@shuvi/router-react": "1.0.63",
80
+ "@shuvi/runtime": "1.0.63",
81
+ "@shuvi/shared": "1.0.63",
82
+ "@shuvi/toolpack": "1.0.63",
83
+ "@shuvi/utils": "1.0.63",
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.61"
101
+ "@shuvi/service": "1.0.63"
102
102
  },
103
103
  "devDependencies": {
104
104
  "@shuvi/service": "workspace:*",
@@ -1,2 +0,0 @@
1
- export declare const requestIdleCallback: ((callback: IdleRequestCallback, options?: IdleRequestOptions) => number) & typeof globalThis.requestIdleCallback;
2
- export declare const cancelIdleCallback: ((handle: number) => void) & typeof globalThis.cancelIdleCallback;
@@ -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
- };