Skip to content

Commit 4a613f2

Browse files
committed
Add PWA support to site
1 parent 58269b9 commit 4a613f2

7 files changed

Lines changed: 134 additions & 187 deletions

File tree

site/pages/docs/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
order: 12
3-
title: 命令行工具(内置)
3+
title: 命令行工具
44
type: 开发指南
55
---
66

site/pages/docs/cli_module.md

Lines changed: 0 additions & 181 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "Pushy 极速热更新",
3+
"short_name": "Pushy",
4+
"description": "Pushy 极速热更新",
5+
"start_url": "/",
6+
"scope": "/",
7+
"display": "standalone",
8+
"background_color": "#ffffff",
9+
"theme_color": "#0f5fff",
10+
"icons": [
11+
{
12+
"src": "/images/logo.svg",
13+
"sizes": "any",
14+
"type": "image/svg+xml",
15+
"purpose": "any maskable"
16+
}
17+
]
18+
}

site/pages/public/register-pwa.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(function () {
2+
if (!('serviceWorker' in navigator)) {
3+
return;
4+
}
5+
6+
window.addEventListener('load', function () {
7+
navigator.serviceWorker.register('/sw.js').catch(function (error) {
8+
console.warn('PWA registration failed:', error);
9+
});
10+
});
11+
})();

site/pages/public/sitemap.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
<url>
3131
<loc>https://pushy.react-native.cn/docs/cli</loc>
3232
</url>
33-
<url>
34-
<loc>https://pushy.react-native.cn/docs/cli_module</loc>
35-
</url>
3633
<url>
3734
<loc>https://pushy.react-native.cn/docs/bestpractice</loc>
3835
</url>

site/pages/public/sw.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const CACHE_NAME = 'pushy-site-v1';
2+
const PRECACHE_URLS = ['/', '/manifest.webmanifest', '/images/logo.svg'];
3+
4+
self.addEventListener('install', (event) => {
5+
event.waitUntil(
6+
caches
7+
.open(CACHE_NAME)
8+
.then((cache) => cache.addAll(PRECACHE_URLS))
9+
.then(() => self.skipWaiting()),
10+
);
11+
});
12+
13+
self.addEventListener('activate', (event) => {
14+
event.waitUntil(
15+
caches
16+
.keys()
17+
.then((keys) =>
18+
Promise.all(
19+
keys
20+
.filter((key) => key !== CACHE_NAME)
21+
.map((key) => caches.delete(key)),
22+
),
23+
)
24+
.then(() => self.clients.claim()),
25+
);
26+
});
27+
28+
self.addEventListener('fetch', (event) => {
29+
const { request } = event;
30+
31+
if (request.method !== 'GET') {
32+
return;
33+
}
34+
35+
const url = new URL(request.url);
36+
if (url.origin !== self.location.origin) {
37+
return;
38+
}
39+
40+
if (request.mode === 'navigate') {
41+
event.respondWith(
42+
fetch(request)
43+
.then((response) => {
44+
const copy = response.clone();
45+
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
46+
return response;
47+
})
48+
.catch(() => caches.match(request).then((cached) => cached || caches.match('/'))),
49+
);
50+
return;
51+
}
52+
53+
event.respondWith(
54+
caches.match(request).then((cached) => {
55+
if (cached) {
56+
return cached;
57+
}
58+
59+
return fetch(request).then((response) => {
60+
if (response.ok) {
61+
const copy = response.clone();
62+
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
63+
}
64+
return response;
65+
});
66+
}),
67+
);
68+
});

site/rspress.config.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ export default defineConfig({
4949
items: [
5050
{ text: 'API 文档', link: '/docs/api' },
5151
{ text: 'API Token', link: '/docs/api-token' },
52-
{ text: '命令行工具(内置)', link: '/docs/cli' },
53-
{ text: '命令行工具(自定义模块)', link: '/docs/cli_module' },
52+
{ text: '命令行工具', link: '/docs/cli' },
5453
{ text: '场景实践', link: '/docs/bestpractice' },
5554
],
5655
},
@@ -67,6 +66,41 @@ export default defineConfig({
6766
plugins: [pluginSass()],
6867
html: {
6968
tags: [
69+
{
70+
tag: 'link',
71+
attrs: {
72+
rel: 'manifest',
73+
href: '/manifest.webmanifest',
74+
},
75+
},
76+
{
77+
tag: 'meta',
78+
attrs: {
79+
name: 'theme-color',
80+
content: '#0f5fff',
81+
},
82+
},
83+
{
84+
tag: 'meta',
85+
attrs: {
86+
name: 'apple-mobile-web-app-capable',
87+
content: 'yes',
88+
},
89+
},
90+
{
91+
tag: 'meta',
92+
attrs: {
93+
name: 'apple-mobile-web-app-title',
94+
content: 'Pushy',
95+
},
96+
},
97+
{
98+
tag: 'script',
99+
attrs: {
100+
src: '/register-pwa.js',
101+
defer: true,
102+
},
103+
},
70104
{
71105
tag: 'meta',
72106
attrs: {

0 commit comments

Comments
 (0)