-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: ensure tree-shaken if blocks correctly hydrate #14589
Conversation
🦋 Changeset detectedLatest commit: 063ce36 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Not sure I understand how treeshaking can affect the behaviour? |
Rollup is being super smart here, but it see's that the Ending up with this: function if_block(node, fn, elseif = false) {
if (hydrating) {
hydrate_next();
}
var anchor = node;
var condition = null;
var flags = elseif ? EFFECT_TRANSPARENT : 0;
var has_branch = false;
const update_branch = (new_condition, fn2) => {
if (condition === (condition = new_condition)) return;
let mismatch = false;
if (hydrating) {
const is_else = (
/** @type {Comment} */
anchor.data === HYDRATION_START_ELSE
);
if (condition === is_else) {
anchor = traverse_nodes(true);
set_hydrate_node(anchor);
set_hydrating(false);
mismatch = true;
}
}
if (mismatch) {
set_hydrating(true);
}
};
block(() => {
has_branch = false;
if (!has_branch) {
update_branch(null);
}
}, flags);
if (hydrating) {
anchor = hydrate_node;
}
} |
preview: https://svelte-dev-git-preview-svelte-14589-svelte.vercel.app/ this is an automated message |
If the behaviour is different with treeshaking vs without then it must be a bug in Rollup, surely? It's suspicious that we're calling |
(though I don't really understand why that wouldn't show up in our test suite already...) |
I thought that at first, but it's not because the entire block gets tree shaken, including the bit that does the hydration of the template – so we just need to ensure we move the hydration node accordingly. |
Reproduced the bug locally in the sandbox, which confirms that it's nothing to do with treeshaking, which is a relief because that would have indicated a very serious bug in Rollup. The answer is quite simple — per #14589 (comment), we just need to do |
Fixes #14588. We need to ensure we move the hydration node when the
if
block basically all gets tree-shaken away. Unfortunately, I found no way to test this as this relies on Rollup treeshaking away parts of the bundle and runtime.