[GH-ISSUE #2946] [Issue]: Toggling or editing one extension restarts every other enabled extension #1522

Closed
opened 2026-10-21 05:37:54 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2946

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

The code that runs extension JavaScript is keyed on the whole list of extensions. So changing any single extension — toggling one on/off, editing it, deleting it, or importing another — tears down and re-runs every enabled extension's code from scratch, not just the one you touched.

For extensions that just register a few event listeners this is mostly invisible (they get cleaned up and re-registered). But any extension that keeps state in memory loses it, and any one-time side effect it did outside the cleanup API (a raw appendChild, a global counter, a self-registering widget) gets duplicated each time. So a user who flips one extension can silently corrupt or double up an unrelated extension.

How to reproduce (try it yourself)

  1. Install two enabled JS extensions:
    • Ext A js:
      const n = (window.__a = (window.__a || 0) + 1);
      marinara.addElement("body", "div", { textContent: "A loaded #" + n });
      
    • Ext B js:
      marinara.addElement("body", "div", { textContent: "B" });
      
  2. Load the app — Ext A shows A loaded #1.
  3. Go to Settings → Extensions and toggle Ext B off, then on.

You'll see: Ext A re-runs every time you touch Ext B — A loaded #2, then #3 — even though you never changed Ext A, and its in-memory state (window.__a) climbs because the whole module is re-evaluated.
Expected: changing Ext B affects only Ext B; Ext A keeps running with its existing state untouched.

Where it is

packages/client/src/components/layout/CustomThemeInjector.tsx — the "Execute enabled extension JS" effect at lines 103-271, whose dependency array is [installedExtensions] (line 271). The teardown at 268-270 runs cleanupFns for all extensions, and each run rebuilds every extension from scratch (fresh apiKey at 116, fresh Blob/objectURL/import at 248-252). It's driven by useUpdateExtension invalidating the extensions query (use-extensions.ts:67-76, staleTime: 0), which hands React a new array reference.

What's happening

When one extension's data changes, the query returns a new top-level array reference, the effect's dependency changes, and the single effect cleans up and re-imports every enabled extension — there's no per-extension isolation. (For the record: it is not caused by the 15s refetch — unchanged data keeps the same reference via structural sharing — and not a reorder, since the server orders by installedAt. A single element's data change is enough to break reference equality of the array.)

Suggested fix

Isolate extensions from each other: render one small keyed component (or run one keyed effect) per ext.id, so only the changed extension reloads. Or diff installedExtensions by id + a content hash of (enabled, css, js) and only tear down / re-load the extensions that actually changed, leaving the rest running.


AI-investigated against v2.0.6; cited lines were read and the repro verified. Please confirm before fixing.

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2946 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** The code that runs extension JavaScript is keyed on the *whole* list of extensions. So changing any single extension — toggling one on/off, editing it, deleting it, or importing another — tears down and re-runs **every** enabled extension's code from scratch, not just the one you touched. For extensions that just register a few event listeners this is mostly invisible (they get cleaned up and re-registered). But any extension that keeps state in memory loses it, and any one-time side effect it did outside the cleanup API (a raw `appendChild`, a global counter, a self-registering widget) gets duplicated each time. So a user who flips one extension can silently corrupt or double up an unrelated extension. **How to reproduce (try it yourself)** 1. Install two enabled JS extensions: - **Ext A** js: ``` const n = (window.__a = (window.__a || 0) + 1); marinara.addElement("body", "div", { textContent: "A loaded #" + n }); ``` - **Ext B** js: ``` marinara.addElement("body", "div", { textContent: "B" }); ``` 2. Load the app — Ext A shows `A loaded #1`. 3. Go to Settings → Extensions and toggle **Ext B** off, then on. **You'll see:** Ext A re-runs every time you touch Ext B — `A loaded #2`, then `#3` — even though you never changed Ext A, and its in-memory state (`window.__a`) climbs because the whole module is re-evaluated. **Expected:** changing Ext B affects only Ext B; Ext A keeps running with its existing state untouched. **Where it is** `packages/client/src/components/layout/CustomThemeInjector.tsx` — the "Execute enabled extension JS" effect at lines 103-271, whose dependency array is `[installedExtensions]` (line 271). The teardown at 268-270 runs `cleanupFns` for **all** extensions, and each run rebuilds every extension from scratch (fresh `apiKey` at 116, fresh Blob/objectURL/import at 248-252). It's driven by `useUpdateExtension` invalidating the extensions query (`use-extensions.ts:67-76`, `staleTime: 0`), which hands React a new array reference. **What's happening** When one extension's data changes, the query returns a new top-level array reference, the effect's dependency changes, and the single effect cleans up and re-imports **every** enabled extension — there's no per-extension isolation. (For the record: it is **not** caused by the 15s refetch — unchanged data keeps the same reference via structural sharing — and **not** a reorder, since the server orders by `installedAt`. A single element's data change is enough to break reference equality of the array.) **Suggested fix** Isolate extensions from each other: render one small keyed component (or run one keyed effect) per `ext.id`, so only the changed extension reloads. Or diff `installedExtensions` by `id` + a content hash of `(enabled, css, js)` and only tear down / re-load the extensions that actually changed, leaving the rest running. --- *AI-investigated against v2.0.6; cited lines were read and the repro verified. Please confirm before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 28, 2026):

Fixed on staging: extension CSS/JS injection is isolated per extension, so changing one extension no longer tears down and reruns every other enabled extension.

<!-- gh-comment-id:4827784507 --> @SpicyMarinara commented on GitHub (Jun 28, 2026): Fixed on staging: extension CSS/JS injection is isolated per extension, so changing one extension no longer tears down and reruns every other enabled extension.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1522
No description provided.