Skip to content

Content update on React navigation #9893

Open
1 of 1 issue completed
Open

Description

@fregante

This issue is about two related bugs:

  • observe remains active on views where it shouldn't run (example)
  • components Svelte remain in memory and still listening to events like URL changes

Svelte component issue

When I mount() components the first time and then they're removed by GitHub, they're still in memory but dormant, potentially garbage collected if we're lucky.

However with self-updating components, they continue to self-update even when not attached to the document.

Repro

  1. Open https://github.com/refined-github/refined-github/blob/main/source/features/action-pr-link.tsx
  2. Click on the "Features" folder
  3. Click on another feature in the sidebar

the previous component is still updated in memory even if rendered nowhere. This specifically happens because GitHub is removing the first one, so observe will mount a new one.

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. fregante commented on Jul 28, 2026

    @fregante
    MemberAuthor

    Solution: unmount on navigate

    1. unload features (I might slip in an auto-unload right before init)
    2. use the signal to unmount

    But then again a url store would then become meaningless if every page load removed all svelte components. I don’t think it's practical. Also this would bring us back to "a new component is needed, but rgh-seen is still there"

  2. fregante commented on Jul 28, 2026

    @fregante
    MemberAuthor

    Solution: unmount before mount

    Or maybe "unmount previous before mounting again", no need to specifically unmount everything every time.

    This is currently being achieved via singleton() function

  3. fregante commented on Jul 28, 2026

    @fregante
    MemberAuthor

    Solution: mount once, re-attach later

    Instead of calling mount() every time, we just restore the element that was removed by GitHub:

    const container = <div />;
    mount(ClosingRemarks, {target: container, props});
    observe('.js-discussion', anchor => {
    anchor.after(container);
    }, {signal});

    We actually had something similar in JSX land too: getUi() generates and caches a component, and a manager adds or removes it

    function updateUi({delegateTarget: field}: DelegateEvent<Event, HTMLTextAreaElement>): void {
    if (isVulnerableToLinkLoss(field.value)) {
    closestElement(bannerParent, field).append(getUi(field));
    } else {
    getUi(field).remove();
    }
    }

  4. fregante commented on Jul 28, 2026

    @fregante
    MemberAuthor

    Solution: disconnect callback

    Custom elements have a native disconnectCallback, but sadly extensions cannot use custom elements (outside the MAIN world).

    Such a callback would let us detect when React removes our element, while keeping the element active as well as re-activate it if it comes back into the document.

    There are two lightweight ways to replicate this:

    • create a custom element like <rgh-widget> to independently inject into MAIN and to use as an event emitter
    • use ResizeObserver to detect connect/disconnect events:
    import { mount, unmount, type Component } from 'svelte';
    
    export function componentRoot<P extends Record<string, unknown>>(Comp: Component<P>, props?: P) {
      const root = document.createElement('div');
      let instance: Record<string, any> | undefined;
    
      new ResizeObserver(() => {
        if (root.isConnected && !instance) {
          instance = mount(Comp, { target: root, props: (props ?? {}) as P });
        } else if (!root.isConnected && instance) {
          unmount(instance);
          instance = undefined;
        }
      }).observe(root);
    
      return root;
    }

    and then:

    - mount(Counter, {target})
    + target.append(componentRoot(Counter))
  5. added
    meta
    on Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    meta

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions