-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsync.js
102 lines (81 loc) · 2.5 KB
/
sync.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import diffhtml from 'diffhtml';
import { Socket } from 'engine.io-client';
process.env.NODE_ENV = 'production';
let interval = null;
const domNodes = new Map();
const { outerHTML, html } = diffhtml;
window.staticSyncHandlers = new Set();
window.staticSyncSocket = undefined;
function open() {
clearInterval(interval);
const host = location.host.split(':')[0];
const socket = window.staticSyncSocket = new Socket(`ws://${host}:54321`, {
transports: ['websocket'],
});
socket.on('open', () => {
console.log('diffhtml-static-sync socket connected');
socket.on('message', message => {
const { file, markup, quiet = false } = JSON.parse(message);
if (!file) {
return;
}
if (!quiet) {
console.log(`${file} changed`);
}
if (staticSyncHandlers.size) {
let retVal = false;
staticSyncHandlers.forEach(fn => {
retVal = retVal || fn({ file, markup, quiet });
});
if (retVal) {
return retVal;
}
}
// Handle CSS files.
if (file.includes('.css')) {
const queryString = `?sync=${Date.now()}`;
const domNode = document.querySelector(`link[href='${file}']`) || domNodes.get(file);
if (domNode) {
if (!quiet) {
console.log(`${file} found on page, reloading`);
}
domNode.href = domNode.href.replace(/\?.*|$/, queryString);
domNodes.set(file, domNode);
}
// Reload all stylesheets since we want to be sure everything is updated.
else {
[...document.querySelectorAll('link')].forEach(domNode => {
const queryString = `?sync=${Date.now()}`;
domNode.href = domNode.href.replace(/\?.*|$/, queryString);
});
}
return;
}
const path = location.pathname.slice(1) || 'index';
const ext = file.split('.').slice(-1)[0];
if (
file === true ||
path === file ||
(
path === file.split('.').slice(0, -1).join('.') &&
ext === 'html' ||
ext === 'md'
)
) {
outerHTML(document.documentElement, html(markup));
}
// All other files cause a full page reload.
else {
location.reload();
}
});
});
socket.on('close', () => {
console.log('Socket closed, attempting to reopen every 2 seconds');
interval = setInterval(() => open(), 2000);
});
socket.on('error', (e) => {
console.log('Socket errored', e);
});
}
open();