Skip to content

Avoid storing outside DOM elements on custom element instances - #4019

Draft
PhilippeCollin wants to merge 2 commits into
mainfrom
fix-stored-outside-elements
Draft

PhilippeCollin wants to merge 2 commits into
mainfrom
fix-stored-outside-elements

Conversation

@PhilippeCollin

Copy link
Copy Markdown

PR Summary:

Avoid caching references to outside DOM elements on custom element instances so elements continue working cleanly when other sections or components are dynamically re-rendered.

Why are these changes introduced?

When a section or custom element is replaced dynamically (for example via the Section Rendering API or in the theme editor), other custom elements that remain on the page can hold stale references to detached DOM nodes if they cached document-wide lookups on this during construction.

This draft PR shows the changes surfaced by the proposed theme/no-stored-outside-element ESLint rule in Shopify/theme-tools#1413 across Dawn's custom elements.

What approach did you take?

Across Dawn's 36 JS files, the rule reported 13 warnings in 12 files:

  • 7 outside-element lookups replaced with getters (assets/product-form.js, assets/cart-notification.js header, assets/details-disclosure.js, assets/localization-form.js, assets/main-search.js, assets/predictive-search.js, assets/price-per-item.js): look up the element on demand via a getter instead of caching it once in constructor().
  • 2 hot-path outside lookups cached with an isConnected check (assets/global.js HeaderDrawer.header, assets/quick-order-list.js stickyHeaderElement): both are read during resize/scroll handlers, so they reuse the cached node while it stays connected and re-query if replaced.
  • 2 inside-element lookups scoped to this.querySelector(...) (assets/cart-notification.js notification, assets/cart.js lineItemStatusElement): #cart-notification, #shopping-cart-line-item-status, and #CartDrawer-LineItemStatus live inside their respective custom elements, so scoping to this avoids document-wide queries altogether.
  • 1 single-method lookup changed to a local const (assets/media-gallery.js preventStickyHeader): this.stickyHeader was only read inside that method.
  • 1 intentional exception left unchanged (assets/cart-disclosure-modal.js this.opener): <cart-disclosure-modal> moves itself to document.body, explicitly observes this.opener?.isConnected via a MutationObserver, and removes itself when its opener disconnects.

Visual impact on existing themes

None.

@PhilippeCollin
PhilippeCollin force-pushed the fix-stored-outside-elements branch from f938bd0 to bd590a4 Compare November 24, 2026 15:30

This branch has not been deployed

No deployments
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.

1 participant