Skip to content

Shopify Dude Fix

Shopify Section JavaScript Stops Working After a Theme Editor Update

Make section scripts survive dynamic theme editor rendering.

Quick answer: Initialize each section on initial page load and on shopify:section:load; clean up on shopify:section:unload.
On this page

Why this happens

The theme editor can replace section markup without reloading the document. A listener attached only at DOMContentLoaded will miss replacement elements.

Implementation

Scope initialization to the section element. Use an idempotent initializer and remove listeners or disconnect observers when the section is unloaded. Keep the event registration in a shared theme asset rather than repeating it in every section instance.

Example

document.addEventListener('shopify:section:load', event => {
  initializeSection(event.target);
});
document.addEventListener('shopify:section:unload', event => {
  destroySection(event.target);
});

Implement these functions for your component and also initialize existing sections on initial page load. A WeakMap can track instances without relying on globally unique selectors.

Common misunderstanding: Theme editor events are not a general storefront AJAX lifecycle. Explicitly initialize components after your own markup replacements too.
How to test this:

  1. Open the editor
  2. add two instances
  3. change a setting
  4. reorder and remove a section
  5. verify controls still work and one click fires only one handler
  6. repeat on the published storefront preview.
Sources and further reading: Shopify reference ยท Further reading