- Refer to variables -
<h1>Hello {name}</h1>
- Dynamic attributes -
<img src={src} />
or<img {src} />
- accessibility -
<img {src} alt='accessible description.' />
- accessibility -
- Styling -
<style> CSS... </style>
- Nested components -
<Nested />
- Component names are always capitalized, to distinguish them from HTML elements.
- HTML tags -
<p>{@html string}</p>
-
Assignments -
<button on:click={increment}> </button>
-
Declarations -
$: doubled = count * 2
-
$: console.log()
$: { console.log() }
$: if (count >= 10) { }
-
-
() => numbers = [...numbers, numbers.length + 1];
-
() => numbers[numbers.length] = numbers.length + 1;
-
Name of updated variable must appear on the left hand side of the assignment.
const obj = { foo: { bar: 1 } }; const foo = obj.foo; foo.bar = 2; // instead const obj = { foo: { bar: 1 } }; obj.foo.bar = 2;
-
- Declaring props -
<script> export let answer; </script>
- Default values -
<script> export let answer = 'secret'; </script>
- Spread props -
<PackageInfo {...pkg} />
- If blocks -
{#if count > 10} .. {/if}
- Else blocks -
{#if count > 10} .. {:else} .. {/if}
- Else-if blocks -
{#if count > 10} .. {:else if count < 5} .. {:else} .. {/if}
- Each blocks:
{#each colors as color} .. {/each}
{#each colors as color, i} .. {/each}
- Keyed each blocks -
{#each things as thing (thing.id)} .. {/each}
- Await blocks:
{#await promise} .. {:then value} .. {:catch error} .. {/await}
{#await promise then number} .. {/await}
- DOM events -
<div on:pointermove={handleMove}> .. </div>
- Inline handlers -
<div on:pointermove={(e) => .. }> .. </div>
- Event modifiers -
<button on:click|once={() => alert(clicked)}>
- List of modifiers:
preventDefault
stopPropagation
passive
nonpassive
capture
once
self
trusted
- Chaining modifiers -
on:click|once|capture={ .. }
- List of modifiers:
- Component events:
- Create event dispatcher
<script> import { createEventDispatcher } from "svelte"; const dispatch = createEventDispatcher(); </script>
<Inner on:message={handleMessage}} />
- Create event dispatcher
- Event forwarding -
<Inner on:message />
- DOM event forwarding -
<button on:click> Push </button>
- Data flow in Svelte is top down.
- Text inputs -
<input bind:value={name}>
- Numeric inputs:
<input type="number" bind:value={a} min="0" max="10" />
<input type="range" bind:value={a} min="0" max="10" />
- Checkbox inputs -
<input type="checkbox" bind:checked={yes}>
- Select bindings -
<select bind:value={selected}>
- Group inputs:
<input type="radio" name="scoops" value={number} bind:group={scoops} />
<input type="checkbox" name="colors" value={color} bind:group={colors} />
- Select multiple -
<select multiple bind:value={flavors}> .. </select>
- Textarea inputs:
<textarea bind:value={value}> </textarea>
<textarea bind:value></textarea>
- onMount- runs after component is first rendered to the DOM.
- beforeUpdate and afterUpdate:
- before and after DOM is updated.
beforeUpdate
runs beforeonMount
.
- tick - returns and resolves a promise:
- after state changes have been applied to the DOM.
- immediately, if there are no pending state changes.
- Writable stores -
const count = writable(0);
count.update((n) => n + 1);
count.set(0);
- Auto-subscriptions:
onDestroy(unsubscribe);
- reference a store with
$
. Example,{$count}
- Readable stores -
readable(initialValue, function)
- Derived stores -
derived(store, function)
- Custom stores:
- a store only needs to correctly implement the
subscribe
method. - can add custom methods to the store.
- a store only needs to correctly implement the
- Store bindings - you can bind writable stores (has a
set
method).<input bind:value={$name}>
<button on:click={() => $name += '!'}> </button>
- Tweens - gradual changes (progress bar).
- Options:
delay
duration
easing
-p => t
functioninterpolate
- Options:
- Springs - frequent changes
- Fade -
transition:fade
- Fly -
transition:fly
- In and out -
in:fade
,out:fly
, and so on. - Custom CSS
- Custom JS - typewriter effect, and so on.
- Transition events:
on:introstart
on:introend
on:outrostart
on:outroend
- Global transitions -
transition:slide|global
- Key Blocks - play transition when value changes,
{#key i} {/key}
. - Deferred transitions -
in:receive
,out:send
- Flip - "First, Last, Invert, Play",
animate:flip
.
- Use - element-level lifecycle functions,
<div class="menu" use:trapFocus>
. - Adding parameters -
<button use:tooltip={{ content }}> </button>
- Contenteditable bindings -
<div bind:innerHTML={html} contenteditable />
contenteditable
attribute support:textContent
innerHTML
- Each block bindings - bind properties inside an
each
block. - Media elements - bind properties of
<audio>
and<video>
elements.readonly
bindings:duration
- total duration of video in secondsbuffered
- array of{start, end}
objectsseekable
- dittoplayed
- dittoseeking
- booleanended
- booleanreadyState
- number between 0 and 4videoWidth
- for video onlyvideoHeght
- for video only
two-way
bindings:currentTime
- current point in the video, in secondsplaybackRate
- how fast to play the video,1
is normalpaused
volume
- value between 0 and 1muted
- boolean
- Dimensions - bind block-level element:
clientWidth
clientHeight
offsetWidth
offsetHeight
- This -
bind:this
- Component bindings - bind to component props.
- Binding to component instances - bind to component instances with
bind:this
.
- Classes:
<button class="card {flipped ? 'flipped' : ''}>
<button class="card" class:flipped={flipped}>
- Shorthand Class -
<button class="card" class:flipped>
- Styles - inline
style
attributes.<button class="card" style:--bg-1="palegoldenrod">
- Component styles - influence styles inside a child component.
:global
should be used as a last resort.<style> .box { background-color: var(--color, #ddd); } </style>
<Box --color="red" />
- Slots - components can have children.
<div class="card"> <slot /> </div>
<Card> <span>Patrick BATEMAN</span> </Card>
- Named slots -
<div class="card"> <slot name="company" /> </div>
- Slot fallbacks -
<slot name="telephone"> telephone </slot>
- Slot props -
<slot {item} />
- Checking for slot content -
{#if $$slots.header} .. {/if}
- setContext and getContext:
- Called during component initialization.
- Context object can include anything, including stores.
- svelte:self - allows a component to contain itself recursively.
- svelte:component - instead of if blocks, create a single dynamic component.
- svelte:element - instead of if blocks, create a single dynamic element.
- svelte:window - add event listeners to the
window
object.- Can also add event modifiers like
preventDetault
.
- Can also add event modifiers like
- svelte:window bindings -
<svelte:window bind:scrollY={y} />
- List of properties you can bind:
innerWidth
innerHeight
outerWidth
outerHieght
scrollX
scrollY
online
- an alias forwindow.navigator.onLine
- All properties are
readonly
exceptscrollX
andscrollY
.
- List of properties you can bind:
- svelte:body - add event listeners to
document.body
. - svelte:document - add event listeners to
document
.- Avoid
mouseenter
andmouseleave
handlers here. - Use
<svelte:body>
instead.
- Avoid
- svelte:head - insert elements inside
<head>
of your document.- Useful for thins like
<title>
and<meta>
tags for good SEO. - In SSR mode, contents of
<svelte:head>
are returned separately from your HTML.
- Useful for thins like
- svelte:options - specify compiler options.
<svelte:options immutable/>
.- Available options:
immutable={true}
immutable={false}
- default.accessors={true}
accessors={false}
- default.namespace="..."
customElement="...
- svelte:fragment - place content in a named slot without wrapping it in a container DOM element.
- Sharing code -
<script context="module> </script>
- Code will run once when the module first evaluates.
- Components talk to each other without any state management.
- Exports - you can't have a default export, because the component is the default export.
- Debug:
- Inspect logs -
console.log(...)
- Pause execution -
(@debug ...}
- Inspect logs -
SvelteKit's main job boils down to three things:
- Routing - figure out which route matches an incoming request
- Loading - get the data needed by the route
- Rendering - generate HTML (on the server) or update the DOM (in the browser)
- Pages - filesystem-based routing, meaning routes are defined by folders.
- Every
+page.svelte
file insidesrc/routes
creates a page. - Navigation is like a single-page app.
- Every
- Layouts - share UI between pages with
+layout.svelte
.+layout.svelte
files apply tochild
andsibling
routes.
- Route parameters - use square brackets around a valid variable name,
src/routes/blog/[slug]/+page.svelte
- For multiple route parameters in one segment:
foo/[bar]x[baz]
- Page data - declare
load
function with+page.server.js
. - Layout data -
+layout.server.js
loads data for every child route.
- Setting headers - set response headers with
setHeaders
.- Commonly used for customizing caching behavior with
Cache-Control
.
- Commonly used for customizing caching behavior with
- Reading and writing cookies:
- read cookies with
cookies.get(name, options);
. - write cookies with
cookies.set(name, value, options);
.- explicitly configure path since browsers set it on the parent of current path.
- SvelteKit sets the following defaults:
- httpOnly: true
- secure: true
- sameSite: lax
- read cookies with
- The $lib alias - anything inside
src/lib
can be accessed using$lib
.- You can use
$lib
as long as the module is in thesrc
folder. - "Put code close to where it's used".
- You can use
- The form element -
<form method="POST"> </form>
- Named form actions -
<form method="POST" action"?/create"> </form>
- Most pages need multiple actions.
- Default actions cannot coexist with named actions.
- Validation:
- HTML form validation like
required
, and so on. - Server-side validation.
- HTML form validation like
- Progressive enhancement - enhance experience with JavaScript,
use:enhance
.- App should still be usable when JavaScript is disabled in browsers.
- Customize use:enhance - customize by providing callback,
use:enhance={() => {}}
.- Useful for adding
pending states
andoptimistic UI
.
- Useful for adding
- General info:
- Uses
+server.js
for API route handlers. - Request handlers must return a
Response
object. - Generating responses with SvelteKit, use
json(data)
.
- Uses
- GET handlers
- POST handlers:
- Use form actions for most cases, less code and works without JavaScript.
- Only mutate
data
in a way that you'd get the same result by reloading the page.
- Other handlers:
PUT
,DELETE
, and so on.
- SvelteKit makes three readonly stores available via
$app/stores
module:page
navigating
updated
- Page:
url
- the URL of current pageparams
- the current page's parametersroute
- object withid
representing the current routestatus
- the HTTP status code of the current pageerror
- the error object of the current pagedata
- the data of the current page with return values of allload
functionsform
- the data returned from aform action
.
- Navigating - represents the current navigation.
from
andto
objects with the following properties:params
route
url
type
- the type of navigation:link
popstate
goto
- Updated -
true/false
, if a new app version has been deployed since opening the page.- Manually check for new versions,
updated.check()
.
- Manually check for new versions,
- Basics - two types of errors;
expected
andunexpected
errors.- Expected errors:
- Created with
@sveltejs/kit
error helper. - Displays expected error message.
- Created with
- Unexpected errors:
- Any other error like
throw new Error()
. - Displays generic
Internal Error
. - Can contain sensitive data.
- Any other error like
- Expected errors:
- Error pages - customize error page by using
+error.svelte
.- SvelteKit renders an error page on
load
function errors.
- SvelteKit renders an error page on
- Fallback errors - customize fallback error page in
src/error.html
file.- Can include:
%sveltekit.status%
- the HTTP status code%sveltekit.error.message%
- the error message
- Can include:
- Redirects - use
throw redirect(...)
to redirect from one page to another.- You can use
throw redirect(...)
inside:load
functions- form actions,
- API routes
handle
hook.
- Common status codes:
303
- for form actions after successful submission307
- for temporary redirects308
- for permanent redirects
- You can use
- handle - receives an
event
object and aresolve
function, returns aResponse
object.- in
src/hooks.server.js
file.
- in
- RequestEvent -
event
object passed intohandle
.- Passed into API routes, form actions, and
load
functions. - Properties and methods:
cookies
fetch
getClientAddress()
isDataRequest
locals
params
request
route
setHeaders(...)
url
- Passed into API routes, form actions, and
- handleFetch -
event
object'sfetch
method.- Inherits the
cookie
andauthorization
headers from the incoming request. - Can make relative requests on the server.
- Internal requests go directly to the handler function when running on the server.
- Inherits the
- handleError - intercept
unexpected
errors.
- Basics:
- export
page options
from the following:+page.js
+page.server.js
+layout.js
+layout.server.js
- Available options:
ssr
- whether or not pages should be server-renderedcsr
- whether to load the SvelteKit clientprerender
- whether to prerender pages at build time, instead of per-requesttrailingSlash
- whether to strip, add, or ignore trailing slashes in URLs
- Options apply to individual pages if exported from
+page.js
or+page.server.js
. - Options apply to group of pages if exported from
+layout.js
or+layout.server.js
. - Export from root layout to define an option for the whole app.
- Child layouts and pages override values from parent.
- export
- ssr - generates HTML on the server.
- SvelteKit does SSR by default.
- Improves performance, resilience, and SEO.
- Setting
ssr
tofalse
in the root+layout.server.js
turns the app into a SPA.
- csr - makes pages interactive and update on navigation without a full-page reload.
- prerender - generate HTML for a page once, at build time.
- Serving static data is cheap and performant.
- Serve large number of users without worrying about
cache-control
headers. - Prerendered pages:
- must not contain
form actions
. - must get the same content from the server.
- must not contain
- Setting
prerenter
totrue
in the root+layout.server.js
turns SvelteKit to SSG.
- trailingSlash:
- trailing URL slashes might harm SEO.
- default value is
never
. - add trailing slash with
always
. - support both cases with
ignore
.
- Preloading - preload data or JavaScript to speed up page loads.
data-sveltekit-preload-data
hover
- default, falls back totap
on mobiletap
- begin preloading on tapoff
- disable preloading
data-sveltekit-preload-code
eager
- preload everything on the pageviewport
- preload everything as it appears in the viewporthover
- defaulttap
off
- preloading programmatically, import from
$app/navigation
:preloadData('/foo')
preloadCode('/bar')
- Reloading the page -
data-sveltekit-reload
, to reload when navigating between pages.
- Optional parameters - use double brackets for optional parameters,
[[foo]]
. - Rest parameters - for matching an unknown number of path segments, use
[...rest]
.- More specific routes will be tested first, making rest parameters as
catch-all
routes.
- More specific routes will be tested first, making rest parameters as
- Param matchers - validate route params with matchers, `/colors/[color=matcher].
- Matchers run both on the server and in the browser.
- Route groups - group routes using parenthesis,
(authed)/app/+page.svelte
.- Useful for grouping routes that need authentication.
- Use
(authed)/+layout.server.js
to control grouped routes behavior. - Use
(authed)/+layout.svelte
to customize grouped routes UI.
- Breaking out of layouts - break from layout hierarchy by adding
@
,[email protected]
.src/routes/a/b/c/+page.svelte
inherits every layout above it:src/routes/+layout.svelte
src/routes/a/+layout.svelte
src/routes/a/b/+layout.svelte
src/routes/a/b/c/+layout.svelte
- The root layout applies to every page of your app. You cannot break out of it.
- Universal load functions - turn server
load
functions into universalload
functions.- Rename
+page.server.js
file to+page.js
. - Functions run on the server during SSR, but also run in the browser when app hydrates.
- Universal vs server docs.
- Use cases for universal
load
functions:- loading data from external API
- use in-memory data
- delay navigation until image has been preloaded
- return from
load
that can't be serialized
- Rename
- Using both load functions - access server data via the
data
property,data.message
.- Data isn't merged, need to explicitly return
message
from universal load function.
- Data isn't merged, need to explicitly return
- Using parent data - access parent data from
load
functions withawait parent()
.Universal load
functions can get data from aparent server load
function.- A
server load
function can only get parent data from anotherserver load
function. - Fetch other data that is not dependent on parent data first.
- Invalidation - invalidate specific or pattern URLs in load function with
invalidate(...)
. - Custom dependencies - specify dependencies with
depends(...)
.- Useful when not using
fetch(url)
.
- Useful when not using
- invalidateAll - re-run all
load
functions withinvalidateAll()
.invalidateAll()
re-runsload
functions withouturl
dependencies.invalidate(() => true)
does not re-runload
functions withouturl
dependencies.
- General info:
static
variables are known at build time.import { API_KEY } from '$env/static/private
, use variable: `API_KEY.
dynamic
variables read values when the app runs.import { env } from '$env/dynamic/private
, use variable:env.API_KEY
.
private
variables Cannot import env variables intoclient-side code
.- Can only import env variables into server modules:
+page.server.js
+layout.server.js
+server.js
- any modules ending with
.server.js
- any modules inside
src/lib/server
- Server modules can only be imported by other server modules.
- Can only import env variables into server modules:
public
variables can be safely exposed to the browser.
- $env/static/private
- $env/dynamic/private
- $env/static/public
- $env/dynamic/public