Usually when things work on build
but not start
it has to do with Hot Module Replacement (HMR). Sage 9 uses HMR so that styles, etc, can be updated in the browser while start
is running without reloading the page. It’s a cool feature! But because it “reloads” content by directly modifying the DOM, it sometimes doesn’t trigger particular document events that other things might be expecting (i.e. onload
) or it triggers them at the wrong time (i.e. before things have actually loaded).
I haven’t tested this out myself, but this post has a quick fix to disable HMR and BrowserSync, so start
will still watch and rebuild your assets, you just have to reload the page in your browser manually: Is it possible to "watch" without browsersync?
Alternatively, you could refactor your code to trigger off of some other event that isn’t tied to document load (this is probably why msnry.layout()
works when triggers off the click in your code).