Skip to content

Commit

Permalink
feat: pwa
Browse files Browse the repository at this point in the history
- update pwa setup to work with public_path external cdn
  • Loading branch information
denchiklut committed Jan 30, 2024
1 parent af64430 commit ee414a0
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 43 deletions.
27 changes: 0 additions & 27 deletions pwa/manifest.json

This file was deleted.

6 changes: 2 additions & 4 deletions src/client/utils/pwa/pwa.util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Workbox } from 'workbox-window'
import { basePath } from 'src/common'
import { basename, basePath } from 'src/common'

export function registerSW(promptForUpdate: () => Promise<boolean>) {
if (!('serviceWorker' in navigator)) return

const wb = new Workbox(basePath('/service-worker.js'), {
scope: '/'
})
const wb = new Workbox(basePath('/service-worker.js'), { scope: basename })

wb.addEventListener('waiting', async () => {
wb.addEventListener('controlling', () => window.location.reload())
Expand Down
1 change: 1 addition & 0 deletions src/server/controller/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './render'
export * from './health'
export * from './version'
export * from './pwa'
34 changes: 34 additions & 0 deletions src/server/controller/pwa/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Request, Response } from 'express'
import { basename, publicPath } from 'src/common'

export const getManifest = (_: Request, res: Response) => {
res.json({
short_name: 'SSR app',
name: 'SSR app',
start_url: basename,
display: 'standalone',
theme_color: '#efefef',
background_color: '#f2f4f6',
icons: [
{
src: publicPath('icons/192.png'),
sizes: '192x192'
},
{
src: publicPath('icons/512.png'),
sizes: '512x512'
},
{
src: publicPath('icons/maskable.png'),
sizes: '150x150',
type: 'image/svg+xml',
purpose: 'any'
},
{
src: publicPath('icons/maskable.png'),
type: 'image/svg+xml',
purpose: 'maskable'
}
]
})
}
2 changes: 2 additions & 0 deletions src/server/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { appRoutes } from './app'
import { staticRoutes } from './static'
import { versionRoutes } from './version'
import { healthRoutes } from './health'
import { pwaRoutes } from './pwa'

export const router = Router()
staticRoutes(router)
versionRoutes(router)
healthRoutes(router)
pwaRoutes(router)
appRoutes(router)
13 changes: 13 additions & 0 deletions src/server/router/pwa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type Router, static as staticRoute } from 'express'
import { resolve } from 'path'

import { basename, basePath } from 'src/common'
import { getManifest } from '../controller'
import { pwa } from '../middleware'

export const pwaRoutes = (router: Router) => {
if (IS_DEV) return

router.get(basePath('/manifest.json'), getManifest)
router.use(basename, pwa, staticRoute(resolve(__dirname, '../client/pwa'), { redirect: false }))
}
9 changes: 1 addition & 8 deletions src/server/router/static.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { resolve } from 'path'
import { Router, static as staticRoute } from 'express'
import { pwa } from 'server/middleware'
import { basename } from 'src/common'

export function staticRoutes(router: Router) {
const distClient = resolve(__dirname, '../client')
router.use(staticRoute(IS_DEV ? 'assets' : distClient))

if (IS_PROD) {
router.use(basename, pwa, staticRoute(resolve(distClient, 'pwa'), { redirect: false }))
}
router.use(staticRoute(IS_DEV ? 'assets' : resolve(__dirname, '../client')))
}
5 changes: 1 addition & 4 deletions webpack/plugins/copy.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import CopyPlugin from 'copy-webpack-plugin'
import { IS_PROD, ROOT_DIR } from '../utils'

const config = {
patterns: [
{ from: join(ROOT_DIR, 'assets/icons'), to: 'icons' },
{ from: join(ROOT_DIR, 'pwa/manifest.json'), to: 'pwa' }
]
patterns: [{ from: join(ROOT_DIR, 'assets/icons'), to: 'icons' }]
}

export const copyPlugin = IS_PROD && new CopyPlugin(config)

0 comments on commit ee414a0

Please sign in to comment.