Skip to content

Commit

Permalink
added automatic client page reload (#3188)
Browse files Browse the repository at this point in the history
solution for #3105 

~~not sure if updatenotification is the right place, so opinions?~~

now impleneted in `main.js`
  • Loading branch information
khassel authored Sep 13, 2023
1 parent f2957f9 commit 7a1591b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ _This release is scheduled to be released on 2023-10-01._
- Added optional AnimateCSS animate for `hide()`, `show()`, `updateDom()`
- Added AnimateIn and animateOut in module config definition
- Apply AnimateIn rules on the first start
- Added automatic client page reload when server was restarted by setting `reloadAfterServerRestart: true` in `config.js`, per default `false` (#3105)

### Removed

Expand Down
5 changes: 5 additions & 0 deletions js/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const defaults = {
// e.g. you need to add `frameguard: false` for embedding MagicMirror in another website, see https://github.com/MichMich/MagicMirror/issues/2847
httpHeaders: { contentSecurityPolicy: false, crossOriginOpenerPolicy: false, crossOriginEmbedderPolicy: false, crossOriginResourcePolicy: false, originAgentCluster: false },

// properties for checking if server is alive and has same startup-timestamp, the check is per default enabled
// (interval 30 seconds). If startup-timestamp has changed the client reloads the magicmirror webpage.
checkServerInterval: 30 * 1000,
reloadAfterServerRestart: false,

modules: [
{
module: "updatenotification",
Expand Down
21 changes: 21 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,33 @@ const MM = (function () {
*/
modulesStarted: function (moduleObjects) {
modules = [];
let startUp = "";

moduleObjects.forEach((module) => modules.push(module));

Log.info("All modules started!");
sendNotification("ALL_MODULES_STARTED");

createDomObjects();

if (config.reloadAfterServerRestart) {
setInterval(async () => {
// if server startup time has changed (which means server was restarted)
// the client reloads the mm page
try {
const res = await fetch(`${location.protocol}//${location.host}/startup`);
const curr = await res.text();
if (startUp === "") startUp = curr;
if (startUp !== curr) {
startUp = "";
window.location.reload(true);
console.warn("Refreshing Website because server was restarted");
}
} catch (err) {
Log.error(`MagicMirror not reachable: ${err}`);
}
}, config.checkServerInterval);
}
},

/**
Expand Down
4 changes: 3 additions & 1 deletion js/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const socketio = require("socket.io");

const Log = require("logger");
const Utils = require("./utils");
const { cors, getConfig, getHtml, getVersion } = require("./server_functions");
const { cors, getConfig, getHtml, getVersion, getStartup } = require("./server_functions");

/**
* Server
Expand Down Expand Up @@ -91,6 +91,8 @@ function Server(config) {

app.get("/config", (req, res) => getConfig(req, res));

app.get("/startup", (req, res) => getStartup(req, res));

app.get("/", (req, res) => getHtml(req, res));

server.on("listening", () => {
Expand Down
12 changes: 11 additions & 1 deletion js/server_functions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require("fs");
const path = require("path");
const Log = require("logger");
const startUp = new Date();

/**
* Gets the config.
Expand All @@ -11,6 +12,15 @@ function getConfig(req, res) {
res.send(config);
}

/**
* Gets the startup time.
* @param {Request} req - the request
* @param {Response} res - the result
*/
function getStartup(req, res) {
res.send(startUp);
}

/**
* A method that forwards HTTP Get-methods to the internet to avoid CORS-errors.
*
Expand Down Expand Up @@ -117,4 +127,4 @@ function getVersion(req, res) {
res.send(global.version);
}

module.exports = { cors, getConfig, getHtml, getVersion };
module.exports = { cors, getConfig, getHtml, getVersion, getStartup };

0 comments on commit 7a1591b

Please sign in to comment.