Skip to content

feat: include the component instance in the dev __svelte_meta - #19286

Merged
dummdidumm merged 6 commits into
mainfrom
feat/svelte-meta-component
Nov 17, 2026
Merged

dummdidumm merged 6 commits into
mainfrom
feat/svelte-meta-component

Conversation

@baseballyama

Copy link
Copy Markdown
Member

__svelte_meta tells tools which file and line rendered an element, but not which component instance did. When the same component is rendered many times (a button in a list, a row in an {#each}), a tool that starts from a DOM element (an element picker, an inspector) cannot tell the instances apart, so it can't jump to the right instance in a component tree or show that instance's state.

This adds component to the dev __svelte_meta: the component context the element was rendered in.

element.__svelte_meta = {
  parent: dev_stack,
  loc: { file, line, column },
  component: component_context // new
};
  • It is the context that is already current when the element is created, so no new tracking is added. Inside a snippet it is the component rendering the snippet, which matches the render-tree parent stack (loc.file still points at the file that declared the snippet).
  • Also set for <svelte:element>.

Performance

  • Production: unchanged (__svelte_meta is only assigned in dev).
  • Dev: one more property on an object that is already created per element. Mounting 20 000 elements in dev (jsdom, median of 35 warm runs, 3 runs each): 419–433 ms with the change, 417–437 ms without it, so within noise.
  • Memory: an element now references its component context. Effects already reference their DOM nodes, so this only matters if app code keeps a detached element alive, in which case its context stays alive with it (dev only).

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

`__svelte_meta` tells tools which file and line rendered an element, but
not which component instance did. When the same component is rendered many
times (a button in a list, a row in an `{#each}`), a tool that starts from a
DOM element (an element picker, an inspector) cannot tell the instances
apart, so it can't jump to the right one in a component tree or show that
instance's state.

`component` is the component context the element was rendered in. It is
the context that was already current when the element was created, so no
new tracking is added: inside a snippet it is the component rendering the
snippet, matching the render-tree `parent` stack.

Cost: one more property on an object that dev mode already creates per
element; nothing in production, where `__svelte_meta` isn't assigned.
Mounting 20 000 elements in dev (jsdom, median of 35 runs, 3 runs each)
took 419-433 ms with the change and 417-437 ms without it.
@pkg-svelte-dev

pkg-svelte-dev Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Install the latest version of svelte from 2c1c788:

pnpm add https://pkg.svelte.dev/svelte/c/2c1c788232c825cf735282319e1e500b6db7aa50

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/svelte/pr/19286

@changeset-bot

changeset-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2c1c788

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

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

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
Copy Markdown

baseballyama added a commit that referenced this pull request Nov 17, 2026
`__svelte_meta` tells tools which file and line rendered an element, but
not which component instance did. When the same component is rendered many
times (a button in a list, a row in an `{#each}`), a tool that starts from a
DOM element (an element picker, an inspector) cannot tell the instances
apart, so it can't jump to the right one in a component tree or show that
instance's state.

`component` is the component context the element was rendered in. It is
the context that was already current when the element was created, so no
new tracking is added: inside a snippet it is the component rendering the
snippet, matching the render-tree `parent` stack.

Cost: one more property on an object that dev mode already creates per
element; nothing in production, where `__svelte_meta` isn't assigned.
Mounting 20 000 elements in dev (jsdom, median of 35 runs, 3 runs each)
took 419-433 ms with the change and 417-437 ms without it.

From #19286, taken into this branch so devtools can resolve
an element to its component instance before that PR is merged.
baseballyama added a commit to sveltejs/vite-devtools-svelte that referenced this pull request Nov 17, 2026
…er-module skip

- The kit3 compat profile installs outside the workspace, so it got Svelte
  5.57.3 from npm, which has no `svelte/internal/devtools`; its dev server
  never started. It now links the branch build too.
- The pinned emit-dev-events commit includes sveltejs/svelte#19286
  (`__svelte_meta.component`), which the element picker will use.
- plugin.ts lost branch coverage below its 95% threshold when the
  componentTracking cases went away: the tracking transform's skip of a
  server-compiled module is tested directly.
baseballyama added a commit to sveltejs/vite-devtools-svelte that referenced this pull request Nov 17, 2026
The Reactivity and Components panels make you find the component first,
which is hard in a large app; users start from what they see on the page.

The Svelte Inspector fork (packages/inspector) is now shipped with the
plugin and linked to the DevTools: in inspector mode a click selects the
element's component instance in the Components panel (tree revealed,
details open), and Alt/⌥+click opens the source in the editor as before.
The hover label shows the instance and both actions. A "Pick element"
button in the Components panel turns the inspector on in the app page.

- The instance comes from `__svelte_meta.component` (sveltejs/svelte#19286),
  resolved to the nearest tracked component.
- The pick goes page -> dev server -> every DevTools UI (two tabs, or the
  Vite DevTools dock, which is opened on a pick). The server keeps the last
  pick for 10 s, so a dock panel loaded by that pick still gets it.
- Changes to the forked files are marked and listed in
  packages/inspector/README.md; without the DevTools link the fork behaves
  as upstream. `svelteDevtools({ inspector })` sets its options or turns
  it off.
- Library components are recognised also when their filename is relative
  to the project (`node_modules/...`).
@baseballyama
baseballyama marked this pull request as ready for review November 17, 2026 15:40
Copilot AI balanced review requested due to automatic review settings November 17, 2026 15:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Comment thread .changeset/svelte-meta-component.md Resolved
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
@baseballyama

Copy link
Copy Markdown
Member Author

Thank you for your review!

…stom-element slots still receive metadata without `component`, preventing element-based tooling from identifying their rendering instance.

This commit fixes the issue reported at packages/svelte/src/internal/client/dev/elements.js:30

### Missing metadata writers
The merged base also assigns `__svelte_meta` in `dom/blocks/html.js`, `dom/blocks/snippet.js`, and `dom/blocks/slot.js`. Those assignments still contain only `parent` and `loc`. Since `assign_locations` skips elements that already have metadata, this new assignment never fills in their `component` field.

For example, the `<b>` emitted by `svelte-meta-html-snippet`, both `<section>` roots in `svelte-meta-raw-snippet`, and the native `<slot>` elements in `svelte-meta-native-slot` all have an undefined `__svelte_meta.component`, while their enclosing `<output>` has a valid context. An inspector selecting these roots cannot use the new field to identify the rendering instance.

### Fix and validation
The suggested patch adds the current component context to all three remaining metadata writers and adds ownership assertions to their existing tests. Those assertions failed in all five applicable DOM/hydration variants before the runtime fix; all 29 focused `svelte-meta` tests passed afterward. The full suite was not run.

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: dummdidumm <sholthausen@web.de>
@dummdidumm
dummdidumm merged commit 8ab6fe7 into main Nov 17, 2026
16 checks passed
@dummdidumm
dummdidumm deleted the feat/svelte-meta-component branch November 17, 2026 16:18
@github-actions github-actions Bot mentioned this pull request Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants