A quick demo for rendering Svelte via Service Worker Side Rendering (SWSR). Like SSR (Server Side Rendering) but within a Service Worker!
This is a demo meant to illustrate how to get Svelte SSR in a Service Worker. It is intentionally very minimal. It is a fork of svelte-ssr-worker and lightly modified for Service Workers. Svelte and Rollup are a bit out of date in this PoC and new projects shouldn't use this as a starting point.
$ git clone https://github.com/patricknelson/svelte-ssr-service-worker
$ cd svelte-ssr-service-worker
$ npm install
The following are npm
scripts included in the project.
They are invoked via npm run <name>
on the command line; for example: npm run build:dom
.
This is an alias for sequentially running the build:dom
and build:service-worker
scripts.
Note: These are sequential because
build:service-worker
imports thepublic/index.html
thatbuild:dom
produces.
Builds the client for production, using the src/index.dom.js
entry point.
All files within the /public
directory comprise your front-end client application.
Builds your Service Worker code, using the src/index.service-worker.js
entry point. The final worker file is saved to public/build/bundle.service-worker.js
.
Important: This script must run after
build:dom
because it relies on itspublic/index.html
output.
It's loaded via the DOM code (in bundle.js
) via:
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('build/bundle.service-worker.js');
}
Starts a local development server.
This is used to preview/visit your front-end application only.
Note: This does not run your Worker code.
This is an alias for running the start
and watch:dom
scripts simultaneously.
Watches your src/index.dom.js
and its imports for changes.
Watches your src/index.service-worker.js
and its imports for changes.
You should have a storage bucket setup and attached to a CDN ahead of time.
Once the CDN address is known, you will need to update the {{CDN}}
value within config/shared.js
.
Then, after a successful build
, you will need to:
- Upload
public/*
to your storage bucket (or similar) - Upload
build/index.js
to Cloudflare Workers
Note: Cloudflare's
wrangler
can handle both of these steps!
You're done~! 🎉
MIT © Luke Edwards