SKILL.blade.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. ---
  2. name: inertia-svelte-development
  3. description: "Develops Inertia.js v2 Svelte client-side applications. Activates when creating Svelte pages, forms, or navigation; using Link, Form, or router; working with deferred props, prefetching, or polling; or when user mentions Svelte with Inertia, Svelte pages, Svelte forms, or Svelte navigation."
  4. license: MIT
  5. metadata:
  6. author: laravel
  7. ---
  8. @php
  9. /** @var \Laravel\Boost\Install\GuidelineAssist $assist */
  10. @endphp
  11. # Inertia Svelte Development
  12. ## Documentation
  13. Use `search-docs` for detailed Inertia v2 Svelte patterns and documentation.
  14. ## Basic Usage
  15. ### Page Components Location
  16. Svelte page components should be placed in the `{{ $assist->inertia()->pagesDirectory() }}` directory.
  17. ### Page Component Structure
  18. @boostsnippet("Basic Svelte Page Component", "svelte")
  19. <script>
  20. export let users
  21. </script>
  22. <div>
  23. <h1>Users</h1>
  24. <ul>
  25. {#each users as user (user.id)}
  26. <li>{user.name}</li>
  27. {/each}
  28. </ul>
  29. </div>
  30. @endboostsnippet
  31. ## Client-Side Navigation
  32. ### Basic Link Component
  33. Use `<Link>` for client-side navigation instead of traditional `<a>` tags:
  34. @boostsnippet("Inertia Svelte Navigation", "svelte")
  35. <script>
  36. import { Link } from '@inertiajs/svelte'
  37. </script>
  38. <Link href="/">Home</Link>
  39. <Link href="/users">Users</Link>
  40. <Link href={`/users/${user.id}`}>View User</Link>
  41. @endboostsnippet
  42. ### Link With Method
  43. @boostsnippet("Link With POST Method", "svelte")
  44. <script>
  45. import { Link } from '@inertiajs/svelte'
  46. </script>
  47. <Link href="/logout" method="post">Logout</Link>
  48. @endboostsnippet
  49. ### Prefetching
  50. Prefetch pages to improve perceived performance:
  51. @boostsnippet("Prefetch on Hover", "svelte")
  52. <script>
  53. import { Link } from '@inertiajs/svelte'
  54. </script>
  55. <Link href="/users" prefetch>Users</Link>
  56. @endboostsnippet
  57. ### Programmatic Navigation
  58. @boostsnippet("Router Visit", "svelte")
  59. <script>
  60. import { router } from '@inertiajs/svelte'
  61. function handleClick() {
  62. router.visit('/users')
  63. }
  64. // Or with options
  65. function createUser() {
  66. router.visit('/users', {
  67. method: 'post',
  68. data: { name: 'John' },
  69. onSuccess: () => console.log('Success!'),
  70. })
  71. }
  72. </script>
  73. @endboostsnippet
  74. ## Form Handling
  75. @if($assist->inertia()->hasFormComponent())
  76. ### Form Component (Recommended)
  77. The recommended way to build forms is with the `<Form>` component:
  78. @boostsnippet("Form Component Example", "svelte")
  79. <script>
  80. import { Form } from '@inertiajs/svelte'
  81. </script>
  82. <Form action="/users" method="post" let:errors let:processing let:wasSuccessful>
  83. <input type="text" name="name" />
  84. {#if errors.name}
  85. <div>{errors.name}</div>
  86. {/if}
  87. <input type="email" name="email" />
  88. {#if errors.email}
  89. <div>{errors.email}</div>
  90. {/if}
  91. <button type="submit" disabled={processing}>
  92. {processing ? 'Creating...' : 'Create User'}
  93. </button>
  94. {#if wasSuccessful}
  95. <div>User created!</div>
  96. {/if}
  97. </Form>
  98. @endboostsnippet
  99. @if($assist->inertia()->hasFormComponentResets())
  100. ### Form Component Reset Props
  101. The `<Form>` component supports automatic resetting:
  102. - `resetOnError` - Reset form data when the request fails
  103. - `resetOnSuccess` - Reset form data when the request succeeds
  104. - `setDefaultsOnSuccess` - Update default values on success
  105. Use the `search-docs` tool with a query of `form component resetting` for detailed guidance.
  106. @boostsnippet("Form With Reset Props", "svelte")
  107. <script>
  108. import { Form } from '@inertiajs/svelte'
  109. </script>
  110. <Form
  111. action="/users"
  112. method="post"
  113. resetOnSuccess
  114. setDefaultsOnSuccess
  115. let:errors
  116. let:processing
  117. let:wasSuccessful
  118. >
  119. <input type="text" name="name" />
  120. {#if errors.name}
  121. <div>{errors.name}</div>
  122. {/if}
  123. <button type="submit" disabled={processing}>
  124. Submit
  125. </button>
  126. </Form>
  127. @endboostsnippet
  128. @else
  129. Note: This version of Inertia does not support `resetOnError`, `resetOnSuccess`, or `setDefaultsOnSuccess` on the `<Form>` component. Using these props will cause errors. Upgrade to Inertia v2.2.0+ to use these features.
  130. @endif
  131. Forms can also be built using the `useForm` hook for more programmatic control. Use the `search-docs` tool with a query of `useForm helper` for guidance.
  132. @endif
  133. ### `useForm` Hook
  134. @if($assist->inertia()->hasFormComponent() === false)
  135. For Inertia v2.0.x: Build forms using the `useForm` hook as the `<Form>` component is not available until v2.1.0+.
  136. @else
  137. For more programmatic control or to follow existing conventions, use the `useForm` hook:
  138. @endif
  139. @boostsnippet("useForm Example", "svelte")
  140. <script>
  141. import { useForm } from '@inertiajs/svelte'
  142. const form = useForm({
  143. name: '',
  144. email: '',
  145. password: '',
  146. })
  147. function submit() {
  148. $form.post('/users', {
  149. onSuccess: () => $form.reset('password'),
  150. })
  151. }
  152. </script>
  153. <form on:submit|preventDefault={submit}>
  154. <input type="text" bind:value={$form.name} />
  155. {#if $form.errors.name}
  156. <div>{$form.errors.name}</div>
  157. {/if}
  158. <input type="email" bind:value={$form.email} />
  159. {#if $form.errors.email}
  160. <div>{$form.errors.email}</div>
  161. {/if}
  162. <input type="password" bind:value={$form.password} />
  163. {#if $form.errors.password}
  164. <div>{$form.errors.password}</div>
  165. {/if}
  166. <button type="submit" disabled={$form.processing}>
  167. Create User
  168. </button>
  169. </form>
  170. @endboostsnippet
  171. ## Inertia v2 Features
  172. ### Deferred Props
  173. Use deferred props to load data after initial page render:
  174. @boostsnippet("Deferred Props with Empty State", "svelte")
  175. <script>
  176. export let users
  177. </script>
  178. <div>
  179. <h1>Users</h1>
  180. {#if !users}
  181. <div class="animate-pulse">
  182. <div class="h-4 bg-gray-200 rounded w-3/4 mb-2"></div>
  183. <div class="h-4 bg-gray-200 rounded w-1/2"></div>
  184. </div>
  185. {:else}
  186. <ul>
  187. {#each users as user (user.id)}
  188. <li>{user.name}</li>
  189. {/each}
  190. </ul>
  191. {/if}
  192. </div>
  193. @endboostsnippet
  194. ### Polling
  195. Use the `usePoll` hook to automatically refresh data at intervals. It handles cleanup on unmount and throttles polling when the tab is inactive.
  196. @boostsnippet("Basic Polling", "svelte")
  197. <script>
  198. import { usePoll } from '@inertiajs/svelte'
  199. export let stats
  200. usePoll(5000)
  201. </script>
  202. <div>
  203. <h1>Dashboard</h1>
  204. <div>Active Users: {stats.activeUsers}</div>
  205. </div>
  206. @endboostsnippet
  207. @boostsnippet("Polling With Request Options and Manual Control", "svelte")
  208. <script>
  209. import { usePoll } from '@inertiajs/svelte'
  210. export let stats
  211. const { start, stop } = usePoll(5000, {
  212. only: ['stats'],
  213. onStart() {
  214. console.log('Polling request started')
  215. },
  216. onFinish() {
  217. console.log('Polling request finished')
  218. },
  219. }, {
  220. autoStart: false,
  221. keepAlive: true,
  222. })
  223. </script>
  224. <div>
  225. <h1>Dashboard</h1>
  226. <div>Active Users: {stats.activeUsers}</div>
  227. <button on:click={start}>Start Polling</button>
  228. <button on:click={stop}>Stop Polling</button>
  229. </div>
  230. @endboostsnippet
  231. - `autoStart` (default `true`) — set to `false` to start polling manually via the returned `start()` function
  232. - `keepAlive` (default `false`) — set to `true` to prevent throttling when the browser tab is inactive
  233. ## Server-Side Patterns
  234. Server-side patterns (Inertia::render, props, middleware) are covered in inertia-laravel guidelines.
  235. ## Common Pitfalls
  236. - Using traditional `<a>` links instead of Inertia's `<Link>` component (breaks SPA behavior)
  237. - Forgetting to add loading states (skeleton screens) when using deferred props
  238. - Not handling the `undefined` state of deferred props before data loads
  239. - Using `<form>` without preventing default submission (use `<Form>` component or `on:submit|preventDefault`)
  240. - Forgetting to check if `<Form>` component is available in your Inertia version