Skip to content

Commit

Permalink
fix: allow PWA in netlify (anse-app#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 authored Jun 25, 2023
1 parent 889c6e9 commit df7c761
Show file tree
Hide file tree
Showing 6 changed files with 1,967 additions and 2,196 deletions.
87 changes: 42 additions & 45 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ import unocss from 'unocss/astro'
import solidJs from '@astrojs/solid-js'

import node from '@astrojs/node'
import { VitePWA } from 'vite-plugin-pwa'
import AstroPWA from '@vite-pwa/astro'
import vercel from '@astrojs/vercel/edge'
import netlify from '@astrojs/netlify/edge-functions'
import disableBlocks from './plugins/disableBlocks'

const envAdapter = () => {
if (process.env.OUTPUT === 'vercel') {
return vercel()
} else if (process.env.OUTPUT === 'netlify') {
return netlify()
} else {
return node({
mode: 'standalone',
})
switch (process.env.OUTPUT) {
case 'vercel': return vercel()
case 'netlify': return netlify()
default: return node({ mode: 'standalone' })
}
}

Expand All @@ -25,48 +21,49 @@ export default defineConfig({
integrations: [
unocss(),
solidJs(),
AstroPWA({
registerType: 'autoUpdate',
injectRegister: 'inline',
manifest: {
name: 'ChatGPT-API Demo',
short_name: 'ChatGPT Demo',
description: 'A demo repo based on OpenAI API',
theme_color: '#212129',
background_color: '#ffffff',
icons: [
{
src: 'pwa-192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: 'pwa-512.png',
sizes: '512x512',
type: 'image/png',
},
{
src: 'icon.svg',
sizes: '32x32',
type: 'image/svg',
purpose: 'any maskable',
},
],
},
client: {
installPrompt: true,
periodicSyncForUpdates: 20,
},
devOptions: {
enabled: true,
},
}),
],
output: 'server',
adapter: envAdapter(),
vite: {
plugins: [
process.env.OUTPUT === 'vercel' && disableBlocks(),
process.env.OUTPUT === 'netlify' && disableBlocks('netlify'),
process.env.OUTPUT !== 'netlify' && VitePWA({
registerType: 'autoUpdate',
manifest: {
name: 'ChatGPT-API Demo',
short_name: 'ChatGPT Demo',
description: 'A demo repo based on OpenAI API',
theme_color: '#212129',
background_color: '#ffffff',
icons: [
{
src: 'pwa-192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: 'pwa-512.png',
sizes: '512x512',
type: 'image/png',
},
{
src: 'icon.svg',
sizes: '32x32',
type: 'image/svg',
purpose: 'any maskable',
},
],
},
client: {
installPrompt: true,
periodicSyncForUpdates: 20,
},
devOptions: {
enabled: true,
},
}),
process.env.OUTPUT === 'netlify' && disableBlocks(),
],
},
})
40 changes: 18 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,31 @@
"lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx,.astro --fix"
},
"dependencies": {
"@astrojs/netlify": "2.0.0",
"@astrojs/node": "^5.0.4",
"@astrojs/solid-js": "^2.0.2",
"@astrojs/vercel": "^3.1.3",
"@unocss/reset": "^0.50.1",
"astro": "^2.0.15",
"eslint": "^8.36.0",
"eventsource-parser": "^0.1.0",
"highlight.js": "^11.7.0",
"@astrojs/netlify": "2.3.0",
"@astrojs/node": "^5.3.0",
"@astrojs/solid-js": "^2.2.0",
"@astrojs/vercel": "^3.5.0",
"astro": "^2.7.0",
"eslint": "^8.43.0",
"eventsource-parser": "^1.0.0",
"highlight.js": "^11.8.0",
"js-sha256": "^0.9.0",
"katex": "^0.6.0",
"katex": "^0.16.7",
"markdown-it": "^13.0.1",
"markdown-it-highlightjs": "^4.0.1",
"markdown-it-katex": "^2.0.3",
"solid-js": "1.6.12",
"solidjs-use": "^1.2.0",
"undici": "^5.20.0"
"solid-js": "1.7.6",
"solidjs-use": "^2.1.0",
"undici": "^5.22.1"
},
"devDependencies": {
"@iconify-json/carbon": "^1.1.16",
"@evan-yang/eslint-config": "^1.0.9",
"@iconify-json/carbon": "^1.1.18",
"@types/markdown-it": "^12.2.3",
"@typescript-eslint/parser": "^5.54.1",
"@unocss/preset-attributify": "^0.50.1",
"@unocss/preset-icons": "^0.50.4",
"@unocss/preset-typography": "^0.50.3",
"eslint-plugin-astro": "^0.24.0",
"@typescript-eslint/parser": "^5.60.0",
"@vite-pwa/astro": "^0.1.1",
"eslint-plugin-astro": "^0.27.1",
"punycode": "^2.3.0",
"unocss": "^0.50.1",
"@evan-yang/eslint-config": "^1.0.1",
"vite-plugin-pwa": "^0.14.1"
"unocss": "^0.50.8"
}
}
10 changes: 2 additions & 8 deletions plugins/disableBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
export default function plugin(platform?: string) {
export default function plugin() {
const transform = (code: string, id: string) => {
if (id.includes('pages/api/generate.ts')) {
return {
code: code.replace(/^.*?#vercel-disable-blocks([\s\S]+?)#vercel-end.*?$/gm, ''),
map: null,
}
}
if (platform === 'netlify' && id.includes('layouts/Layout.astro')) {
return {
code: code.replace(/^.*?<!-- netlify-disable-blocks -->([\s\S]+?)<!-- netlify-disable-end -->.*?$/gm, ''),
code: code.replace(/^.*?#vercel-disable-blocks([\s\S]+?)#vercel-end.*$/gm, ''),
map: null,
}
}
Expand Down
Loading

0 comments on commit df7c761

Please sign in to comment.