Sapper is dead! What's next in Svelte?
In case you missed it, Rich Harris gave a presentation at Svelte Summit 2020, where he announced that Sapper v1 will never be released! Instead, he showed what's coming next in Svelte itself.
Be aware that at the time of me writing this blog post, none of this is officially released yet, and very likely will change in the near future. Nonetheless, it's exciting to see a sneak preview of what the future of Svelte will look like.
Getting started
To get started today, you can run this command in your terminal, assuming you have npm
installed: npm init svelte@next
In the future, it'll probably just be npm init svelte
, which is super clean and easy to remember. This will be a nice change from having to run npx degit svelte/template my-template
.
Here's what you'll see if you run this command today:
█████████ ███████████ ███████ ███████████ ███
███░░░░░███░█░░░███░░░█ ███░░░░░███ ░░███░░░░░███░███
░███ ░░░ ░ ░███ ░ ███ ░░███ ░███ ░███░███
░░█████████ ░███ ░███ ░███ ░██████████ ░███
░░░░░░░░███ ░███ ░███ ░███ ░███░░░░░░ ░███
███ ░███ ░███ ░░███ ███ ░███ ░░░
░░█████████ █████ ░░░███████░ █████ ███
░░░░░░░░░ ░░░░░ ░░░░░░░ ░░░░░ ░░░
Pump the brakes! A little disclaimer...
svelte@next is not ready for use yet. It definitely can't
run your apps, and it might not run at all.
We haven't yet started accepting community contributions,
and we don't need people to start raising issues yet.
Given these warnings, please feel free to experiment, but
you're on your own for now. We'll have something to show
soon.
It'll go on to ask you if you want to use TypeScript, which is really nice for those who like to use TypeScript, and nice that it's optional for those who don't.
Here's the full directory structure you will get with an initial installation:
├── .gitignore
├── package.json
├── README.md
├── snowpack.config.js
├── src
│ ├── app.html
│ ├── components
│ │ └── Counter.svelte
│ └── routes
│ └── index.svelte
├── static
│ ├── favicon.ico
│ └── robots.txt
└── svelte.config.js
Starting the dev server
Once it's done setting up files, you need to run npm install
and then npm run dev
to spin up the dev server. Here's what you'll see:
snowpack
http://localhost:3001 • http://10.0.0.180:3001
Server started in 643ms.
▼ Console
[snowpack] installing dependencies...
[snowpack] ✔ install complete! [0.59s]
[snowpack]
⦿ web_modules/ size gzip brotli
├─ svelte-hmr/runtime/hot-api-esm.js 22.08 KB 7.4 KB 6.29 KB
├─ svelte-hmr/runtime/proxy-adapter-dom.js 5.17 KB 1.65 KB 1.38 KB
├─ svelte.js 0.18 KB 0.15 KB 0.11 KB
├─ svelte/internal.js 52.36 KB 13.16 KB 11.36 KB
└─ svelte/store.js 3.3 KB 1 KB 0.88 KB
[snowpack] > Listening on http://localhost:3000
What is happening under the hood? This is very different from the Svelte and Sapper templates that came before. There is no longer a rollup.config.js
nor a webpack.config.js
, because it does not use Rollup nor Webpack, at least not during development.
Instead, it uses Snowpack to handle compiling and serving client-side resources. Snowpack does not bundle your resources, and relies heavily on JavaScript’s native module system, which means development is much faster. There is even a snowpack.config.js
file which gives you a place to configure Snowpack to some degree:
// Consult https://www.snowpack.dev to learn about these options
module.exports = {
extends: '@sveltejs/snowpack-config'
};
Building your application
There is now also a new svelte.config.js
file, which lets you define an "adapter", used with npm run build
to build your application into a production web site:
module.exports = {
// By default, `npm run build` will create a standard Node app.
// You can create optimized builds for different platforms by
// specifying a different adapter
adapter: '@sveltejs/adapter-node'
};
The default adapter will use Rollup to build your site into a Node.js web server. It seems that this web server doesn't use Express.js, though that might change as well, or maybe there will be a special adapter for Express.
If you want to have a purely static export, you can currently replace @sveltejs/adapter-node
with @sveltejs/adapter-static
, but be sure to run npm install @sveltejs/adapter-static
as well.
In the future, there will be many other adapters, for example, building specifically for certain web hosting platforms, serverless architectures, and who knows what else? The cool thing about this adapter approach, is that you can build your web site without necessarily knowing how it will be built or deployed. You'll be able to change the adapter without changing your code.
Dependencies
Let's have a look at the package.json
:
{
"name": "demo",
"version": "0.0.1",
"scripts": {
"dev": "svelte dev",
"build": "svelte build"
},
"devDependencies": {
"@sveltejs/adapter-node": "0.0.12",
"@sveltejs/kit": "0.0.23",
"@sveltejs/snowpack-config": "0.0.4",
"svelte": "^3.29.0"
}
}
Note that there are very few dependencies here. I really like how minimal this is. Both of the scripts are using the new svelte
CLI from @sveltejs/kit
, though that name might change, and it's not even available on GitHub yet. For now, you can look at the npm package.
Routes
You'll notice a folder src/routes/
where you can define your routes similar to how Sapper (or Next.js, etc.) let you define routes. Basically, your folder and file structure in here will map one-to-one with the routes on your web site. This is really nice, and easy to work with, especially if you're used to using PHP or other similar web development platforms.
If you're not building a static-only web site, you can also define server-side routes, similar to what you can do with Sapper. For example, you can create a file at src/routes/api.js
:
export async function get(req) {
return {
status: 200,
body: {
hello: 'world'
}
}
}
If you're familiar with Sapper, you might notice that you have to return an object with status
and body
properties, instead of using an Express res
object for your response. This is because it is not Express middleware. It uses an internal Node web server, with an API similar to what you may have used with some serverless cloud functions.
To create a layout component, to provide a consistent header and footer wrapped around all your routes, you can create a file called $layout.svelte
, similar to Sapper's _layout.svelte
.
You can also make an error handler route called $error.svelte
, to handle 404s and other programming errors. It receives a status
prop and also an error
prop, so you can decide how to display the error to your users.
Migrating
Rich Harris notes that migrating from Sapper or other similar frameworks should be fairly straightforward, since most of the folder structure and other concepts are pretty similar. You'll probably just need to rename some files, and change how your server-side routes work, because they will no longer be written as Express middleware.
For fetching data for both server-side and client-side rendering, the Sapper approach of having a <script context="module">
block currently still works, though it's possible that will change.
Conclusion
If you're excited about all this stuff, it's definitely too early to start building your applications using it, but I'm willing to bet that it'll be a good choice to get started by using Sapper today, with the expectation that it'll be easy enough to migrate to this in the future, once it's ready.
To see a demo, check out Rich Harris' video Futuristic Web Development
If you're interested in learning more about Svelte, check out my video course The Joy of Svelte.