-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhot-content-reload.js
61 lines (52 loc) · 2.23 KB
/
hot-content-reload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const React = require('react');
const { withRouter } = require('next/router');
const io = require('socket.io-client');
const { DEFAULT_LIVE_UPDATE_PORT, LIVE_UPDATE_EVENT_NAME, LIVE_UPDATE_NAMESPACE } = require('./lib/client-consts');
module.exports.hotContentReload = function hotContentReload({
disable = false,
port = DEFAULT_LIVE_UPDATE_PORT,
namespace = LIVE_UPDATE_NAMESPACE,
eventName = LIVE_UPDATE_EVENT_NAME
} = {}) {
return function withHotContentReload(WrappedComponent) {
if (disable) {
return WrappedComponent;
}
class Component extends React.Component {
componentDidMount() {
const portStr = process.env.NEXT_PUBLIC_HOT_RELOAD_CLIENT_PORT ?? (port ? String(port) : location.port);
namespace = process.env.NEXT_PUBLIC_HOT_RELOAD_PATH ?? namespace;
eventName = process.env.NEXT_PUBLIC_HOT_RELOAD_EVENT_NAME ?? eventName;
this.socket = io(`${location.protocol}//${location.hostname}${prefixPort(portStr)}${namespace}`);
this.socket.on(eventName, () => {
this.props.router
.replace(this.props.router.pathname, this.props.router.asPath, {
scroll: false
})
.catch((error) => {
console.error(`withHotContentReload failed to replace path, error: ${error.message}`);
});
});
this.socket.on('connect', () => {
this.socket.emit('hello');
});
}
componentWillUnmount() {
if (this.socket) {
this.socket.close();
}
}
render() {
return React.createElement(WrappedComponent, this.props, null);
}
}
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}
Component.displayName = `WithHotContentReload(${getDisplayName(WrappedComponent)})`;
return withRouter(Component);
};
};
function prefixPort(port) {
return port ? `:${port}` : '';
}