Skip to content

Commit e6e571d

Browse files
[fix] Disconnect websocket on page reload to prevent missed notifications
When users click "Save" or "Save and Continue", the page reloads and closes the websocket connection. If a background task completes during this reload, the notification is lost because there's no active websocket connection to receive it. This change explicitly disconnects the websocket on beforeunload, ensuring the server knows the client is unavailable. When the page reloads and the websocket reconnects, any queued notifications will be delivered properly. Fixes #264
1 parent 0f3d262 commit e6e571d

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

openwisp_notifications/static/openwisp-notifications/js/notifications.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ const owNotificationWindow = {
3232
// closing the window
3333
$(window).on("beforeunload", function () {
3434
owNotificationWindow.remove();
35+
// Disconnect websocket to prevent missed notifications during page reload
36+
if (typeof notificationSocket !== "undefined") {
37+
notificationSocket.close();
38+
}
3539
});
3640
// Get authority to play notification sound when
3741
// other windows are closed
@@ -160,7 +164,9 @@ function notificationWidget($) {
160164
}
161165

162166
function appendPage() {
163-
$("#ow-notifications-loader").before(pageContainer(fetchedPages[lastRenderedPage]));
167+
$("#ow-notifications-loader").before(
168+
pageContainer(fetchedPages[lastRenderedPage]),
169+
);
164170
if (lastRenderedPage >= renderedPages) {
165171
$(".ow-notification-wrapper div:first").remove();
166172
}
@@ -185,7 +191,10 @@ function notificationWidget($) {
185191
},
186192
success: function (res) {
187193
nextPageUrl = res.next;
188-
if (res.count === 0 || (res.results.length === 0 && nextPageUrl === null)) {
194+
if (
195+
res.count === 0 ||
196+
(res.results.length === 0 && nextPageUrl === null)
197+
) {
189198
// If response does not have any notification, show no-notifications message.
190199
$(".ow-no-notifications").removeClass("ow-hide");
191200
$("#ow-mark-all-read").addClass("disabled");
@@ -225,7 +234,9 @@ function notificationWidget($) {
225234
busy = true;
226235
if (lastRenderedPage > renderedPages) {
227236
$(".ow-notification-wrapper div.page:last").remove();
228-
var addedDiv = pageContainer(fetchedPages[lastRenderedPage - renderedPages - 1]);
237+
var addedDiv = pageContainer(
238+
fetchedPages[lastRenderedPage - renderedPages - 1],
239+
);
229240
$(".ow-notification-wrapper").prepend(addedDiv);
230241
lastRenderedPage -= 1;
231242
}
@@ -247,7 +258,9 @@ function notificationWidget($) {
247258

248259
function notificationListItem(elem) {
249260
let klass;
250-
const datetime = dateTimeStampToDateTimeLocaleString(new Date(elem.timestamp));
261+
const datetime = dateTimeStampToDateTimeLocaleString(
262+
new Date(elem.timestamp),
263+
);
251264

252265
if (!notificationReadStatus.has(elem.id)) {
253266
if (elem.unread) {
@@ -408,7 +421,9 @@ function markNotificationRead(elem) {
408421
function notificationHandler($, elem) {
409422
var notification = fetchedPages
410423
.flat()
411-
.find((notification) => notification.id == elem.get(0).id.replace("ow-", "")),
424+
.find(
425+
(notification) => notification.id == elem.get(0).id.replace("ow-", ""),
426+
),
412427
targetUrl = elem.data("location");
413428

414429
// If notification is unread then send read request
@@ -433,7 +448,9 @@ function notificationHandler($, elem) {
433448
</div>
434449
<div class="ow-notification-date">${datetime}</div>
435450
`);
436-
$(".ow-message-title").html(convertMessageWithRelativeURL(notification.message));
451+
$(".ow-message-title").html(
452+
convertMessageWithRelativeURL(notification.message),
453+
);
437454
$(".ow-message-description").html(notification.description);
438455
$(".ow-overlay-notification").removeClass("ow-hide");
439456

@@ -473,7 +490,8 @@ function initWebSockets($) {
473490
}
474491
// Check whether to display notification toast
475492
if (data.notification) {
476-
let toast = $(`<div class="ow-notification-toast ${data.notification.level}"
493+
let toast =
494+
$(`<div class="ow-notification-toast ${data.notification.level}"
477495
data-location="${convertAbsoluteURLToRelativeURL(data.notification.target_url)}"
478496
id="ow-${data.notification.id}">
479497
<div class="icon ow-notify-close btn" role="button" tabindex="1"></div>

0 commit comments

Comments
 (0)