---
name: svelte-store-builder
description: "A complete Svelte store module with typed state, named action functions, and optional localStorage or sessionStorage persistence that survives server-side rendering, written in JavaScript or TypeScript and commented for the next reader. Use when a Svelte app needs a new store with typed state, action functions, and optional browser storage persistence that still works under server-side rendering. Do not use for reviewing an existing store, or managing state that lives inside a single component. Related topics: coding, svelte."
---

# Svelte Store Builder

A complete Svelte store module with typed state, named action functions, and optional localStorage or sessionStorage persistence that survives server-side rendering, written in JavaScript or TypeScript and commented for the next reader.

## When to use

Use this skill when a Svelte app needs a new store with typed state, action functions, and optional browser storage persistence that still works under server-side rendering. Do not use it for reviewing an existing store, or managing state that lives inside a single component.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Type the state from the initial JSON so every action is checked against the same shape.
3. Create the store with the chosen kind: writable with initial state, readable with a start function and cleanup, or derived with an explicit initial value.
4. Expose each action as a named function beside subscribe, updating through a new object or array reference.
5. When persistence is selected, hydrate on creation and save on change behind a window check with a try/catch around the parse, plus a reset action.
6. Add brief comments and a three-line component usage example, then confirm the module runs no browser-only code at import time.

## Inputs

- **Store name** (free text, e.g. "myStore")
- **Store type** (choose one: writable, readable, derived)
- **Language** (choose one: JavaScript, TypeScript)
- **Initial state** (longer free text)
- **Persistence** (choose one: none, localStorage, sessionStorage)
- **Actions** (longer free text)

## Output

One code block labeled js or ts with the complete store module, then a three-line usage example inside a component.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Svelte Store

Write a Svelte store module.

- Store name: {{Store name}}
- Store type: {{Store type}}
- Language: {{Language}}
- Initial state: {{Initial state}}
- Persistence: {{Persistence}}
- Actions: {{Actions}}

Type the state from the initial JSON (a TypeScript type or a JSDoc typedef) so every action is checked against it. Create the store with the chosen type: writable with the initial state, readable with a start function that returns its cleanup, derived from the source stores with an explicit initial value. Expose actions as named functions on an object alongside subscribe, so components call store.increment() rather than importing set, and make each action use update with a new object or array reference. When persistence is selected, read the saved value on creation and write on every change, guarded by a check that window exists so the module works during server-side rendering, and wrap the JSON parse in a try/catch that falls back to the initial state. Add a reset action when persistence is on. Comment each section briefly.

Return the full source in one code block labeled js or ts to match the language, followed by a three-line usage example in a component.

## Pitfalls

- Touching localStorage at module import time, which throws during server-side rendering.
- Mutating the state object in place inside update, which skips subscribers because the reference is unchanged.
- Returning a derived store without an initial value, so subscribers briefly see undefined before the first computation.
