zulip/web/tests/reload.test.cjs
Aman Agrawal fc9a9a9110 reload: Defer state_data fetching to avoid partial-transfer failures.
When the app triggers a reload (e.g., after an expired event queue),
the server previously embedded the entire state_data (multiple
megabytes) in the HTML response. On marginal networks — most commonly
Firefox background tabs resuming from laptop suspend — this large
response could suffer a partial transfer (NS_ERROR_NET_PARTIAL_TRANSFER),
leaving users stuck on the loading screen indefinitely. See #36094.

Fix this by having the reload navigate to /?state_data=deferred
instead of doing a bare page reload. The server skips the expensive
do_events_register() call and returns a small HTML shell. The client
then fetches state_data via POST /json/register with hardcoded
parameters matching the server-side do_events_register call. This is
the same pattern spectators already use.

The /compatibility check that precedes the reload only verifies that
a small response can be fetched; the deferred approach ensures the
actual page load is comparably small, with state_data fetched
separately via an API call that has its own error handling.

Fixes #36094.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-21 08:13:38 -07:00

92 lines
2.6 KiB
JavaScript

"use strict";
const assert = require("node:assert/strict");
const {mock_esm, set_global, zrequire} = require("./lib/namespace.cjs");
const {run_test, noop} = require("./lib/test.cjs");
const $ = require("./lib/zjquery.cjs");
const channel = mock_esm("../src/channel");
mock_esm("../src/popup_banners", {
open_reloading_application_banner: noop,
});
// override file-level function call in reload.ts
window.addEventListener = noop;
const reload = zrequire("reload");
const reload_state = zrequire("reload_state");
set_global("window", {
to_$: () => $("window-stub"),
location: {
href: "http://zulip.zulipdev.com/#",
reload: noop,
replace: noop,
},
});
run_test("old_metadata_string_is_stale", () => {
assert.ok(reload.is_stale_refresh_token({reload_data: {hash: ""}}, Date.now()), true);
});
run_test("recent_token_is_not_stale ", () => {
assert.ok(
!reload.is_stale_refresh_token(
{
hash: "",
timestamp: Date.parse("21 Jan 2022 00:00:00 GMT"),
},
Date.parse("23 Jan 2022 00:00:00 GMT"),
),
);
});
run_test("old_token_is_stale ", () => {
assert.ok(
reload.is_stale_refresh_token(
{
hash: "",
timestamp: Date.parse("13 Jan 2022 00:00:00 GMT"),
},
Date.parse("23 Jan 2022 00:00:00 GMT"),
),
);
});
run_test("reload", () => {
channel.get = (opts) => {
assert.equal(opts.url, "/compatibility");
opts.success();
};
// No reload has been initiated so "maybe_reload_*" shouldn't
// do anything
reload.maybe_reset_pending_reload_timeout("compose_start");
assert.equal(reload.reset_reload_timeout, undefined);
reload.maybe_reset_pending_reload_timeout("compose_end");
assert.equal(reload.reset_reload_timeout, undefined);
// Initiate reload should create a new timeout and creates
// reset_reload_timeout
reload.initiate({});
assert.equal(typeof reload.reset_reload_timeout, "function");
reload.maybe_reset_pending_reload_timeout("compose_start");
reload.maybe_reset_pending_reload_timeout("compose_end");
});
run_test("immediate_reload_skips_compatibility_check", () => {
reload_state.clear_for_testing();
// do_reload_app should run synchronously without the /compatibility
// check, so channel.get should not be called. Setting it to
// undefined ensures a clear error if it is called unexpectedly.
channel.get = undefined;
reload.initiate({immediate: true, save_compose: true});
assert.ok(reload_state.is_in_progress());
});