A First Look at SvelteKit
Published:
SvelteKit is a serverless first Svelte metaframework for building web applications with filesystem-based routing and multiple rendering modes.
Outline
All of this project’s code can be found in the First Look monorepo on my GitHub.
Introduction
Back in July I was considering building a project with Sapper, which thankfully I never actually started. I say thankfully because, as Rich Harris announced at the recent Svelte Summit, Sapper 1.0 will be released… never.
Instead the Svelte core team is shifting its efforts to a project known as SvelteKit, as detailed in What’s the deal with SvelteKit?. It’s important to emphasize here that the people building Svelte, Sapper, and SvelteKit are all basically the same people. So really nothing drastic is changing here, it’s more of a rebrand and namespace migration. Or is it?
There is also going to be a larger focus on serverless technology, with Svelte now being referred to as a “serverless-first” framework. But to me the most significant change by far is the removal of Rollup as a development dependency and its replacement with Snowpack Vite.
SvelteKit is very new, so new it currently exists mostly in the form of the blog post linked at the beginning of this article a monorepo inside the SvelteJS GitHub organization and a website. But you can download it and start using it, albeit with many, many caveats attached to it.
Initialize Demo App
After initializing the project you’ll be given a disclaimer.
Welcome to SvelteKit!
This is release candidate software; expect bugs and missing features.
- Problems? Open an issue if none exists already.
- Stuck? Visit us at discord.com/invite/svelte.
- Install community-maintained integrations.
You’ll then be asked a series of questions to configure your application. Feel free to answer based on your own personal use case.
For the sake of simplicity in this blog post I answered no to all options.
Install Dependencies and Start Development Server
Open localhost:5173
to see the project.
Something might happen if you click the +
or -
buttons around 0
, although what that could be no one can possibly know.
Project Structure
I used to have a Snowpack joke here, unfortunately that joke is now deprecated. Now we’ll look at the code.
HTML Entry Point
src/app.html
contains our websites entry point and includes dynamic content for the head
and body
.
App Entry Point
src/routes/+page.svelte
contains our JavaScript entry point and includes a single file Svelte component. A .svelte
file contains three parts:
<script>
for JavaScript<style>
for CSS- Any markup you want to include with HTML
The <svelte:head>
element allows you to insert elements inside the <head>
of your document such as the title.
Pages
Each page of your app is a Svelte component written in a .svelte
file. Pages are created by adding files and directories to the src/routes
directory of your project. A +page.svelte
component defines a page of your app, so src/routes/+page.svelte
is the root of the site. By default, pages are rendered both on the server (SSR) for the initial request and in the browser (CSR) for subsequent navigation.
Counter Component
Unlike React, Svelte does not have a virtual DOM. Instead, Svelte includes its own system of reactivity to keep the DOM in sync with your application state. This includes responding to events, such as a mouse click. First we initialize a variable count
to a value of 0
.
We can listen to any event on an element with the on:
directive.
To test that everything is working, make a change to +page.svelte
.
Modules
The $app/environment
module provides variables for checking the app’s current environment. browser
is true
when the app is running in the browser and false
when running on the server, in development mode, or in production. dev
is true
in development mode and false
in production.
If the client-side router
is already loaded because we came here from elsewhere in the app then it is used automatically. Since there’s no dynamic data on this page, we can prerender
and serve it as a static asset in production.
Open localhost:5173/about to see this page.
Layouts
Inside src/routes
is a layout component that applies to every page. Links between pages are contained in the <Header>
component.
A <slot />
tag indicates where a component places its children, so our layout component is the parent of our pages.
Stores
Inside src/routes/Header
is a Header.svelte
file which imports page
from $app/stores
. Stores are added to the context of your root component. They are unique to each request on the server rather than shared between multiple requests handled by the same server simultaneously.
This makes it is safe to include user-specific data in page
, but it also means the stores are not free-floating objects. They must be accessed during component initialization.
page
is a readable store whose value reflects the object passed to load
functions. It contains:
host
path
params
query
The stores themselves attach to the correct context at the point of subscription so you can import and use them directly in components without boilerplate.
SvelteKit will run the page’s load
function as soon as the user hovers over the link (on desktop) or touches it (on mobile). This makes the app feel snappier since you aren’t waiting for the click
event to trigger navigation.
Config
You may have heard in the past that SvelteKit was using Snowpack. While SvelteKit was heavily developed in tandem with Snowpack, it migrated to Vite in February 2021 about a month before its beta launch. You can read Rich Harris’s reasoning for the switch on GitHub:
While we had misgivings about Vite 1 (which gave Vue apps preferential treatment, and didn’t really support SSR), Vite 2 does a really great job of solving some tricky problems that were previously in SvelteKit’s domain, like CSS code-splitting or fixing stack traces during SSR. Since it’s Rollup-based, it also means that SvelteKit apps can benefit from the very large ecosystem of Rollup plugins.
The project’s configuration can be found in svelte.config.js
.
Official Adapters for Deployment
Svelte apps are built with adapters for optimizing your project to deploy with different environments.
adapter-cloudflare-workers
for Cloudflare Workersadapter-cloudflare
for Cloudflare Pagesadapter-netlify
for Netlifyadapter-vercel
for Vercel
There are also adapters for running your app on a Node server or deploying on a static hosting provider.
adapter-node
for creating self-contained Node apps.adapter-static
for prerendering your entire site as a collection of static files.adapter-auto
for automatically choosing the adapter for your current environment if it is a supported environment.
By default, vite build
will generate a Node app that you can run with pnpm preview
. To use a different adapter, install it and update svelte.config.js
accordingly. We do not need to install the Vercel adapter unless we want to use Vercel specific features. Otherwise adapter-auto
will have everything needed for deployment.
Go to ajcwebdev-sveltekit.vercel.app
to see the deployed app.