Skip to content

Commit

Permalink
feat: include manifest.webmanifest on dev (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin authored Jun 5, 2022
1 parent 858d3c2 commit b644b18
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 16 deletions.
3 changes: 2 additions & 1 deletion examples/preact-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"start-sw-reloadsw": "npm run run-build-sw-reloadsw && npm run serve",
"start-sw-claims": "npm run run-build-sw-claims && npm run serve",
"start-sw-claims-reloadsw": "npm run run-build-sw-reloadsw-claims && npm run serve",
"dev": "cross-env DEBUG=vite-plugin-pwa:* vite",
"dev": "cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true vite",
"dev-claims": "cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true CLAIMS=true SW=true vite",
"build": "rimraf dist && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true vite build",
"run-build": "rimraf dist && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true vite build",
"run-build-reloadsw": "rimraf dist && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true RELOAD_SW=true vite build",
Expand Down
6 changes: 4 additions & 2 deletions examples/preact-router/src/ReloadPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ function ReloadPrompt() {
updateServiceWorker,
} = useRegisterSW({
onRegistered(r) {
// @ts-ignore
// @ts-expect-error just ignore
if (reloadSW === 'true') {
r && setInterval(() => {
// eslint-disable-next-line no-console
console.log('Checking for sw update')
r.update()
}, 20000 /* 20s for testing purposes */)
}
else {
// eslint-disable-next-line prefer-template
// eslint-disable-next-line prefer-template,no-console
console.log('SW Registered: ' + r)
}
},
onRegisterError(error) {
// eslint-disable-next-line no-console
console.log('SW registration error', error)
},
})
Expand Down
3 changes: 2 additions & 1 deletion examples/preact-router/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"noEmit": true,
"jsx": "preserve",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment"
"jsxFragmentFactory": "Fragment",
"types": ["vite/client", "vite-plugin-pwa/client"]
},
"include": ["src"]
}
11 changes: 9 additions & 2 deletions examples/preact-router/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineConfig } from 'vite'
import preact from '@preact/preset-vite'
import { ManifestOptions, VitePWA, VitePWAOptions } from 'vite-plugin-pwa'
import { VitePWA } from 'vite-plugin-pwa'
import type { ManifestOptions, VitePWAOptions } from 'vite-plugin-pwa'
import replace from '@rollup/plugin-replace'

const pwaOptions: Partial<VitePWAOptions> = {
Expand Down Expand Up @@ -30,6 +31,12 @@ const pwaOptions: Partial<VitePWAOptions> = {
},
],
},
devOptions: {
enabled: process.env.SW_DEV === 'true',
/* when using generateSW the PWA plugin will switch to classic */
type: 'module',
navigateFallback: 'index.html',
},
}

const replaceOptions = { __DATE__: new Date().toISOString() }
Expand All @@ -48,7 +55,7 @@ if (claims)
pwaOptions.registerType = 'autoUpdate'

if (reload) {
// @ts-ignore
// @ts-expect-error just ignore
replaceOptions.__RELOAD_SW__ = 'true'
}

Expand Down
3 changes: 2 additions & 1 deletion examples/solid-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"start-sw-reloadsw": "npm run run-build-sw-reloadsw && npm run serve",
"start-sw-claims": "npm run run-build-sw-claims && npm run serve",
"start-sw-claims-reloadsw": "npm run run-build-sw-reloadsw-claims && npm run serve",
"dev": "cross-env DEBUG=vite-plugin-pwa:* vite",
"dev": "cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true vite",
"dev-claims": "cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true CLAIMS=true SW=true vite",
"build": "rimraf dist && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true vite build",
"run-build": "rimraf dist && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true vite build",
"run-build-reloadsw": "rimraf dist && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true RELOAD_SW=true vite build",
Expand Down
5 changes: 3 additions & 2 deletions examples/solid-router/src/ReloadPrompt.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable react/react-in-jsx-scope,react/no-unknown-property */
import { Component, Show } from 'solid-js'
import type { Component } from 'solid-js'
import { Show } from 'solid-js'
import { useRegisterSW } from 'virtual:pwa-register/solid'
import styles from './ReloadPrompt.module.css'

Expand All @@ -13,7 +14,7 @@ const ReloadPrompt: Component = () => {
} = useRegisterSW({
immediate: true,
onRegistered(r) {
// @ts-ignore
// @ts-expect-error just ignore
if (reloadSW === 'true') {
r && setInterval(() => {
// eslint-disable-next-line no-console
Expand Down
11 changes: 9 additions & 2 deletions examples/solid-router/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineConfig } from 'vite'
import solidPlugin from 'vite-plugin-solid'
import { ManifestOptions, VitePWA, VitePWAOptions } from 'vite-plugin-pwa'
import { VitePWA } from 'vite-plugin-pwa'
import type { ManifestOptions, VitePWAOptions } from 'vite-plugin-pwa'
import replace from '@rollup/plugin-replace'

const pwaOptions: Partial<VitePWAOptions> = {
Expand Down Expand Up @@ -30,6 +31,12 @@ const pwaOptions: Partial<VitePWAOptions> = {
},
],
},
devOptions: {
enabled: process.env.SW_DEV === 'true',
/* when using generateSW the PWA plugin will switch to classic */
type: 'module',
navigateFallback: 'index.html',
},
}

const replaceOptions = { __DATE__: new Date().toISOString() }
Expand All @@ -48,7 +55,7 @@ if (claims)
pwaOptions.registerType = 'autoUpdate'

if (reload) {
// @ts-ignore
// @ts-expect-error just ignore
replaceOptions.__RELOAD_SW__ = 'true'
}

Expand Down
3 changes: 2 additions & 1 deletion examples/svelte-routify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"start-sw-reloadsw": "npm run run-build-sw-reloadsw && npm run serve",
"start-sw-claims": "npm run run-build-sw-claims && npm run serve",
"start-sw-claims-reloadsw": "npm run run-build-sw-reloadsw-claims && npm run serve",
"dev": "cross-env DEBUG=vite-plugin-pwa:* vite",
"dev": "routify -b && cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true vite",
"dev-claims": "routify -b && cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true CLAIMS=true SW=true vite",
"build": "routify -b && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true vite build",
"run-build": "routify -b && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true vite build",
"run-build-reloadsw": "routify -b && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true RELOAD_SW=true vite build",
Expand Down
6 changes: 6 additions & 0 deletions examples/svelte-routify/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const pwaOptions = {
},
],
},
devOptions: {
enabled: process.env.SW_DEV === 'true',
/* when using generateSW the PWA plugin will switch to classic */
type: 'module',
navigateFallback: 'index.html',
},
}

const replaceOptions = { __DATE__: new Date().toISOString() }
Expand Down
3 changes: 2 additions & 1 deletion examples/sveltekit-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "svelte-kit dev",
"dev": "cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true svelte-kit dev",
"dev-claims": "cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true CLAIMS=true SW=true svelte-kit dev",
"build": "svelte-kit build",
"package": "svelte-kit package",
"preview": "svelte-kit preview",
Expand Down
8 changes: 8 additions & 0 deletions examples/sveltekit-pwa/pwa-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const pwaConfiguration = {
},
]
},
devOptions: {
enabled: process.env.SW_DEV === 'true',
/* when using generateSW the PWA plugin will switch to classic */
type: 'module',
navigateFallback: '/',
webManifestUrl: '/_app/manifest.webmanifest'
},
}

const claims = process.env.CLAIMS === 'true'
Expand All @@ -40,6 +47,7 @@ const sw = process.env.SW === 'true'
const replaceOptions = {
__DATE__: new Date().toISOString(),
__RELOAD_SW__: reload ? 'true' : 'false',
__SW_DEV__: process.env.SW_DEV === 'true' ? 'true' : 'false',
}

const workboxOrInjectManifestEntry = {
Expand Down
7 changes: 5 additions & 2 deletions examples/sveltekit-pwa/src/routes/__layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
// replaced dynamically
const date = '__DATE__'
const enableSwDev = '__SW_DEV__'
const enableManifest = (!dev && browser) || (dev && browser && enableSwDev === 'true')
let ReloadPrompt
onMount(async () => {
!dev && browser && (ReloadPrompt = (await import('$lib/components/ReloadPrompt.svelte')).default)
enableManifest && (ReloadPrompt = (await import('$lib/components/ReloadPrompt.svelte')).default)
})
</script>

<svelte:head>
{#if (!dev && browser)}
{#if enableManifest}
<link rel="manifest" href="/_app/manifest.webmanifest">
{/if}
</svelte:head>
Expand Down
10 changes: 10 additions & 0 deletions src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ export function injectServiceWorker(html: string, options: ResolvedVitePWAOption
`${manifest}</head>`,
)
}

export function injectDevManifest(html: string, options: ResolvedVitePWAOptions) {
const crossorigin = options.useCredentials ? ' crossorigin="use-credentials"' : ''
const manifest = options.manifest ? `<link rel="manifest" href="${options.base}${options.manifestFilename}"${crossorigin}>` : ''

return html.replace(
'</head>',
`${manifest}</head>`,
)
}
27 changes: 26 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve } from 'path'
import { existsSync } from 'fs'
import type { OutputBundle } from 'rollup'
import type { Plugin, ResolvedConfig } from 'vite'
import { generateSimpleSWRegister, injectServiceWorker } from './html'
import { generateSimpleSWRegister, injectDevManifest, injectServiceWorker } from './html'
import { generateInjectManifest, generateRegisterSW, generateServiceWorker } from './modules'
import type { ExtendManifestEntriesHook, ResolvedVitePWAOptions, VitePWAOptions, VitePluginPWAAPI } from './types'
import { resolveOptions } from './options'
Expand Down Expand Up @@ -141,6 +141,31 @@ export function VitePWA(userOptions: Partial<VitePWAOptions> = {}): Plugin[] {
viteConfig = config
options = await resolveOptions(userOptions, viteConfig)
},
transformIndexHtml: {
enforce: 'post',
transform(html) {
if (options.disable || !options.manifest || !options.devOptions.enabled)
return html

return injectDevManifest(html, options)
},
},
configureServer(server) {
if (!options.disable && options.manifest && options.devOptions.enabled) {
const name = options.devOptions.webManifestUrl ?? `${options.base}${options.manifestFilename}`
server.middlewares.use((req, res, next) => {
const url = req.url
if (url === name) {
res.statusCode = 200
res.write(generateWebManifestFile(options), 'utf-8')
res.end()
}
else {
next()
}
})
}
},
resolveId(id) {
return resolveDevId(id, options)
},
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,13 @@ export interface DevOptions {
* **WARNING**: this option will only be used when using `injectManifest` strategy.
*/
navigateFallback?: string

/**
* On dev mode the `manifest.webmanifest` file can be on other path.
*
* For example, **SvelteKit** will request `/_app/manifest.webmanifest`.
*
* @default `${vite.base}${pwaOptions.manifestFilename}`
*/
webManifestUrl?: string
}

0 comments on commit b644b18

Please sign in to comment.