Skip to content
MCP Servers
Skill

/svelte-core-bestpractices

Guidance on writing fast, robust, modern Svelte code. Load this skill whenever in a Svelte project and asked to write/edit or analyze a Svelte component or module. Covers reactivity, event handling, styling, integration with libraries and more.

From plugin
ai-tools
3403 skills1 agent1 MCP

Claude Code

$ npx -y skills add sveltejs/ai-tools --skill svelte-core-bestpractices --agent claude-code

SKILL.md

svelte-core-bestpractices.SKILL.md
name: svelte-core-bestpractices
description: Guidance on writing fast, robust, modern Svelte code. Load this skill whenever in a Svelte project and asked to write/edit or analyze a Svelte component or module. Covers reactivity, event handling, styling, integration with libraries and more.

`$state`

Only use the `$state` rune for variables that should be _reactive_ — in other words, variables that cause an `$effect`, `$derived` or template expression to update. Everything else can be a normal variable.

Objects and arrays (`$state({...})` or `$state([...])`) are made deeply reactive, meaning mutation will trigger updates. This has a trade-off: in exchange for fine-grained reactivity, the objects must be proxied, which has performance overhead. In cases where you're dealing with large objects that are only ever reassigned (rather than mutated), use `$state.raw` instead. This is often the case with API responses, for example.

`$derived`

To compute something from state, use `$derived` rather than `$effect`:

// do this
let square = $derived(num * num);

// don't do this
let square;

$effect(() => {
	square = num * num;
});

> [!NOTE] `$derived` is given an expression, _not_ a function. If you need to use a function (because the expression is complex, for example) use `$derived.by`.

Deriveds are writable — you can assign to them, just like `$state`, except that they will re-evaluate when their expression changes.

If the derived expression is an object or array, it will be returned as-is — it is _not_ made deeply reactive. You can, however, use `$state` inside `$derived.by` in the rare cases that you need this.

`$effect`

Effects are an escape hatch and should mostly be avoided. In particular, avoid updating state inside effects.

  • If you need to sync state to an external library such as D3, it is often neater to use [`{@attach ...}`](references/attach.md)
  • If you need to run some code in response to user interaction, put the code directly in an event handler or use a [function binding](references/bind.md) as appropriate
  • If you need to log values for debugging purposes, use [`$inspect`](references/inspect.md)
  • If you need to observe something external to Svelte, use [`createSubscriber`](references/svelte-reactivity.md)

Never wrap the contents of an effect in `if (browser) {...}` or similar — effects do not run on the server.

`$props`

Treat props as though they will change. For example, values that depend on props should usually use `$derived`:

// @errors: 2451
let { type } = $props();

// do this
let color = $derived(type === 'danger' ? 'red' : 'green');

// don't do this — `color` will not update if `type` changes
let color = type === 'danger' ? 'red' : 'green';

`$inspect.trace`

`$inspect.trace` is a debugging tool for reactivity. If something is not updating properly or running more than it should you can add `$inspect.trace(label)` as the first line of an `$effect` or `$derived.by` (or any function they call) to trace their dependencies and discover which one triggered an update.

`#key` block

While the `#key` block is useful when you want transitions/animations to run when a certain expression changes, it can be very expensive — it unmounts every element/component inside each time, so should be used sparingly. Instead of using `#key` to cause expressions in a child component's `<script>` block to re-run, for example, consider wrapping those expressions in `$derived`.

Events

Any element attribute starting with `on` is treated as an event listener:

<button onclick={() => {...}}>click me</button>

<!-- attribute shorthand also works -->
<button {onclick}>...</button>

<!-- so do spread attributes -->
<button {...props}>...</button>

If you need to attach listeners to `window` or `document` you can use `<svelte:window>` and `<svelte:document>`:

<svelte:window onkeydown={...} />
<svelte:document onvisibilitychange={...} />

Avoid using `onMount` or `$effect` for this.

Snippets

[Snippets](references/snippet.md) are a way to define reusable chunks of markup that can be instantiated with the [`{@render ...}`](references/render.md) tag, or passed to components as props. They must be declared within the template.

{#snippet greeting(name)}
	<p>hello {name}!</p>
{/snippet}

{@render greeting('world')}

> [!NOTE] Snippets declared at the top level of a component (i.e. not inside elements or blocks) can be referenced inside `<script>`. A snippet that doesn't reference component state is also available in a `<script module>`, in which case it can be exported for use by other components.

Each blocks

Prefer to use [keyed each blocks](references/each.md) — this improves performance by allowing Svelte to surgically insert or remove items rather than updating the DOM belonging to existing items.

> [!NOTE] The key _must_ uniquely identify the object. Do not use the index as a key.

Avoid destructuring if you need to mutate the item (with something like `bind:value={item.count}`, for example).

Using JavaScript variables in CSS

If you have a JS variable that you want to use inside CSS you can set a CSS custom property with the `style:` directive.

<div style:--columns={columns}>...</div>

You can then reference `var(--columns)` inside the component's `<style>`.

Styling child components

The CSS in a component's `<style>` is scoped to that component. If a parent component needs to control the child's styles, the preferred way is to use CSS custom properties:

<!-- Parent.svelte -->
<Child --color="red" />

<!-- Child.svelte -->
<h1>Hello</h1>

<style>
	h1 {
		color: var(--color);
	}
</style>

If this is impossible (for example, the child component comes from a library) you can use `:global` to override styles:

<div>
	<Child />
</div>

<style>
	div :global {
		h1 {
			color: red;
		}
	}
</style>

Context

Consider using context instead o

Read more
@sveltejs
Ships withai-tools

Repo for the official Svelte MCP server.

3403 skills1 agent1 MCP
Get the whole plugin

Stars on GitHub

Momentum

stars in total
340
Star history of sveltejs/ai-tools on GitHub, a chart by star-history.com

Other skills on ai-tools.

svelte-code-writer

svelte-code-writer

CLI tools for Svelte 5 documentation lookup and code analysis. MUST be used whenever creating, editing or analyzing any Svelte…

@sveltejs@sveltejsInstall Skill
svelte-mcp-feedback

svelte-mcp-feedback

Report Svelte MCP autofixer opportunities when the user corrects a Svelte or SvelteKit mistake the agent made. Use after…

@sveltejs@sveltejsInstall Skill

More skills from other plugins.

gitnexus-cli

gitnexus-cli

Use when the user needs to run GitNexus CLI commands like analyze/index a repo, check status, clean the index, generate a wiki,…

@abhigyanpatwari@abhigyanpatwariInstall Skill
fastmcp-client-cli

fastmcp-client-cli

Query and invoke tools on MCP servers using fastmcp list and fastmcp call. Use when you need to discover what tools a server…

fastmcp3d29k
@prefecthq@prefecthqInstall Skill
n8n-agents

n8n-agents

Design n8n AI agents the right way. Use when building or editing any @n8n/n8n-nodes-langchain.* AI node — an AI Agent, LLM chain,…

n8n-mcp3d23k
@czlonkowski@czlonkowskiInstall Skill
post-to-xhs

post-to-xhs

小红书内容发布技能。支持两种发布模式:(1) 上传图文模式 - 图片+短文;(2) 写长文模式 - 长篇文章+排版模板。 支持两种输入方式:用户提供完整内容和图片/图片URL,直接发布;或提供网页URL,自动提取内容和图片。…

@xpzouying@xpzouyingInstall Skill

Questions about svelte-core-bestpractices.

Which coding agents can use svelte-core-bestpractices?
svelte-core-bestpractices installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.