Skip to content

Node.js based forum software built for the modern web

fix: #12017, unreliable forum updated messaging

Julian Lam715bdac3faa2
diff --git a/public/src/sockets.js b/public/src/sockets.js
index 9e840ec513..1a87e57646 100644
--- a/public/src/sockets.js
+++ b/public/src/sockets.js
@@ -4,6 +4,8 @@
const io = require('socket.io-client');
// eslint-disable-next-line no-redeclare
const $ = require('jquery');
+// eslint-disable-next-line import/no-unresolved
+const { alert } = require('alerts');
app = window.app || {};
@@ -113,23 +115,6 @@ app = window.app || {};
console.warn('[socket.io] ', data.eventName, 'is now deprecated in favour of', data.replacement);
});
- socket.removeAllListeners('event:nodebb.ready');
- socket.on('event:nodebb.ready', function (data) {
- if ((data.hostname === app.upstreamHost) && (!app.cacheBuster || app.cacheBuster !== data['cache-buster'])) {
- app.cacheBuster = data['cache-buster'];
- require(['alerts'], function (alerts) {
- alerts.alert({
- alert_id: 'forum_updated',
- title: '[[global:updated.title]]',
- message: '[[global:updated.message]]',
- clickfn: function () {
- window.location.reload();
- },
- type: 'warning',
- });
- });
- }
- });
socket.on('event:livereload', function () {
if (app.user.isAdmin && !ajaxify.currentPage.match(/admin/)) {
window.location.reload();
@@ -156,7 +141,7 @@ app = window.app || {};
});
}
- function onConnect() {
+ async function onConnect() {
if (!reconnecting) {
hooks.fire('action:connected');
} else {
@@ -171,7 +156,19 @@ app = window.app || {};
reJoinCurrentRoom();
- socket.emit('meta.reconnected');
+ const { 'cache-buster': hash, hostname } = await socket.emit('meta.reconnected');
+ if ((hostname === app.upstreamHost) && (!app.cacheBuster || app.cacheBuster !== hash)) {
+ app.cacheBuster = hash;
+ alert({
+ alert_id: 'forum_updated',
+ title: '[[global:updated.title]]',
+ message: '[[global:updated.message]]',
+ clickfn: function () {
+ window.location.reload();
+ },
+ type: 'warning',
+ });
+ }
hooks.fire('action:reconnected');
diff --git a/src/socket.io/meta.js b/src/socket.io/meta.js
index e62ae0e356..f150102f13 100644
--- a/src/socket.io/meta.js
+++ b/src/socket.io/meta.js
@@ -1,7 +1,9 @@
'use strict';
+const os = require('os');
const user = require('../user');
+const meta = require('../meta');
const topics = require('../topics');
const SocketMeta = module.exports;
@@ -13,7 +15,10 @@ SocketMeta.reconnected = function (socket, data, callback) {
topics.pushUnreadCount(socket.uid);
user.notifications.pushCount(socket.uid);
}
- callback();
+ callback(null, {
+ 'cache-buster': meta.config['cache-buster'],
+ hostname: os.hostname(),
+ });
};
/* Rooms */
diff --git a/src/webserver.js b/src/webserver.js
index 824aaa71bd..8885849c7a 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -4,7 +4,6 @@
const fs = require('fs');
const util = require('util');
const path = require('path');
-const os = require('os');
const nconf = require('nconf');
const express = require('express');
const chalk = require('chalk');
@@ -85,10 +84,7 @@ exports.listen = async function () {
await initializeNodeBB();
winston.info('🎉 NodeBB Ready');
- require('./socket.io').server.emit('event:nodebb.ready', {
- 'cache-buster': meta.config['cache-buster'],
- hostname: os.hostname(),
- });
+ require('./socket.io').server.emit('event:nodebb.ready', {});
plugins.hooks.fire('action:nodebb.ready');