Skip to content

Commit df7c761

Browse files
fix: allow PWA in netlify (#419)
1 parent 889c6e9 commit df7c761

File tree

6 files changed

+1967
-2196
lines changed

6 files changed

+1967
-2196
lines changed

astro.config.mjs

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@ import unocss from 'unocss/astro'
33
import solidJs from '@astrojs/solid-js'
44

55
import node from '@astrojs/node'
6-
import { VitePWA } from 'vite-plugin-pwa'
6+
import AstroPWA from '@vite-pwa/astro'
77
import vercel from '@astrojs/vercel/edge'
88
import netlify from '@astrojs/netlify/edge-functions'
99
import disableBlocks from './plugins/disableBlocks'
1010

1111
const envAdapter = () => {
12-
if (process.env.OUTPUT === 'vercel') {
13-
return vercel()
14-
} else if (process.env.OUTPUT === 'netlify') {
15-
return netlify()
16-
} else {
17-
return node({
18-
mode: 'standalone',
19-
})
12+
switch (process.env.OUTPUT) {
13+
case 'vercel': return vercel()
14+
case 'netlify': return netlify()
15+
default: return node({ mode: 'standalone' })
2016
}
2117
}
2218

@@ -25,48 +21,49 @@ export default defineConfig({
2521
integrations: [
2622
unocss(),
2723
solidJs(),
24+
AstroPWA({
25+
registerType: 'autoUpdate',
26+
injectRegister: 'inline',
27+
manifest: {
28+
name: 'ChatGPT-API Demo',
29+
short_name: 'ChatGPT Demo',
30+
description: 'A demo repo based on OpenAI API',
31+
theme_color: '#212129',
32+
background_color: '#ffffff',
33+
icons: [
34+
{
35+
src: 'pwa-192.png',
36+
sizes: '192x192',
37+
type: 'image/png',
38+
},
39+
{
40+
src: 'pwa-512.png',
41+
sizes: '512x512',
42+
type: 'image/png',
43+
},
44+
{
45+
src: 'icon.svg',
46+
sizes: '32x32',
47+
type: 'image/svg',
48+
purpose: 'any maskable',
49+
},
50+
],
51+
},
52+
client: {
53+
installPrompt: true,
54+
periodicSyncForUpdates: 20,
55+
},
56+
devOptions: {
57+
enabled: true,
58+
},
59+
}),
2860
],
2961
output: 'server',
3062
adapter: envAdapter(),
3163
vite: {
3264
plugins: [
3365
process.env.OUTPUT === 'vercel' && disableBlocks(),
34-
process.env.OUTPUT === 'netlify' && disableBlocks('netlify'),
35-
process.env.OUTPUT !== 'netlify' && VitePWA({
36-
registerType: 'autoUpdate',
37-
manifest: {
38-
name: 'ChatGPT-API Demo',
39-
short_name: 'ChatGPT Demo',
40-
description: 'A demo repo based on OpenAI API',
41-
theme_color: '#212129',
42-
background_color: '#ffffff',
43-
icons: [
44-
{
45-
src: 'pwa-192.png',
46-
sizes: '192x192',
47-
type: 'image/png',
48-
},
49-
{
50-
src: 'pwa-512.png',
51-
sizes: '512x512',
52-
type: 'image/png',
53-
},
54-
{
55-
src: 'icon.svg',
56-
sizes: '32x32',
57-
type: 'image/svg',
58-
purpose: 'any maskable',
59-
},
60-
],
61-
},
62-
client: {
63-
installPrompt: true,
64-
periodicSyncForUpdates: 20,
65-
},
66-
devOptions: {
67-
enabled: true,
68-
},
69-
}),
66+
process.env.OUTPUT === 'netlify' && disableBlocks(),
7067
],
7168
},
7269
})

package.json

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,31 @@
1414
"lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx,.astro --fix"
1515
},
1616
"dependencies": {
17-
"@astrojs/netlify": "2.0.0",
18-
"@astrojs/node": "^5.0.4",
19-
"@astrojs/solid-js": "^2.0.2",
20-
"@astrojs/vercel": "^3.1.3",
21-
"@unocss/reset": "^0.50.1",
22-
"astro": "^2.0.15",
23-
"eslint": "^8.36.0",
24-
"eventsource-parser": "^0.1.0",
25-
"highlight.js": "^11.7.0",
17+
"@astrojs/netlify": "2.3.0",
18+
"@astrojs/node": "^5.3.0",
19+
"@astrojs/solid-js": "^2.2.0",
20+
"@astrojs/vercel": "^3.5.0",
21+
"astro": "^2.7.0",
22+
"eslint": "^8.43.0",
23+
"eventsource-parser": "^1.0.0",
24+
"highlight.js": "^11.8.0",
2625
"js-sha256": "^0.9.0",
27-
"katex": "^0.6.0",
26+
"katex": "^0.16.7",
2827
"markdown-it": "^13.0.1",
2928
"markdown-it-highlightjs": "^4.0.1",
3029
"markdown-it-katex": "^2.0.3",
31-
"solid-js": "1.6.12",
32-
"solidjs-use": "^1.2.0",
33-
"undici": "^5.20.0"
30+
"solid-js": "1.7.6",
31+
"solidjs-use": "^2.1.0",
32+
"undici": "^5.22.1"
3433
},
3534
"devDependencies": {
36-
"@iconify-json/carbon": "^1.1.16",
35+
"@evan-yang/eslint-config": "^1.0.9",
36+
"@iconify-json/carbon": "^1.1.18",
3737
"@types/markdown-it": "^12.2.3",
38-
"@typescript-eslint/parser": "^5.54.1",
39-
"@unocss/preset-attributify": "^0.50.1",
40-
"@unocss/preset-icons": "^0.50.4",
41-
"@unocss/preset-typography": "^0.50.3",
42-
"eslint-plugin-astro": "^0.24.0",
38+
"@typescript-eslint/parser": "^5.60.0",
39+
"@vite-pwa/astro": "^0.1.1",
40+
"eslint-plugin-astro": "^0.27.1",
4341
"punycode": "^2.3.0",
44-
"unocss": "^0.50.1",
45-
"@evan-yang/eslint-config": "^1.0.1",
46-
"vite-plugin-pwa": "^0.14.1"
42+
"unocss": "^0.50.8"
4743
}
4844
}

plugins/disableBlocks.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
export default function plugin(platform?: string) {
1+
export default function plugin() {
22
const transform = (code: string, id: string) => {
33
if (id.includes('pages/api/generate.ts')) {
44
return {
5-
code: code.replace(/^.*?#vercel-disable-blocks([\s\S]+?)#vercel-end.*?$/gm, ''),
6-
map: null,
7-
}
8-
}
9-
if (platform === 'netlify' && id.includes('layouts/Layout.astro')) {
10-
return {
11-
code: code.replace(/^.*?<!-- netlify-disable-blocks -->([\s\S]+?)<!-- netlify-disable-end -->.*?$/gm, ''),
5+
code: code.replace(/^.*?#vercel-disable-blocks([\s\S]+?)#vercel-end.*$/gm, ''),
126
map: null,
137
}
148
}

0 commit comments

Comments
 (0)