Menu

Introduction edit this section

Note: Sapper is in early development, and some things may change before we hit version 1.

What is Sapper?

Sapper is a framework for building extremely high-performance web apps. You're looking at one right now! There are two basic concepts:

  • Each page of your app is a Svelte component
  • You create pages by adding files to the routes directory of your project. These will be server-rendered so that a user's first visit to your app is as fast as possible, then a client-side app takes over

Building an app with all the modern best practices — code-splitting, offline support, server-rendered views with client-side hydration — is fiendishly complicated. Sapper does all the boring stuff for you so that you can get on with the creative part.

You don't need to know Svelte to understand the rest of this guide, but it will help. In short, it's a UI framework that compiles your components to highly optimized vanilla JavaScript. Read the introductory blog post and the guide to learn more.

Why the name?

In war, the soldiers who build bridges, repair roads, clear minefields and conduct demolitions — all under combat conditions — are known as sappers.

For web developers, the stakes are generally lower than for combat engineers. But we face our own hostile environment: underpowered devices, poor network connections, and the complexity inherent in front-end engineering. Sapper, which is short for S*velte *app maker, is your courageous and dutiful ally.

Comparison with Next.js

Next.js is a React framework from Zeit, and is the inspiration for Sapper. There are a few notable differences, however:

  • Sapper is powered by Svelte instead of React, so it's faster and your apps are smaller
  • Instead of route masking, we encode route parameters in filenames (see the routing section below)
  • As well as pages, you can create server routes in your routes directory. This makes it very easy to, for example, add a JSON API such as the one powering this very page (try visiting /guide.json)
  • Links are just <a> elements, rather than framework-specific <Link> components. That means, for example, that this link right here, despite being inside a blob of markdown, works with the router as you'd expect

Getting started

The easiest way to start building a Sapper app is to clone the sapper-template repo with degit:

npm install -g degit
degit sveltejs/sapper-template my-app
cd my-app
npm install
npm run dev

This will scaffold a new project in the my-app directory, install its dependencies, and start a server on localhost:3000. Try editing the files to get a feel for how everything works – you may not need to bother reading the rest of this guide!

Sapper app structure edit this section

This section is a reference for the curious. We recommend you play around with the project template first, and come back here when you've got a feel for how things fit together.

If you take a look inside the sapper-template repo, you'll see some files that Sapper expects to find:

├ package.json
├ app
│ ├ manifest
│ │ ├ # files generated by Sapper
│ ├ App.html
│ ├ client.js
│ ├ server.js
│ ├ service-worker.js
│ └ template.html
├ assets
│ ├ # your files here
├ routes
│ ├ # your routes here
│ ├ _error.html
│ └ index.html
├ webpack
│ ├ client.config.js
│ ├ server.config.js
│ └ service-worker.config.js

You'll notice a few extra files and a cypress directory which relates to testing — we don't need to worry about those right now.

You can create these files from scratch, but it's much better to use the template. See getting started for instructions on how to easily clone it

package.json

Your package.json contains your app's dependencies, including sapper, and defines a number of scripts:

  • npm run dev — start the app in development mode, and watch source files for changes
  • npm run build — build the app in production mode
  • npm run export — bake out a static version, if applicable (see exporting)
  • npm start — start the app in production mode after you've built it
  • npm test — run the tests (see testing)

app

This contains the three entry points for your app — app/client.js, app/server.js and (optionally) app/service-worker.js — along with an app/template.html file and your main app/App.html component.

app/App.html

This is the root component of your entire app. It is responsible for overall layout and for rendering individual pages:

<!-- app/App.html -->
<main>
  <svelte:component this={Page} {...props}>
</main>

<style>
  main {
    max-width: 800px;
    margin: 0 auto;
  }
</style>

app/client.js

This must import, and call, the init function from sapper/runtime.js, using your App.html and the routes object Sapper generates:

import { init } from 'sapper/runtime.js';
import { routes } from './manifest/client.js';
import App from './App.html';

init({
  target: document.querySelector('#sapper'),
  App,
  routes
});

In many cases, that's the entirety of your entry module, though you can do as much or as little here as you wish. See the runtime API section for more information on functions you can import.

app/server.js

This is a normal Express (or Polka, etc) app, with three requirements:

  • it should serve the contents of the assets folder, using for example serve-static
  • it should call app.use(sapper({ App, routes })) at the end, where routes is imported from app/manifest/server.js and App is your App.html
  • it must listen on process.env.PORT

Beyond that, you can write the server however you like.

app/service-worker.js

Service workers act as proxy servers that give you fine-grained control over how to respond to network requests. For example, when the browser requests /goats.jpg, the service worker can respond with a file it previously cached, or it can pass the request on to the server, or it could even respond with something completely different, such as a picture of llamas.

Among other things, this makes it possible to build applications that work offline.

Because every app needs a slightly different service worker (sometimes it's appropriate to always serve from the cache, sometimes that should only be a last resort in case of no connectivity), Sapper doesn't attempt to control the service worker. Instead, you write the logic in service-worker.js. You can import any of the following from app/manifest/service-worker.js:

  • assets — an array of files found in the assets directory
  • shell — the client-side JavaScript generated by webpack
  • routes — an array of { pattern: RegExp } objects you can use to determine whether a Sapper-controlled page is being requested
  • timestamp — the time the service worker was generated (useful for generating unique cache names)

app/template.html

This file is a template for responses from the server. Sapper will inject content that replaces the following tags:

  • %sapper.base% — a <base> element (see base URLs)
  • %sapper.styles% — critical CSS for the page being requested
  • %sapper.head% — HTML representing page-specific <head> contents, like <title>
  • %sapper.html% — HTML representing the body of the page being rendered
  • %sapper.scripts% — script tags for the client-side app

assets

This is a place to put any files that your app uses — fonts, images and so on. For example assets/favicon.png will be served as /favicon.png.

Sapper doesn't serve these assets — you'd typically use serve-static for that — but it will read the contents of the assets folder so that you can easily generate a cache manifest for offline support (see service-worker.js).

routes

This is the meat of your app — the pages and server routes. See the section on routing for the juicy details.

webpack

Sapper uses webpack to bundle your app. There are three config files, one for each entry point (the service worker is optional). You probably won't need to change the config, but if you want to (for example to add new loaders or plugins), you can.

Routing edit this section

As we've seen, there are two types of route in Sapper — pages, and server routes.

Pages

Pages are Svelte components written in .html files. When a user first visits the application, they will be served a server-rendered version of the route in question, plus some JavaScript that 'hydrates' the page and initialises a client-side router. From that point forward, navigating to other pages is handled entirely on the client for a fast, app-like feel.

The filename determines the route. For example, routes/index.html is the root of your site:

<!-- routes/index.html -->
<svelte:head>
  <title>Welcome</title>
</svelte:head>

<h1>Hello and welcome to my site!</h1>

A file called either routes/about.html or routes/about/index.html would correspond to the /about route:

<!-- routes/about.html -->
<svelte:head>
  <title>About</title>
</svelte:head>

<h1>About this site</h1>
<p>TODO...</p>

Dynamic parameters are encoded using [brackets]. For example, here's how you could create a page that renders a blog post:

<!-- routes/blog/[slug].html -->
<svelte:head>
  <title>{post.title}</title>
</svelte:head>

<h1>{post.title}</h1>

<div class='content'>
  {@html post.html}
</div>

<script>
  export default {
    // the (optional) preload function takes a
    // `{ params, query }` object and turns it into
    // the data we need to render the page
    preload({ params, query }) {
      // the `slug` parameter is available because this file
      // is called [slug].html
      const { slug } = params;

      return this.fetch(`blog/${slug}.json`).then(r => r.json()).then(post => {
        return { post };
      });
    }
  };
</script>

See the section on preloading for more info about preload and this.fetch

Server routes

Server routes are modules written in .js files that export functions corresponding to HTTP methods. Each function receives Express request and response objects as arguments, plus a next function. This is useful for creating a JSON API. For example, here's how you could create an endpoint that served the blog page above:

// routes/blog/[slug].json.js
import db from './_database.js'; // the underscore tells Sapper this isn't a route

export async function get(req, res, next) {
  // the `slug` parameter is available because this file
  // is called [slug].json.js
  const { slug } = req.params;

  const post = await db.get(slug);

  if (post !== null) {
    res.set('Content-Type', 'application/json');
    res.end(JSON.stringify(post));
  } else {
    next();
  }
}

delete is a reserved word in JavaScript. To handle DELETE requests, export a function called del instead.

There are three simple rules for naming the files that define your routes:

  • A file called routes/about.html corresponds to the /about route. A file called routes/blog/[slug].html corresponds to the /blog/:slug route, in which case params.slug is available to preload
  • The file routes/index.html (or routes/index.js) corresponds to the root of your app. routes/about/index.html is treated the same as routes/about.html.
  • Files and directories with a leading underscore do not create routes. This allows you to colocate helper modules and components with the routes that depend on them — for example you could have a file called routes/_helpers/datetime.js and it would not create a /_helpers/datetime route

Subroutes

Suppose you have a route called /settings and a series of subroutes such as /settings/profile and /settings/notifications.

You could do this with two separate pages — routes/settings.html (or routes/settings/index.html) and routes/settings/[submenu].html, but it's likely that they'll share a lot of code. If there's no routes/settings.html file, then routes/settings/[submenu].html will match /settings as well as subroutes like /settings/profile, but the value of params.submenu will be undefined in the first case.

Error page

In addition to regular pages, there is a 'special' page that Sapper expects to find — routes/_error.html. This will be shown when an error occurs while rendering a page.

The error object is made available to the template along with the HTTP status code.

Regexes in routes

You can use a subset of regular expressions to qualify route parameters, by placing them in parentheses after the parameter name.

For example, routes/items/[id([0-9]+)].html would only match numeric IDs — /items/123 would match, but /items/xyz would not.

Because of technical limitations, the following characters cannot be used: /, \, ?, :, ( and ).

Runtime API edit this section

The sapper/runtime.js module contains functions for controlling your app and responding to events. More functions will be added in future versions.

init(target, routes)

  • target — an element to render pages to
  • routes — an array of route objects generated by Sapper

This configures the router and starts the application — listens for clicks on <a> elements, interacts with the history API, and renders and updates your Svelte components.

Returns a Promise that resolves when the initial page has been hydrated.

import { init } from 'sapper/runtime.js';
import { routes } from './manifest/client.js';

init(document.querySelector('#sapper'), routes).then(() => {
  console.log('client-side app has started');
});

goto(href, [options])

  • href — the page to go to
  • options — can include a replaceState property, which determines whether to use history.pushState (the default) or history.replaceState). Not required

Programmatically navigates to the given href. If the destination is a Sapper route, Sapper will handle the navigation, otherwise the page will be reloaded with the new href. (In other words, the behaviour is as though the user clicked on a link with this href.)

prefetch(href)

  • href — the page to prefetch

Programmatically prefetches the given page, which means a) ensuring that the code for the page is loaded, and b) calling the page's preload method with the appropriate options. This is the same behaviour that Sapper triggers when the user taps or mouses over an <a> element with rel=prefetch.

prefetchRoutes([routes])

  • routes — an optional array of strings representing routes to prefetch

Programmatically prefetches the code for routes that haven't yet been fetched. Typically, you might call this after init is complete, to speed up subsequent navigation. Omitting arguments will cause all routes to be fetched, or you can specify routes by any matching pathname such as /about (to match routes/about.html) or /blog/* (to match routes/blog/[slug].html). Unlike prefetch, this won't call preload for individual pages.

Preloading edit this section

As seen in the routing section, top-level page components can have a preload function that will load some data that the page depends on. This is similar to getInitialProps in Next.js or asyncData in Nuxt.js.

<script>
  export default {
    preload({ params, query }) {
      const { slug } = params;

      return this.fetch(`blog/${slug}.json`).then(r => r.json()).then(post => {
        return { post };
      });
    }
  };
</script>

Your preload function is optional; whether or not you include it, the component will have access to the query and params objects, on top of any default data specified with a data property.

Argument

When preload runs in the browser, the argument is a { params, query } object where params is derived from the URL and the route filename, and query is an object of values in the query string.

So if the example above was routes/blog/[slug].html and the URL was /blog/some-post?foo=bar&baz, the following would be true:

  • params.slug === 'some-post'
  • query.foo === 'bar'
  • query.baz === true

When preload runs on the server, the argument is the entire Request object, which includes params and query. This is useful if you're using session middleware such as express-session as it allows you to render a page for a specific user, for example:

<script>
  import * as auth from './_auth.js'; // handles client-side auth

  export default {
    preload({ params, session }) {
      // on the server, use the session object
      // on the client, maintain auth state separately
      const user = session ? session.user : auth.user;

      return {
        user
      };
    }
  };
</script>

Return value

If you return a Promise from preload, the page will delay rendering until the promise resolves. You can also return a plain object.

When Sapper renders a page on the server, it will attempt to serialize the resolved value (using devalue) and include it on the page, so that the client doesn't also need to call preload upon initialization. Serialization will fail if the value includes functions or custom classes (cyclical and repeated references are fine, as are built-ins like Date, Map, Set and RegExp).

Context

Inside preload, you have access to three methods...

  • this.fetch(url, options)
  • this.error(statusCode, error)
  • this.redirect(statusCode, location)

...and this.store, if you're using state management.

this.fetch

In browsers, you can use fetch to make AJAX requests, for getting data from your server routes (among other things). On the server it's a little trickier — you can make HTTP requests, but you must specify an origin, and you don't have access to cookies. This means that it's impossible to request data based on the user's session, such as data that requires you to be logged in.

To fix this, Sapper provides this.fetch, which works on the server as well as in the client:

<script>
  export default {
    preload() {
      return this.fetch(`secret-data.json`, {
        credentials: 'include'
      }).then(r => {
        // ...
      });
    }
  };
</script>

Note that you will need to use session middleware such as express-session in your app/server.js in order to maintain user sessions or do anything involving authentication.

this.error

If the user navigated to /blog/some-invalid-slug, we would want to render a 404 Not Found page. We can do that with this.error:

<script>
  export default {
    preload({ params, query }) {
      const { slug } = params;

      return this.fetch(`blog/${slug}.json`).then(r => {
        // assume all responses are either 200 or 404
        if (r.status === 200) {
          return r.json().then(post => {
            return { post };
          });
        } else {
          this.error(404, 'Not found');
        }
      });
    }
  };
</script>

The same applies to other error codes you might encounter.

this.redirect

You can abort rendering and redirect to a different location with this.redirect:

<script>
  export default {
    preload({ params, session }) {
      const user = this.store.get('user');

      if (!user) {
        return this.redirect(302, 'login');
      }

      return {
        user
      };
    }
  };
</script>

Prefetching edit this section

Sapper uses code splitting to break your app into small chunks (one per route), ensuring fast startup times. After the first route renders, it will fetch the code for subsequent routes, so that they're already ready when the user wants to navigate to them.

For dynamic routes, such as our routes/blog/[slug].html example, that's not enough. In order to render the blog post, we need to fetch the data for it, and we can't do that until we know what slug is. In the worst case, that could cause lag as the browser waits for the data to come back from the server.

rel=prefetch

We can mitigate that by prefetching the data. Adding a rel=prefetch attribute to a link...

<a rel=prefetch href='blog/what-is-sapper'>What is Sapper?</a>

...will cause Sapper to run the page's preload function as soon as the user hovers over the link (on a desktop) or touches it (on mobile), rather than waiting for the click event to trigger navigation. Typically, this buys us an extra couple of hundred milliseconds, which is the difference between a user interface that feels laggy, and one that feels snappy.

rel=prefetch is a Sapper idiom, not a standard attribute for <a> elements

State management edit this section

Sapper integrates with the built-in Svelte store. If you're not familiar with Store, read the Svelte state management guide before continuing with this section.

To use Store, you must integrate it with your server and client apps separately.

On the server

Whereas the client-side app has a single Store instance that lasts as long as the page is open, the server-side app must create a new store for each request:

// app/server.js
const { Store } = require('svelte/store.js');

express() // or Polka, or a similar framework
  .use(
    compression({ threshold: 0 }),
    serve('assets'),
    authenticationMiddleware(),
    sapper({
      routes,
      store: request => {
        return new Store({
          user: request.user
        });
      }
    })
  )
  .listen(process.env.PORT);

In this example, we're using some imaginary authenticationMiddleware that creates a request.user object based on the user's cookies. (In real life it tends to be a bit more involved — see express-session and Passport if you're ready to learn more about sessions and authentication.)

Because we've supplied a store option, Sapper creates a new Store instance for each new request. The data in our store will be used to render the HTML that Sapper responds with.

On the client

This time around, we're creating a single store that is attached to each page as the user navigates around the app.

import { init } from 'sapper/runtime.js';
import { Store } from 'svelte/store.js';
import { routes } from './manifest/client.js';

init(document.querySelector('#sapper'), routes, {
  store: data => {
    // `data` is whatever was in the server-side store
    return new Store(data);
  }
});

In order to re-use the server-side store data, it must be serializable (using devalue) — no functions or custom classes, just built-in JavaScript data types

Deployment edit this section

Sapper apps run anywhere that supports Node 8 or higher.

Deploying to Now

We can very easily deploy our apps to Now:

npm install -g now
now

This will upload the source code to Now, whereupon it will do npm run build and npm start and give you a URL for the deployed app.

For other hosting environments, you may need to do npm run build yourself.

Exporting edit this section

Many sites are effectively static, which is to say they don't actually need an Express server backing them. Instead, they can be hosted and served as static files, which allows them to be deployed to more hosting environments (such as Netlify or GitHub Pages). Static sites are generally cheaper to operate and have better performance characteristics.

Sapper allows you to export a static site with a single zero-config sapper export command. In fact, you're looking at an exported site right now!

Static doesn't mean non-interactive — your Svelte components work exactly as they do normally, and you still get all the benefits of client-side routing and prefetching.

This is an experimental feature — if you encounter unexpected behaviour or feel that there are missing features, please raise an issue!

sapper export

Inside your Sapper project, try this:

# npx allows you to use locally-installed dependencies
npx sapper export

This will create an export folder with a production-ready build of your site. You can launch it like so:

npx serve export

Navigate to localhost:5000 (or whatever port serve picked), and verify that your site works as expected.

You can also add a script to your package.json...

{
  "scripts": {
    ...
    "build": "sapper export"
  }
}

...allowing you to npm run build your app.

How it works

When you run sapper export, Sapper first builds a production version of your app, as though you had run sapper build, and copies the contents of your assets folder to the destination. It then starts the server, and navigates to the root of your app. From there, it follows any <a> elements it finds, and captures any data served by the app.

Because of this, any pages you want to be included in the exported site must be reachable by <a> elements. Additionally, any non-page routes should be requested in preload, not in oncreate or elsewhere.

When not to export

The basic rule is this: for an app to be exportable, any two users hitting the same page of your app must get the same content from the server. In other words, any app that involves user sessions or authentication is not a candidate for sapper export.

Note that you can still export apps with dynamic routes, like our routes/blog/[slug].html example from earlier. sapper export will intercept fetch requests made inside preload, so the data served from routes/blog/[slug].json.js will also be captured.

Route conflicts

Because sapper export writes to the filesystem, it isn't possible to have two server routes that would cause a directory and a file to have the same name. For example, routes/foo/index.js and routes/foo/bar.js would try to create export/foo and export/foo/bar, which is impossible.

The solution is to rename one of the routes to avoid conflict — for example, routes/foo-bar.js. (Note that you would also need to update any code that fetches data from /foo/bar to reference /foo-bar instead.)

For pages, we skirt around this problem by writing export/foo/index.html instead of export/foo.

Base URLs edit this section

Ordinarily, the root of your Sapper app is served at /. But in some cases, your app may need to be served from a different base path — for example, if Sapper only controls part of your domain, or if you have multiple Sapper apps living side-by-side.

This can be done like so:

// app/server.js

express() // or Polka, or a similar framework
  .use(
    '/my-base-path', // <!-- add this line
    compression({ threshold: 0 }),
    serve('assets'),
    sapper({ routes })
  )
  .listen(process.env.PORT);

Sapper will detect the base path and configure both the server-side and client-side routers accordingly.

If you're exporting your app, you will need to tell the exporter where to begin crawling:

sapper export --basepath my-base-path

Testing edit this section

You can use whatever testing frameworks and libraries you'd like. The default in sapper-template is Cypress.

Running the tests

npm test

This will start the server and open Cypress. You can (and should!) add your own tests in cypress/integration/spec.js — consult the docs for more information.

Migrating edit this section

Until we reach version 1.0, there may be occasional changes to the project structure Sapper expects. If you upgrade and Sapper sends you here, it's because it was unable to fix your project automatically.

0.6 to 0.7

Consult sapper-template for full examples of all the below points.

package.json

To start a dev server, use sapper dev rather than node server.js. In all likelihood, your package.json will have an npm run dev script that will need to be updated.

Entry points

As of version 0.7, Sapper expects to find your entry points — for client, server and service worker — in an app folder. Instead of using magically-injected __variables__, each entry point imports from its corresponding file in the app/manifests folder. These are automatically generated by Sapper.

// app/client.js (formerly templates/main.js)
import { init } from 'sapper/runtime.js';
import { routes } from './manifest/client.js';

init(document.querySelector('#sapper'), routes);

if (module.hot) module.hot.accept(); // enable hot reloading
// app/server.js (formerly server.js)
// Note that we're now using ES module syntax, because this
// file is processed by webpack like the rest of your app
import sapper from 'sapper';
import { routes } from './manifest/server.js';
// ..other imports

// we now pass the `routes` object to the Sapper middleware
app.use(sapper({
  routes
}));
// app/service-worker.js (formerly templates/service-worker.js)
import { assets, shell, timestamp, routes } from './manifest/service-worker.js';

// replace e.g. `__assets__` with `assets` in the rest of the file

Templates and error pages

In previous versions, we had templates/2xx.html, templates/4xx.html and templates/5xx.html. Now, we have a single template, app/template.html, which should look like your old templates/2xx.html.

For handling error states, we have a 'special' route: routes/_error.html.

This page is just like any other, except that it will get rendered whenever an error states is reached. The component has access to status and error values.

Note that you can now use this.error(statusCode, error) inside your preload functions.

Webpack configs

Your webpack configs now live in a webpack directory:

  • webpack.client.config.js is now webpack/client.config.js
  • webpack.server.config.js is now webpack/server.config.js

If you have a service worker, you should also have a webpack/service-worker.config.js file. See sapper-template for an example.

<0.9 to 0.10

app/template.html

  • Your <head> element must contain %sapper.base% (see (base URLs)
  • Remove references to your service worker; this is now handled by %sapper.scripts%

Pages

  • Your preload functions should now use this.fetch instead of fetch. this.fetch allows you to make credentialled requests on the server, and means that you no longer need to create a global.fetch object in app/server.js.

0.11 to 0.12

In earlier versions, each page was a completely standalone component. Upon navigation, the entire page would be torn down and a new one created. Typically, each page would import a shared <Layout> component to achieve visual consistency.

As of 0.12, this changes: we have a single <App> component, defined in app/App.html, which controls the rendering of the rest of the app. See sapper-template for an example.

This component is rendered with the following values:

  • Page — a component constructor for the current page
  • props — an object with params, query, and any data returned from the page's preload function
  • preloadingtrue during preload, false otherwise. Useful for showing progress indicators

Sapper needs to know about your app component. To that end, you will need to modify your app/server.js and app/client.js:

// app/server.js
import polka from 'polka';
import sapper from 'sapper';
import serve from 'serve-static';
import { routes } from './manifest/server.js';
+import App from './App.html';

polka()
  .use(
    serve('assets'),
-		sapper({ routes })
+		sapper({ App, routes })
  )
  .listen(process.env.PORT);
// app/client.js
import { init } from 'sapper/runtime.js';
import { routes } from './manifest/client.js';
+import App from './App.html';

-init(target: document.querySelector('#sapper'), routes);
+init({
+	target: document.querySelector('#sapper'),
+	routes,
+	App
+});

Once your App.html has been created and your server and client apps updated, you can remove any <Layout> components from your individual pages.

0.13 to 0.14

The 4xx.html and 5xx.html error pages have been replaced with a single page, _error.html. In addition to the regular params, query and path props, it receives status and error.