Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added automatic client page reload #3188

Merged
merged 1 commit into from Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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");
khassel marked this conversation as resolved.
Show resolved Hide resolved

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
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
@@ -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 };