Skip to content

fix(web): load mqtt.js only when Push connects - #1943

Merged
lohanidamodar merged 2 commits into
mainfrom
fix/web-lazy-mqtt
Sep 29, 2026
Merged

lohanidamodar merged 2 commits into
mainfrom
fix/web-lazy-mqtt

Conversation

@lohanidamodar

@lohanidamodar lohanidamodar commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

The web Push service imported mqtt and buffer at module scope, so every app that bundles the web or console SDK carried mqtt.js, even when it never uses Push. mqtt.js has side effects, so bundlers could not tree-shake it.

  • push.ts keeps only import type from mqtt. mqtt and buffer load through a dynamic import() on the first connect, cached, and reset if the load fails.
  • close() during that load now stops the pending connect instead of opening an orphaned connection.
  • connect is read off mqtt.js's default export, since its browser build (dist/mqtt.esm.js) exports only a default.
  • The JWT/session user-id helpers decode base64 with atob + TextDecoder, because userTopic() needs them before connecting.
  • tsconfig: "module": "ES2020", so tsc accepts import().
  • The IIFE build sets inlineDynamicImports, since a script tag has no module loader. ESM/CJS keep mqtt external and lazy.

React Native is unchanged: Metro does not split dynamic imports by default, so a lazy import would gain nothing there.

Test Plan

  • php example.php web console, then npm ci, format:check, lint, analyse and build in examples/web all pass. dist/esm and dist/cjs contain import('mqtt') and no static mqtt import.
  • Console SDK (cloud 2.3.x spec) generated with this change and linked into vibes (vite build):
@appwrite.io/console vibes index chunk gzip
17.0.0 7,144,109
18.0.0 7,535,517 1,968,009
this PR 7,148,099 1,859,801

That is −387,418 B raw and −108,208 B gzip, back within 4 KB of 17.0.0 (the difference is the new SDK methods). vibes does not use Push, so Push and mqtt.js drop out entirely.

  • A minimal Vite consumer that uses Push: its main chunk is 102 KB (35 KB gzip), and mqtt.js (366 KB, 110 KB gzip) and buffer are separate chunks loaded by import(). Against 18.0.0 the same app is one 495 KB chunk.
  • WebChromium (the IIFE bundle subscribing through the mock MQTT broker) and WebNode pass in CI. A Node probe of Push.subscribe against a closed port rejects with connect ECONNREFUSED, so mqtt.js loads and attempts the connect.

Related PRs and Issues

Console SDK 18.0.0 (appwrite/sdk-for-console#114) introduced the static import.

push.ts imported mqtt and buffer statically, so every bundle carried them even without Push. They now load through a dynamic import on the first connect; the IIFE build inlines them.
@greptile-apps

greptile-apps Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Converts mqtt.js to lazy loading in the web client.

The PR appears safe to merge, though behavioral coverage for closing Push during transport loading is still missing.

Fix All in Claude CodeFindings

  1. P2 Lazy-load cancellation lacks coverage ▶
Fix with agent prompt
### Issue 1
templates/web/src/services/push.ts.twig:undefined-544
The new cancellation check runs after MQTT loads, but no behavioral test exercises closing Push during that load. The browser Push test uses the IIFE bundle, which inlines the import, and the Node test never connects Push. An ESM or CJS consumer test should verify that closing during the pending load does not open a connection; build and lint checks cannot catch a regression here.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR defers loading MQTT and Buffer until Push connects, cancels a connect closed during loading, and adjusts the web build for dynamic imports.

  • The latest change reads connect from MQTT’s default export when present, with a namespace fallback.
  • The previous request for a behavioral test of closing Push during a pending load remains outstanding.

Reviews (2) · Last reviewed commit: "fix(web): read connect off mqtt.js's def..."

this.fallbackClientId;

const generation = this.generation;
const { connect, Buffer } = await loadTransport();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Lazy-load cancellation lacks coverage

The new cancellation check runs after MQTT loads, but no behavioral test exercises closing Push during that load. The browser Push test uses the IIFE bundle, which inlines the import, and the Node test never connects Push. An ESM or CJS consumer test should verify that closing during the pending load does not open a connection; build and lint checks cannot catch a regression here.

Prompt To Fix With AI
This is a comment left during a code review.
Path: templates/web/src/services/push.ts.twig
Line: 543

Comment:
**Lazy-load cancellation lacks coverage**

The new cancellation check runs after MQTT loads, but no behavioral test exercises closing Push during that load. The browser Push test uses the IIFE bundle, which inlines the import, and the Node test never connects Push. An ESM or CJS consumer test should verify that closing during the pending load does not open a connection; build and lint checks cannot catch a regression here.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Codex

The browser build (dist/mqtt.esm.js) exports only a default, so the namespace had no connect and Push never opened in the browser.
@lohanidamodar
lohanidamodar merged commit 7ad26c1 into main Sep 29, 2026
61 checks passed
@lohanidamodar
lohanidamodar deleted the fix/web-lazy-mqtt branch September 29, 2026 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants