feat: use sveltekit pwa
This commit is contained in:
1 parent
fd29a7d1f8
commit
1ee2918031
10 files changed
+2347
-116
No files matched your search
@@ -9,3 +9,4 @@ node_modules
|
|||||||
vite.config.js.timestamp-*
|
vite.config.js.timestamp-*
|
||||||
vite.config.ts.timestamp-*
|
vite.config.ts.timestamp-*
|
||||||
stats.html
|
stats.html
|
||||||
|
dev-dist
|
||||||
+3
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "photon-lemmy",
|
"name": "photon-lemmy",
|
||||||
"version": "0.4.25",
|
"version": "0.4.26",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
@@ -18,6 +18,7 @@
|
|||||||
"@types/markdown-it": "^12.2.3",
|
"@types/markdown-it": "^12.2.3",
|
||||||
"@types/markdown-it-container": "^2.0.6",
|
"@types/markdown-it-container": "^2.0.6",
|
||||||
"@types/nprogress": "^0.2.0",
|
"@types/nprogress": "^0.2.0",
|
||||||
|
"@vite-pwa/sveltekit": "^0.2.5",
|
||||||
"autoprefixer": "^10.4.14",
|
"autoprefixer": "^10.4.14",
|
||||||
"jsdom": "^22.1.0",
|
"jsdom": "^22.1.0",
|
||||||
"markdown-it-container": "^3.0.0",
|
"markdown-it-container": "^3.0.0",
|
||||||
@@ -30,6 +31,7 @@
|
|||||||
"tslib": "^2.4.1",
|
"tslib": "^2.4.1",
|
||||||
"typescript": "^5.0.0",
|
"typescript": "^5.0.0",
|
||||||
"vite": "^4.3.0",
|
"vite": "^4.3.0",
|
||||||
|
"vite-plugin-pwa": "^0.16.4",
|
||||||
"vitest": "^0.33.0"
|
"vitest": "^0.33.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<link rel="icon" href="%sveltekit.assets%/img/logo-background.svg" />
|
<link rel="icon" href="%sveltekit.assets%/img/logo-background.svg" />
|
||||||
<link rel="manifest" crossorigin="use-credentials" href="manifest.json" />
|
<link rel="manifest" href="manifest.json" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<meta
|
<meta
|
||||||
name="description"
|
name="description"
|
||||||
|
|||||||
@@ -7,6 +7,9 @@
|
|||||||
import ToastContainer from '$lib/components/ui/toasts/ToastContainer.svelte'
|
import ToastContainer from '$lib/components/ui/toasts/ToastContainer.svelte'
|
||||||
import Moderation from '$lib/components/lemmy/moderation/Moderation.svelte'
|
import Moderation from '$lib/components/lemmy/moderation/Moderation.svelte'
|
||||||
import Sidebar from '$lib/components/ui/Sidebar.svelte'
|
import Sidebar from '$lib/components/ui/Sidebar.svelte'
|
||||||
|
import { onMount } from 'svelte'
|
||||||
|
// @ts-ignore
|
||||||
|
import { pwaInfo } from 'virtual:pwa-info'
|
||||||
|
|
||||||
nProgress.configure({
|
nProgress.configure({
|
||||||
minimum: 0.4,
|
minimum: 0.4,
|
||||||
@@ -28,6 +31,24 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
if (pwaInfo) {
|
||||||
|
const { registerSW } = await import('virtual:pwa-register')
|
||||||
|
registerSW({
|
||||||
|
immediate: true,
|
||||||
|
onRegistered(r) {
|
||||||
|
console.log(`SW Registered: ${r}`)
|
||||||
|
r?.update()
|
||||||
|
},
|
||||||
|
onRegisterError(error) {
|
||||||
|
console.log('SW registration error', error)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
$: webManifest = pwaInfo ? pwaInfo.webManifest.linkTag : ''
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex flex-col min-h-screen">
|
<div class="flex flex-col min-h-screen">
|
||||||
|
|||||||
@@ -1,86 +0,0 @@
|
|||||||
/// <reference lib="webworker" />
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
import { build, files } from '$service-worker'
|
|
||||||
|
|
||||||
const version = '0.4.25'
|
|
||||||
const timestamp = new Date().toLocaleString('en')
|
|
||||||
const worker = self as unknown as ServiceWorkerGlobalScope
|
|
||||||
const FILES = `cache${timestamp}`
|
|
||||||
|
|
||||||
// `build` is an array of all the files generated by the bundler,
|
|
||||||
// `files` is an array of everything in the `static` directory
|
|
||||||
const to_cache = build.concat(files)
|
|
||||||
const staticAssets = new Set(to_cache)
|
|
||||||
|
|
||||||
worker.addEventListener('install', (event) => {
|
|
||||||
event.waitUntil(
|
|
||||||
caches
|
|
||||||
.open(FILES)
|
|
||||||
.then((cache) => cache.addAll(to_cache))
|
|
||||||
.then(() => {
|
|
||||||
worker.skipWaiting()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
worker.addEventListener('activate', (event) => {
|
|
||||||
event.waitUntil(
|
|
||||||
caches.keys().then(async (keys) => {
|
|
||||||
// delete old caches
|
|
||||||
for (const key of keys) {
|
|
||||||
if (key !== FILES) await caches.delete(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
worker.clients.claim()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch the asset from the network and store it in the cache.
|
|
||||||
* Fall back to the cache if the user is offline.
|
|
||||||
*/
|
|
||||||
async function fetchAndCache(request: Request) {
|
|
||||||
const cache = await caches.open(`offline${timestamp}`)
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(request)
|
|
||||||
cache.put(request, response.clone())
|
|
||||||
return response
|
|
||||||
} catch (err) {
|
|
||||||
const response = await cache.match(request)
|
|
||||||
if (response) return response
|
|
||||||
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
worker.addEventListener('fetch', (event) => {
|
|
||||||
if (event.request.method !== 'GET' || event.request.headers.has('range'))
|
|
||||||
return
|
|
||||||
|
|
||||||
const url = new URL(event.request.url)
|
|
||||||
|
|
||||||
// don't try to handle e.g. data: URIs
|
|
||||||
const isHttp = url.protocol.startsWith('http')
|
|
||||||
const isDevServerRequest =
|
|
||||||
url.hostname === self.location.hostname && url.port !== self.location.port
|
|
||||||
const isStaticAsset =
|
|
||||||
url.host === self.location.host && staticAssets.has(url.pathname)
|
|
||||||
const skipBecauseUncached =
|
|
||||||
event.request.cache === 'only-if-cached' && !isStaticAsset
|
|
||||||
|
|
||||||
if (isHttp && !isDevServerRequest && !skipBecauseUncached) {
|
|
||||||
event.respondWith(
|
|
||||||
(async () => {
|
|
||||||
// always serve static files and bundler-generated assets from cache.
|
|
||||||
// if your application has other URLs with data that will never change,
|
|
||||||
// set this variable to true for them and they will only be fetched once.
|
|
||||||
const cachedAsset = isStaticAsset && (await caches.match(event.request))
|
|
||||||
|
|
||||||
return cachedAsset || fetchAndCache(event.request)
|
|
||||||
})()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -8,19 +8,17 @@
|
|||||||
"type": "image/png",
|
"type": "image/png",
|
||||||
"sizes": "512x512",
|
"sizes": "512x512",
|
||||||
"purpose": "maskable"
|
"purpose": "maskable"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"src": "logo_512.png",
|
||||||
|
"type": "image/png",
|
||||||
|
"sizes": "512x512",
|
||||||
|
"purpose": "any"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"background_color": "#000000",
|
"background_color": "#000000",
|
||||||
"display": "standalone",
|
"display": "standalone",
|
||||||
"scope": "/",
|
"scope": "/",
|
||||||
"theme_color": "#000000",
|
"theme_color": "#000000",
|
||||||
"shortcuts": [
|
|
||||||
{
|
|
||||||
"name": "Inbox",
|
|
||||||
"short_name": "Inbox",
|
|
||||||
"description": "View what's in your inbox.",
|
|
||||||
"url": "/inbox"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": "A sleek client for Lemmy."
|
"description": "A sleek client for Lemmy."
|
||||||
}
|
}
|
||||||
+8
-8
@@ -13,14 +13,14 @@ const config = {
|
|||||||
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
|
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
|
||||||
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
|
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
|
||||||
adapter: process.env.ADAPTER == 'node' ? node() : auto(),
|
adapter: process.env.ADAPTER == 'node' ? node() : auto(),
|
||||||
csp: {
|
// csp: {
|
||||||
directives: {
|
// directives: {
|
||||||
'script-src': ['self'],
|
// 'script-src': ['self'],
|
||||||
'object-src': ['none'],
|
// 'object-src': ['none'],
|
||||||
'base-uri': ['none']
|
// 'base-uri': ['none']
|
||||||
},
|
// },
|
||||||
mode: 'nonce'
|
// mode: 'nonce'
|
||||||
}
|
// }
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -8,7 +8,8 @@
|
|||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"strict": true
|
"strict": true,
|
||||||
|
"types": ["vite-plugin-pwa/client"]
|
||||||
}
|
}
|
||||||
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
|
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
|
||||||
//
|
//
|
||||||
|
|||||||
+31
-1
@@ -1,8 +1,38 @@
|
|||||||
import { sveltekit } from '@sveltejs/kit/vite'
|
import { sveltekit } from '@sveltejs/kit/vite'
|
||||||
import { defineConfig, type PluginOption } from 'vite'
|
import { defineConfig, type PluginOption } from 'vite'
|
||||||
|
import { SvelteKitPWA } from '@vite-pwa/sveltekit'
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [sveltekit()],
|
plugins: [
|
||||||
|
sveltekit(),
|
||||||
|
SvelteKitPWA({
|
||||||
|
registerType: 'autoUpdate',
|
||||||
|
strategies: 'generateSW',
|
||||||
|
workbox: {
|
||||||
|
globPatterns: ['client/**/*.{js,css,html,ico,png,svg}'],
|
||||||
|
},
|
||||||
|
devOptions: {
|
||||||
|
enabled: true,
|
||||||
|
type: 'module',
|
||||||
|
navigateFallback: '/',
|
||||||
|
},
|
||||||
|
manifest: {
|
||||||
|
name: 'Photon for Lemmy',
|
||||||
|
short_name: 'Photon',
|
||||||
|
description: 'A lemmy client with a sleek design',
|
||||||
|
theme_color: '#000000',
|
||||||
|
display: 'standalone',
|
||||||
|
icons: [
|
||||||
|
{
|
||||||
|
src: 'logo_512.png',
|
||||||
|
sizes: '512x512',
|
||||||
|
type: 'image/png',
|
||||||
|
purpose: 'masked any',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
|
||||||
define: {
|
define: {
|
||||||
__VERSION__: JSON.stringify(process.env.npm_package_version),
|
__VERSION__: JSON.stringify(process.env.npm_package_version),
|
||||||
|
|||||||
Reference in new issue
Block a user