Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

reload: Defer state_data fetching to avoid partial-transfer failures.

Aman Agrawalfc9a9a911027
diff --git a/web/src/reload.ts b/web/src/reload.ts
index b09cf78e2a8..ba46c2bcd52 100644
--- a/web/src/reload.ts
+++ b/web/src/reload.ts
@@ -166,20 +166,34 @@ function do_reload_app(
blueslip.log("Starting server requested page reload");
reload_state.set_state_to_in_progress();
- // Sometimes the window.location.reload that we attempt has no
- // immediate effect (likely by browsers trying to save power by
- // skipping requested reloads), which can leave the Zulip app in a
- // broken state and cause lots of confusing tracebacks. So, we
- // set ourselves to try reloading a bit later, both periodically
- // and when the user focuses the window.
+ // Navigate to the same page with ?state_data=deferred, which
+ // tells the server to skip embedding state_data in page_params
+ // (the client will fetch it via /json/register instead). This
+ // is important because the /compatibility check above only
+ // verifies that a small response can be fetched; when the
+ // network is marginal (e.g., Firefox background tabs resuming
+ // from laptop suspend), the multi-megabyte HTML with embedded
+ // state_data can suffer a partial transfer while small requests
+ // succeed. With ?state_data=deferred, the HTML response is
+ // comparable in size to /compatibility, and the state_data is
+ // fetched separately via an API call that has its own error
+ // handling.
+ const reload_url = new URL(window.location.href);
+ reload_url.searchParams.set("state_data", "deferred");
+
+ // Sometimes the navigation we attempt has no immediate effect
+ // (likely by browsers trying to save power by skipping requested
+ // reloads), which can leave the Zulip app in a broken state and
+ // cause lots of confusing tracebacks. So, we set ourselves to
+ // try reloading a bit later, both periodically and when the user
+ // focuses the window.
setTimeout(() => {
// We add this handler after a bit of delay, because in some
- // browsers, processing window.location.reload causes the
- // window to gain focus, and duplicate reload attempts result
- // in the browser sending duplicate requests to `/`.
+ // browsers, processing the navigation causes the window to
+ // gain focus, and duplicate reload attempts result in the
+ // browser sending duplicate requests to `/`.
$(window).one("focus", () => {
blueslip.log("Retrying on-focus page reload");
-
window.location.reload();
});
}, 5000);
@@ -196,7 +210,7 @@ function do_reload_app(
blueslip.error("Failed to clean up before reloading", undefined, error);
}
- window.location.reload();
+ window.location.replace(reload_url.toString());
}
export function initiate({
diff --git a/web/src/ui_init.js b/web/src/ui_init.js
index 6e425c88e78..8443a5d9f17 100644
--- a/web/src/ui_init.js
+++ b/web/src/ui_init.js
@@ -803,28 +803,69 @@ function show_try_zulip_modal() {
$(() => {
update_page_loading_indicator_notice();
- // Remove '?show_try_zulip_modal', if present.
+ // Remove transient query parameters.
const url = new URL(window.location.href);
+ let needs_url_cleanup = false;
if (url.searchParams.has("show_try_zulip_modal")) {
url.searchParams.delete("show_try_zulip_modal");
+ needs_url_cleanup = true;
+ }
+ if (url.searchParams.has("state_data")) {
+ url.searchParams.delete("state_data");
+ needs_url_cleanup = true;
+ }
+ if (needs_url_cleanup) {
window.history.replaceState(window.history.state, "", url.toString());
}
- if (page_params.is_spectator) {
- const data = {
- apply_markdown: true,
- client_capabilities: JSON.stringify({
- notification_settings_null: true,
- bulk_message_deletion: true,
- user_avatar_url_field_optional: true,
- // Set this to true when stream typing notifications are implemented.
- stream_typing_notifications: false,
- user_settings_object: true,
- empty_topic_name: true,
- individual_emoji_changes: true,
- }),
- client_gravatar: false,
- };
+ if (page_params.no_event_queue) {
+ // For spectators and client-triggered reloads, fetch
+ // state_data via the API rather than reading it from the
+ // (potentially very large) HTML response. For reloads, this
+ // avoids partial-transfer failures that leave users stuck on
+ // the loading screen. See #36094.
+ let data;
+ if (page_params.is_spectator) {
+ data = {
+ apply_markdown: true,
+ client_capabilities: JSON.stringify({
+ notification_settings_null: true,
+ bulk_message_deletion: true,
+ user_avatar_url_field_optional: true,
+ // Set this to true when stream typing notifications are implemented.
+ stream_typing_notifications: false,
+ user_settings_object: true,
+ empty_topic_name: true,
+ individual_emoji_changes: true,
+ }),
+ client_gravatar: false,
+ };
+ } else {
+ // Logged-in reload: request the same parameters the
+ // server-side do_events_register call uses for the initial
+ // page load. Keep these in sync with the call in
+ // zerver/lib/home.py.
+ data = {
+ apply_markdown: true,
+ client_gravatar: true,
+ slim_presence: true,
+ include_subscribers: "partial",
+ presence_history_limit_days: page_params.presence_history_limit_days_for_web_app,
+ client_capabilities: JSON.stringify({
+ notification_settings_null: true,
+ bulk_message_deletion: true,
+ user_avatar_url_field_optional: true,
+ stream_typing_notifications: true,
+ linkifier_url_template: true,
+ user_list_incomplete: true,
+ include_deactivated_groups: true,
+ archived_channels: true,
+ empty_topic_name: true,
+ simplified_presence_events: true,
+ individual_emoji_changes: true,
+ }),
+ };
+ }
channel.post({
url: "/json/register",
data,
diff --git a/web/tests/reload.test.cjs b/web/tests/reload.test.cjs
index ede47f64f7f..1c592848e52 100644
--- a/web/tests/reload.test.cjs
+++ b/web/tests/reload.test.cjs
@@ -19,6 +19,7 @@ const reload_state = zrequire("reload_state");
set_global("window", {
to_$: () => $("window-stub"),
location: {
+ href: "http://zulip.zulipdev.com/#",
reload: noop,
replace: noop,
},
diff --git a/zerver/lib/home.py b/zerver/lib/home.py
index 13f2d04e250..a39f4947b09 100644
--- a/zerver/lib/home.py
+++ b/zerver/lib/home.py
@@ -108,7 +108,16 @@ def build_page_params_for_home_page_load(
individual_emoji_changes=True,
)
- if user_profile is not None:
+ # When the client triggers a reload (e.g., after an expired event
+ # queue), it navigates to /?state_data=deferred. In that case, we
+ # skip the expensive do_events_register() call and let the client
+ # fetch state via the /json/register API instead. This makes the
+ # HTML response much smaller and avoids partial-transfer failures
+ # on flaky networks (common in Firefox background tabs resuming
+ # from laptop suspend). See #36094.
+ is_client_reload = request.GET.get("state_data") == "deferred" and user_profile is not None
+
+ if user_profile is not None and not is_client_reload:
client = RequestNotes.get_notes(request).client
assert client is not None
state_data = do_events_register(
@@ -127,6 +136,12 @@ def build_page_params_for_home_page_load(
)
queue_id = state_data["queue_id"]
default_language = state_data["user_settings"]["default_language"]
+ elif user_profile is not None:
+ # Client-triggered reload: the client will fetch state_data
+ # via /json/register after the page loads.
+ state_data = None
+ queue_id = None
+ default_language = user_profile.default_language
else:
# The spectator client will be fetching the /register response
# for spectators via the API.
@@ -176,9 +191,9 @@ def build_page_params_for_home_page_load(
two_fa_enabled_user=two_fa_enabled and bool(default_device(user_profile)),
is_spectator=user_profile is None,
presence_history_limit_days_for_web_app=settings.PRESENCE_HISTORY_LIMIT_DAYS_FOR_WEB_APP,
- # There is no event queue for spectators since
- # events support for spectators is not implemented yet.
- no_event_queue=user_profile is None,
+ # The client will fetch state_data (and create an event
+ # queue) via /json/register for spectators and reloads.
+ no_event_queue=user_profile is None or is_client_reload,
show_try_zulip_modal=show_try_zulip_modal,
non_workplace_pricing_eligible=realm_eligible_for_non_workplace_pricing(realm),
is_cloud_realm_with_discounted_plan=realm_on_discounted_cloud_plan(realm),
diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py
index 8d42700456c..9e4c802f382 100644
--- a/zerver/tests/test_home.py
+++ b/zerver/tests/test_home.py
@@ -637,6 +637,21 @@ class HomeTest(ZulipTestCase):
# Should be successful after calling 2fa login function.
self.check_rendered_logged_in_app(result)
+ @override_settings(TERMS_OF_SERVICE_VERSION=None)
+ def test_home_reload(self) -> None:
+ # When the client triggers a reload via ?state_data=deferred,
+ # the server should skip the expensive do_events_register()
+ # call and return state_data=None so the client fetches it
+ # via /json/register instead. See #36094.
+ self.login("hamlet")
+ result = self.client_get("/", {"state_data": "deferred"})
+ self.check_rendered_logged_in_app(result)
+
+ page_params = self._get_page_params(result)
+ self.assertIsNone(page_params["state_data"])
+ self.assertTrue(page_params["no_event_queue"])
+ self.assertFalse(page_params["is_spectator"])
+
@override_settings(TERMS_OF_SERVICE_VERSION=None)
def test_num_queries_for_realm_admin(self) -> None:
# Verify number of queries for Realm admin isn't much higher than for normal users.