Question presented to candidate:
"Our src/stores/ folder has forty Pinia stores and nobody knows which page owns which. How would you colocate stores with the routes and components that use them, and what does that change for bundle size and store lifetime?"
What a strong answer should cover:
- Colocation: keep a feature store next to the feature that owns it (
features/users/users.store.tsbesideUsersPage.vue); keep only truly app-wide stores (session, settings) in a shared folder. - With lazy routes, a store imported only by one route's components ships in that route's chunk; a store imported by two lazy routes is split into a shared chunk. Stores are created on first
useStore(), not at import. - Store ids are global per Pinia: two features using the same id silently get the same store, so namespace ids (
users-feature) or derive them from the folder. - Lifetime: stores live until disposed; for feature-scoped data either reset or
$dispose()on leaving the feature (Pinia 4.1 warns when you dispose a stale reference), or use component-scoped state. - Not all feature state needs a store: state for one component subtree can be provide/inject or a composable, which gives one instance per subtree.
Clarifying questions expected:
- "Is this a plain Vite SPA or Nuxt (which has its own store auto-import conventions)?"
- "Should a feature keep its data when the user navigates away and comes back?"
Code / implementation expected: Optional — a folder layout, a colocated setup store, and the lazy route that uses it.