From f39d026137039a39fc48055f7d65c78ed46d45df Mon Sep 17 00:00:00 2001 From: Jacob Wright Date: Tue, 14 Aug 2018 16:25:15 -0600 Subject: [PATCH] Change actions to init on mount rather than hydrate Looking at the discussion on https://github.com/sveltejs/svelte/pull/1247 it sounds like this was the intended way actions would be set up to work (which is why we didn't add a `mount` lifecycle method). I *believe* this is a fix in the original implementation. Complaints in chat about this surfaced the issue. Some libraries expect the element to be in the DOM when initializing and these libraries cannot be used without any lifecycle hook. @PaulMaly is requesting this be looked at, and I agree with his assesment. What's more, this change *should* be backwards compatable. Actions which work before this change should continue working after this change. --- src/compile/nodes/Element.ts | 2 +- test/js/samples/action/expected-bundle.js | 4 ++-- test/js/samples/action/expected.js | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compile/nodes/Element.ts b/src/compile/nodes/Element.ts index 11eea89107..43e690bfd7 100644 --- a/src/compile/nodes/Element.ts +++ b/src/compile/nodes/Element.ts @@ -846,7 +846,7 @@ export default class Element extends Node { block.addVariable(name); const fn = `%actions-${action.name}`; - block.builders.hydrate.addLine( + block.builders.mount.addLine( `${name} = ${fn}.call(#component, ${this.var}${snippet ? `, ${snippet}` : ''}) || {};` ); diff --git a/test/js/samples/action/expected-bundle.js b/test/js/samples/action/expected-bundle.js index d47781ac82..9453f3c7aa 100644 --- a/test/js/samples/action/expected-bundle.js +++ b/test/js/samples/action/expected-bundle.js @@ -133,7 +133,7 @@ var proto = { /* generated by Svelte vX.Y.Z */ function link(node) { - + function onClick(event) { event.preventDefault(); history.pushState(null, null, event.target.href); @@ -155,11 +155,11 @@ function create_main_fragment(component, ctx) { a = createElement("a"); a.textContent = "Test"; a.href = "#"; - link_action = link.call(component, a) || {}; }, m(target, anchor) { insert(target, a, anchor); + link_action = link.call(component, a) || {}; }, p: noop, diff --git a/test/js/samples/action/expected.js b/test/js/samples/action/expected.js index cb03f97b8d..fab6e6a2b6 100644 --- a/test/js/samples/action/expected.js +++ b/test/js/samples/action/expected.js @@ -2,7 +2,7 @@ import { assign, createElement, detachNode, init, insert, noop, proto } from "svelte/shared.js"; function link(node) { - + function onClick(event) { event.preventDefault(); history.pushState(null, null, event.target.href); @@ -25,11 +25,11 @@ function create_main_fragment(component, ctx) { a = createElement("a"); a.textContent = "Test"; a.href = "#"; - link_action = link.call(component, a) || {}; }, m(target, anchor) { insert(target, a, anchor); + link_action = link.call(component, a) || {}; }, p: noop,