This guide shows you how to deploy Sapper apps on Layer0

This Sapper example app uses server-side rendering and prefetching to provide lightening-fast transitions between pages.

This framework has a connector developed for Layer0. See Connectors for more information.

If you don't already have a Sapper app, use the terminal (or command prompt on Windows) to create one using the commands below:

# for Rollup
npx degit "sveltejs/sapper-template#rollup" my-app

# for webpack
npx degit "sveltejs/sapper-template#webpack" my-app

cd my-app
npm install
npm run dev & open http://localhost:3000

To prepare your Sapper app for deployment on Layer0, run the following in the root folder of your project:

npm install -g @layer0/cli
layer0 init

This will automatically add all of the required dependencies and files to your project. These include:

  • The @layer0/core package - Allows you to declare routes and deploy your application on Layer0
  • The @layer0/sapper package - Provides router middleware that automatically adds Sapper routes to the Layer0 router.
  • The @layer0/prefetch package - Allows you to configure a service worker to prefetch and cache pages to improve browsing speed
  • The @layer0/svelte package - Provides a Prefetch component for prefetching pages
  • layer0.config.js
  • routes.js - A default routes file that sends all requests to Sapper. Update this file to add caching or proxy some URLs to a different origin.

If you're using webpack to build your app, update webpack.config.js to bundle all dependencies in the server build:

                output: config.server.output(),
                target: 'node',
                resolve: { alias, extensions, mainFields },
-               externals: Object.keys(pkg.dependencies).concat('encoding'),
+               externals: ['encoding'],
                module: {
                        rules: [
                                {

If you're using Rollup to build your app, install @rollup/plugin-json:

npm i -D @rollup/plugin-json

Then make the following changes to rollup.config.js:

 import babel from '@rollup/plugin-babel';
 import { terser } from 'rollup-plugin-terser';
 import config from 'sapper/config/rollup.js';
-import pkg from './package.json';
+import json from '@rollup/plugin-json';

 const mode = process.env.NODE_ENV;
 const dev = mode === 'development';

... and make the following changes to the server config ...

                input: config.server.input(),
                output: config.server.output(),
                plugins: [
+                       json(),
                        replace({
                                'process.browser': false,
                                'process.env.NODE_ENV': JSON.stringify(mode)

and

-		            external: Object.keys(pkg.dependencies).concat(require('module').builtinModules),
+		            external: require('module').builtinModules,

To simulate your app within Layer0 locally, run:

layer0 dev

To simulate edge caching locally, run:

layer0 dev --cache

Deploying requires an account on Layer0. Sign up here for free. Once you have an account, you can deploy to Layer0 by running the following in the root folder of your project

layer0 deploy

See deploying for more information.

Follow these steps to add prefetching to your app:

Add the following to src/service-worker.js:

import { timestamp, files, shell, routes } from '@sapper/service-worker'

/* begin: add this to src/service-worker.js */
import { precacheAndRoute } from 'workbox-precaching'
import { Prefetcher } from '@layer0/prefetch/sw'

precacheAndRoute([])
new Prefetcher().route()
/* end: add this to src/service-worker.js */

To prefetch data when links become visible in the viewport, wrap the link in the Prefetch component from @layer0/svelte

<script>
  import { Prefetch } from '@layer0/svelte'
</script>

<Prefetch url="/blog.json">
  <a href="blog">Blog</a>
</Prefetch>

Note that the behavior of the Prefetch component is different from Sapper's built-in support for <a rel="prefetch"> in two ways:

  • rel="prefetch" only prefetches data when the user hovers over the link. The Prefetch component will prefetch data when the link becomes visible, or, if the immediately prop is present, as soon as the page loads.
  • Prefetch will only prefetch from the Layer0 edge cache, which means that additional traffic due to prefetching will never reach your API servers.

See Prefetching for more information.

Edit this guide on GitHub