Skip to content

Commit c365e19

Browse files
Revert "Implicit handshake: 33% faster time-to-first-interactivity (#5499)"
This reverts commit e5fbab0.
1 parent bbda8a8 commit c365e19

File tree

3 files changed

+13
-28
lines changed

3 files changed

+13
-28
lines changed

nicegui/client.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ def build_response(self, request: Request, status_code: int = 200) -> Response:
159159
**core.app.config.socket_io_js_query_params,
160160
'client_id': self.id,
161161
'next_message_id': self.outbox.next_message_id,
162-
'implicit_handshake': not _is_prefetch(request),
163162
}
164163
vue_html, vue_styles, vue_scripts, imports, js_imports, js_imports_urls = \
165164
generate_resources(prefix, self.elements.values())
@@ -209,8 +208,10 @@ async def connected(self, timeout: float | None = None) -> None:
209208
return
210209
self._waiting_for_connection.set()
211210
self._connected.clear()
211+
purpose = (self.request.headers.get('Sec-Purpose') or self.request.headers.get('Purpose') or '').lower()
212+
is_prefetch = 'prefetch' in purpose and 'prerender' not in purpose
212213
try:
213-
await asyncio.wait_for(self._connected.wait(), timeout=None if _is_prefetch(self.request) else timeout)
214+
await asyncio.wait_for(self._connected.wait(), timeout=None if is_prefetch else timeout)
214215
except asyncio.TimeoutError as e:
215216
raise ClientConnectionTimeout(self) from e
216217

@@ -453,8 +454,3 @@ def prune_instances(cls, *, client_age_threshold: float = 60.0) -> None:
453454

454455
except Exception:
455456
log.exception('Error while pruning clients')
456-
457-
458-
def _is_prefetch(request: Request) -> bool:
459-
purpose = (request.headers.get('Sec-Purpose') or request.headers.get('Purpose') or '').lower()
460-
return 'prefetch' in purpose and 'prerender' not in purpose

nicegui/nicegui.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,6 @@ async def _exception_handler_500(request: Request, exception: Exception) -> Resp
187187
return client.build_response(request, 500)
188188

189189

190-
@sio.on('connect')
191-
async def _on_connect(sid: str, data: dict[str, Any], _=None) -> bool:
192-
query = {k: v[0] for k, v in urllib.parse.parse_qs(data.get('QUERY_STRING', '')).items()}
193-
if query.get('implicit_handshake', '') == 'true' and not await _on_handshake(sid, query):
194-
return False
195-
return True
196-
197-
198190
@sio.on('handshake')
199191
async def _on_handshake(sid: str, data: dict[str, Any]) -> bool:
200192
client = Client.instances.get(data['client_id'])
@@ -208,8 +200,7 @@ async def _on_handshake(sid: str, data: dict[str, Any]) -> bool:
208200
else:
209201
client.environ = sio.get_environ(sid)
210202
await sio.enter_room(sid, client.id)
211-
client.handle_handshake(sid, data['document_id'],
212-
int(data['next_message_id']) if 'next_message_id' in data else None)
203+
client.handle_handshake(sid, data['document_id'], data.get('next_message_id'))
213204
assert client.tab_id is not None
214205
await core.app.storage._create_tab_storage(client.tab_id) # pylint: disable=protected-access
215206
return True

nicegui/static/nicegui.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,6 @@ function createApp(elements, options) {
345345
window.path_prefix = options.prefix;
346346
window.nextMessageId = options.query.next_message_id;
347347
window.ackedMessageId = -1;
348-
options.query.document_id = window.documentId;
349-
options.query.tab_id = TAB_ID;
350-
options.query.old_tab_id = OLD_TAB_ID;
351348
window.socket = io(url, {
352349
path: `${options.prefix}/_nicegui_ws/socket.io`,
353350
query: options.query,
@@ -380,20 +377,21 @@ function createApp(elements, options) {
380377
if (transport?.ws?.send) transport.ws.send = wrapFunction(transport.ws.send);
381378
if (transport?.doWrite) transport.doWrite = wrapFunction(transport.doWrite);
382379

383-
function finishHandshake(ok) {
380+
const args = {
381+
client_id: window.clientId,
382+
document_id: window.documentId,
383+
tab_id: TAB_ID,
384+
old_tab_id: OLD_TAB_ID,
385+
next_message_id: window.nextMessageId,
386+
};
387+
window.socket.emit("handshake", args, (ok) => {
384388
if (!ok) {
385389
console.log("reloading because handshake failed for clientId " + window.clientId);
386390
window.location.reload();
387391
}
388392
window.did_handshake = true;
389393
document.getElementById("popup").ariaHidden = true;
390-
}
391-
392-
if (options.query.implicit_handshake) {
393-
finishHandshake(true);
394-
} else {
395-
window.socket.emit("handshake", options.query, finishHandshake);
396-
}
394+
});
397395
},
398396
connect_error: (err) => {
399397
if (err.message == "timeout") {

0 commit comments

Comments
 (0)