This guide shows you how to deploy a Svelte application to Edgio.
Example
System Requirements
Sign up for Edgio
Deploying requires an account on Edgio. Sign up here for free.
Install the Edgio CLI
If you have not already done so, install the Edgio CLI.
1npm i -g @edgio/cli
Create a new Svelte app
If you don’t already have a Svelte app, create one by running the following:
1npx degit sveltejs/template-webpack svelte-app2cd svelte-app3npm install
You can verify your app works by running it locally with:
1npm run dev
Configuring your Svelte app for Edgio
Initialize your project
In the root directory of your project run edgio init
:
1edgio init
This will automatically update your package.json
and add all of the required Edgio dependencies and files to your project. These include:
- The
@edgio/core
package - Allows you to declare routes and deploy your application on Edgio - The
@edgio/prefetch
package - Allows you to configure a service worker to prefetch and cache pages to improve browsing speed edgio.config.js
- A configuration file for Edgioroutes.js
- A default routes file that sends all requests to Svelte.
Adding Edgio Service Worker
To add service worker to your Svelte app, run the following in the root folder of your project:
1npm i process register-service-worker workbox-webpack-plugin
Create service-worker.js
at the root of your project with the following:
1import {skipWaiting, clientsClaim} from 'workbox-core';2import {precacheAndRoute} from 'workbox-precaching';3import {Prefetcher} from '@edgio/prefetch/sw';45skipWaiting();6clientsClaim();7precacheAndRoute(self.__WB_MANIFEST || []);89new Prefetcher().route();
To register the service worker, first create registerServiceWorker.js
in the src
folder:
1/* eslint-disable no-console */23import {register} from 'register-service-worker';45if (process.env.NODE_ENV === 'production') {6 register(`/service-worker.js`, {7 ready() {8 console.log(9 'App is being served from cache by a service worker.\n' +10 'For more details, visit https://goo.gl/AFskqB'11 );12 },13 registered() {14 console.log('Service worker has been registered.');15 },16 cached() {17 console.log('Content has been cached for offline use.');18 },19 updatefound() {20 console.log('New content is downloading.');21 },22 updated() {23 console.log('New content is available; please refresh.');24 },25 offline() {26 console.log(27 'No internet connection found. App is running in offline mode.'28 );29 },30 error(error) {31 console.error('Error during service worker registration:', error);32 },33 });34}
and to include the service worker in the app, edit main.js
(in the src
folder) as follows:
1import './global.css';2import App from './App.svelte';3import './registerServiceWorker';45const app = new App({6 target: document.body,7 props: {8 name: 'world',9 },10});1112export default app;
Now, in webpack.config.js
make the following additions:
1const {InjectManifest} = require('workbox-webpack-plugin');2const webpack = require('webpack');34plugins: [5 new webpack.ProvidePlugin({6 process: 'process/browser',7 }),8 new InjectManifest({9 swSrc: './service-worker.js',10 }),11];
Configure the routes
Next you’ll need to configure Edgio routing in the routes.js
file.
Replace the routes.js
file that was created during edgio init
with the following:
1const {Router} = require('@edgio/core/router');23module.exports = new Router()45 // Send requests to static assets in the build output folder `public`6 .static('public')78 // Send everything else to the App Shell9 .fallback(({appShell}) => {10 appShell('public/index.html');11 });
The example above assumes you’re using Svelte as a single page app. It routes the static assets (JavaScript, CSS, and Images) in the production build folder public
and maps all other requests to the app shell in public/index.html
.
Refer to the CDN-as-code guide for the full syntax of the routes.js
file and how to configure it for your use case.
Run the Svelte app locally on Edgio
Create a production build of your app by running the following in your project’s root directory:
1npm run build
Test your app with the Sites on your local machine by running the following command in your project’s root directory:
1npm run dev
Load the site http://127.0.0.1:3000
Deploying
Create a production build of your app by running the following in your project’s root directory:
1npm run build
Deploy your app to the Sites by running the following command in your project’s root directory:
1edgio deploy
Refer to the Deployments guide for more information on the deploy
command and its options.