Skip to content

Conversation

@paoloricciuti
Copy link
Member

This adds a warning when you are in legacy mode and you set the expression of an attachment reading from an object.

if you have a component like this

<script>
    let state = {
        count: 0,
        attachment: (node)=>{
            alert("attachment run");
        }
    };
</script>

<button onclick={()=>{
    state.count++;
}}>{state.count}</button>

<div {@attach state.attachment}></div>

and you don't realise you are in legacy mode the attachment will rerun every time you change count because you are updating state.count and in legacy mode that invalidate the whole state object (hence also state.attachment which means the attachment reruns).

While the above example is blatantly in legacy mode this is more subtle.

<script>
    import { State } from "some-lib";
    let state = new State();
</script>

<button onclick={()=>{
    state.count++;
}}>{state.count}</button>

<div {@attach state.attachment}></div>

This is up for discussion because i wonder if this warning could be too intrusive...an alternative @dummdidumm suggested is that we could opt in to runes mode if you use @attach...however this would be a breaking change (although probably not breaking for a lot of people) and since it's a new feature we can just label it as a bug. This would also hinder the adoption of attachments a bit and it would be cool if we could use them in legacy mode.

WDYT?

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

@changeset-bot
Copy link

changeset-bot bot commented May 18, 2025

🦋 Changeset detected

Latest commit: 7d1e9f1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

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

@svelte-docs-bot
Copy link

@github-actions
Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@15948

@Rich-Harris
Copy link
Member

Alternative: #15962

@Rich-Harris
Copy link
Member

closing in favour of #15962

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants