This commit is contained in:
Sebastian Seedorf
2020-11-16 10:01:58 +01:00
parent 7fbc95f86a
commit 2629d1e8e9
55 changed files with 4904 additions and 0 deletions

62
src/auto-reload.ts Normal file
View File

@@ -0,0 +1,62 @@
import {Router} from 'express';
import {DefaultConfig, Logger, urlJoin} from '.';
import {v4} from 'uuid';
import Timeout = NodeJS.Timeout;
const router = Router();
if (!DefaultConfig.isProduction) {
let uuid = v4();
let updateTimeout: Timeout|undefined = undefined;
import("node-watch").then((watch) => {
watch.default('public', {recursive: true}, () => {
if (updateTimeout !== undefined) clearTimeout(updateTimeout);
updateTimeout = setTimeout(() => {
uuid = v4();
}, 200);
});
}).catch((err) => { Logger.error(err); });
router.get("/auto-reload/client.js", (req, res) => {
Logger.debug(req.url, req.originalUrl, req.baseUrl);
res.setHeader('Content-Type', "application/javascript");
// language=JavaScript
res.send(`
const loc = window.location;
const url = loc.protocol+'//'+loc.host+'${urlJoin(req.baseUrl, "/auto-reload")}';
// const parse = async res => (await res.json()).uuid;
const parse = function(res) {
return res.json()
.then(function(json) {return json.uuid;}) }
let hash = undefined;
let hadError = false;
setInterval(function() {
try {
fetch(url)
.then(function(res) { return parse(res) })
.then(function(data) {
if (data) {
hash = hash === undefined ? data : hash;
if (hash !== data) {
window.location.reload();
}
}
});
} catch (e) {
if (hadError === false) {
console.log(e);
hadError = true;
}
}
}, 3000);
`.replace(/\t/g, ""));
});
router.get("/auto-reload", (req, res) => {
req.noLogging = true;
res.json({uuid});
});
}
export const AutoReloader = {
router,
};