Skip to content

Commit 147c343

Browse files
committed
feat: add svelte ssr example
1 parent dc1c8ab commit 147c343

File tree

14 files changed

+339
-0
lines changed

14 files changed

+339
-0
lines changed

examples/svelte-ssr/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
/node_modules/
3+
/src/node_modules/@sapper/
4+
yarn-error.log
5+
/cypress/screenshots/
6+
/__sapper__/

examples/svelte-ssr/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# example-svelte-ssr
2+
3+
An example to use ByteMD in Svelte SSR with Sapper

examples/svelte-ssr/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "example-svelte-ssr",
3+
"private": true,
4+
"version": "0.0.1",
5+
"scripts": {
6+
"dev": "sapper dev",
7+
"build": "sapper build",
8+
"export": "sapper export",
9+
"start": "node __sapper__/build"
10+
},
11+
"dependencies": {
12+
"compression": "^1.7.1",
13+
"polka": "next",
14+
"sirv": "^0.4.0"
15+
},
16+
"devDependencies": {
17+
"css-loader": "^3.6.0",
18+
"file-loader": "^6.0.0",
19+
"npm-run-all": "^4.1.5",
20+
"sapper": "^0.27.0",
21+
"style-loader": "^1.2.1",
22+
"svelte": "^3.0.0",
23+
"svelte-loader": "^2.9.0",
24+
"webpack": "^4.7.0"
25+
}
26+
}

examples/svelte-ssr/src/client.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as sapper from '@sapper/app';
2+
3+
sapper.start({
4+
target: document.querySelector('#sapper'),
5+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<script>
2+
export let status;
3+
export let error;
4+
5+
const dev = process.env.NODE_ENV === 'development';
6+
</script>
7+
8+
<style>
9+
h1,
10+
p {
11+
margin: 0 auto;
12+
}
13+
14+
h1 {
15+
font-size: 2.8em;
16+
font-weight: 700;
17+
margin: 0 0 0.5em 0;
18+
}
19+
20+
p {
21+
margin: 1em auto;
22+
}
23+
24+
@media (min-width: 480px) {
25+
h1 {
26+
font-size: 4em;
27+
}
28+
}
29+
</style>
30+
31+
<svelte:head>
32+
<title>{status}</title>
33+
</svelte:head>
34+
35+
<h1>{status}</h1>
36+
37+
<p>{error.message}</p>
38+
39+
{#if dev && error.stack}
40+
<pre>{error.stack}</pre>
41+
{/if}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
3+
</script>
4+
5+
<style>
6+
7+
</style>
8+
9+
<main>
10+
<slot />
11+
</main>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import App from '../../../svelte/src/App.svelte';
3+
</script>
4+
5+
<App />

examples/svelte-ssr/src/server.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sirv from 'sirv';
2+
import polka from 'polka';
3+
import compression from 'compression';
4+
import * as sapper from '@sapper/server';
5+
6+
const { PORT, NODE_ENV } = process.env;
7+
const dev = NODE_ENV === 'development';
8+
9+
polka() // You can also use Express
10+
.use(
11+
compression({ threshold: 0 }),
12+
sirv('static', { dev }),
13+
sapper.middleware()
14+
)
15+
.listen(PORT, (err) => {
16+
if (err) console.log('error', err);
17+
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { timestamp, files, shell, routes } from '@sapper/service-worker';
2+
3+
const ASSETS = `cache${timestamp}`;
4+
5+
// `shell` is an array of all the files generated by the bundler,
6+
// `files` is an array of everything in the `static` directory
7+
const to_cache = shell.concat(files);
8+
const cached = new Set(to_cache);
9+
10+
self.addEventListener('install', (event) => {
11+
event.waitUntil(
12+
caches
13+
.open(ASSETS)
14+
.then((cache) => cache.addAll(to_cache))
15+
.then(() => {
16+
self.skipWaiting();
17+
})
18+
);
19+
});
20+
21+
self.addEventListener('activate', (event) => {
22+
event.waitUntil(
23+
caches.keys().then(async (keys) => {
24+
// delete old caches
25+
for (const key of keys) {
26+
if (key !== ASSETS) await caches.delete(key);
27+
}
28+
29+
self.clients.claim();
30+
})
31+
);
32+
});
33+
34+
self.addEventListener('fetch', (event) => {
35+
if (event.request.method !== 'GET' || event.request.headers.has('range'))
36+
return;
37+
38+
const url = new URL(event.request.url);
39+
40+
// don't try to handle e.g. data: URIs
41+
if (!url.protocol.startsWith('http')) return;
42+
43+
// ignore dev server requests
44+
if (
45+
url.hostname === self.location.hostname &&
46+
url.port !== self.location.port
47+
)
48+
return;
49+
50+
// always serve static files and bundler-generated assets from cache
51+
if (url.host === self.location.host && cached.has(url.pathname)) {
52+
event.respondWith(caches.match(event.request));
53+
return;
54+
}
55+
56+
// for pages, you might want to serve a shell `service-worker-index.html` file,
57+
// which Sapper has generated for you. It's not right for every
58+
// app, but if it's right for yours then uncomment this section
59+
/*
60+
if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) {
61+
event.respondWith(caches.match('/service-worker-index.html'));
62+
return;
63+
}
64+
*/
65+
66+
if (event.request.cache === 'only-if-cached') return;
67+
68+
// for everything else, try the network first, falling back to
69+
// cache if the user is offline. (If the pages never change, you
70+
// might prefer a cache-first approach to a network-first one.)
71+
event.respondWith(
72+
caches.open(`offline${timestamp}`).then(async (cache) => {
73+
try {
74+
const response = await fetch(event.request);
75+
cache.put(event.request, response.clone());
76+
return response;
77+
} catch (err) {
78+
const response = await cache.match(event.request);
79+
if (response) return response;
80+
81+
throw err;
82+
}
83+
})
84+
);
85+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
6+
<meta name="theme-color" content="#333333" />
7+
8+
%sapper.base%
9+
10+
<link rel="manifest" href="manifest.json" crossorigin="use-credentials" />
11+
<link rel="icon" type="image/png" href="favicon.png" />
12+
<style>
13+
body {
14+
margin: 0 10px;
15+
}
16+
</style>
17+
18+
<!-- Sapper generates a <style> tag containing critical CSS
19+
for the current page. CSS for the rest of the app is
20+
lazily loaded when it precaches secondary pages -->
21+
%sapper.styles%
22+
23+
<!-- This contains the contents of the <svelte:head> component, if
24+
the current page has one -->
25+
%sapper.head%
26+
</head>
27+
<body>
28+
<!-- The application will be rendered inside this element,
29+
because `src/client.js` references it -->
30+
<div id="sapper">%sapper.html%</div>
31+
32+
<!-- Sapper creates a <script> tag containing `src/client.js`
33+
and anything else it needs to hydrate the app and
34+
initialise the router -->
35+
%sapper.scripts%
36+
</body>
37+
</html>

0 commit comments

Comments
 (0)