Skip to content

Commit b05734d

Browse files
authored
Fix tags path build (#21)
* Fix tags path * Service worker
1 parent 9418dfa commit b05734d

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

src/lib/components/elements/Tag.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script lang="ts">
2+
import { base } from '$app/paths'
23
import { slug } from 'github-slugger'
34
45
export let text
@@ -8,7 +9,7 @@
89
</script>
910

1011
<a
11-
href={'/tags/' + url}
12+
href={base + '/tags/' + url}
1213
class="rounded-md border bg-arkGray7 px-1 text-sm font-medium hover:bg-arkGray5 {classes}"
1314
>
1415
{text}

src/service-worker.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/// <reference types="@sveltejs/kit" />
2+
/// <reference types="@sveltejs/kit" />
3+
/// <reference no-default-lib="true"/>
4+
/// <reference lib="esnext" />
5+
/// <reference lib="webworker" />
6+
7+
import { build, files, version } from '$service-worker'
8+
9+
// Create a unique cache name for this deployment
10+
const CACHE = `cache-${version}`
11+
12+
const ASSETS = [
13+
...build, // the app itself
14+
...files, // everything in `static`
15+
]
16+
17+
self.addEventListener('install', (event) => {
18+
// Create a new cache and add all files to it
19+
async function addFilesToCache() {
20+
const cache = await caches.open(CACHE)
21+
await cache.addAll(ASSETS)
22+
}
23+
24+
event.waitUntil(addFilesToCache())
25+
})
26+
27+
self.addEventListener('activate', (event) => {
28+
// Remove previous cached data from disk
29+
async function deleteOldCaches() {
30+
for (const key of await caches.keys()) {
31+
if (key !== CACHE) await caches.delete(key)
32+
}
33+
}
34+
35+
event.waitUntil(deleteOldCaches())
36+
})
37+
38+
self.addEventListener('fetch', (event) => {
39+
// ignore POST requests etc
40+
if (event.request.method !== 'GET') return
41+
42+
async function respond() {
43+
const url = new URL(event.request.url)
44+
const cache = await caches.open(CACHE)
45+
46+
// `build`/`files` can always be served from the cache
47+
if (ASSETS.includes(url.pathname)) {
48+
const response = await cache.match(url.pathname)
49+
50+
if (response) {
51+
return response
52+
}
53+
}
54+
55+
// for everything else, try the network first, but
56+
// fall back to the cache if we're offline
57+
try {
58+
const response = await fetch(event.request)
59+
60+
// if we're offline, fetch can return a value that is not a Response
61+
// instead of throwing - and we can't pass this non-Response to respondWith
62+
if (!(response instanceof Response)) {
63+
throw new Error('invalid response from fetch')
64+
}
65+
66+
if (response.status === 200) {
67+
cache.put(event.request, response.clone())
68+
}
69+
70+
return response
71+
} catch (err) {
72+
const response = await cache.match(event.request)
73+
74+
if (response) {
75+
return response
76+
}
77+
78+
// if there's no cache, then just error out
79+
// as there is nothing we can do to respond to this request
80+
throw err
81+
}
82+
}
83+
84+
event.respondWith(respond())
85+
})

0 commit comments

Comments
 (0)