Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into userquin/feat-add-vite6-support
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Nov 29, 2024
2 parents cc935a3 + a4a9a8e commit 88b2e45
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "vite-plugin-pwa",
"type": "module",
"version": "0.21.0",
"packageManager": "pnpm@9.12.3",
"version": "0.21.1",
"packageManager": "pnpm@9.14.4",
"description": "Zero-config PWA for Vite",
"author": "antfu <[email protected]>",
"license": "MIT",
Expand Down
15 changes: 5 additions & 10 deletions src/client/build/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export function registerSW(options: RegisterSWOptions = {}) {

let wb: import('workbox-window').Workbox | undefined
let registerPromise: Promise<void>
let sendSkipWaitingMessage: () => Promise<void> | undefined
let sendSkipWaitingMessage: () => void | undefined

const updateServiceWorker = async (_reloadPage = true) => {
await registerPromise
if (!auto) {
await sendSkipWaitingMessage?.()
sendSkipWaitingMessage?.()
}
}

Expand All @@ -48,12 +48,12 @@ export function registerSW(options: RegisterSWOptions = {}) {
if (!wb)
return

sendSkipWaitingMessage = async () => {
sendSkipWaitingMessage = () => {
// Send a message to the waiting service worker,
// instructing it to activate.
// Note: for this to work, you have to add a message
// listener in your service worker. See below.
await wb?.messageSkipWaiting()
wb?.messageSkipWaiting()
}
if (!autoDestroy) {
if (auto) {
Expand Down Expand Up @@ -98,10 +98,7 @@ export function registerSW(options: RegisterSWOptions = {}) {
!onNeedRefreshCalled && onOfflineReady?.()
}
else {
if (event.isExternal)
window.location.reload()
else
!onNeedRefreshCalled && onOfflineReady?.()
!onNeedRefreshCalled && onOfflineReady?.()
}
}
else if (!event.isUpdate) {
Expand All @@ -111,8 +108,6 @@ export function registerSW(options: RegisterSWOptions = {}) {
// Add an event listener to detect when the registered
// service worker has installed but is waiting to activate.
wb.addEventListener('waiting', showSkipWaitingPrompt)
// @ts-expect-error event listener provided by workbox-window
wb.addEventListener('externalwaiting', showSkipWaitingPrompt)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/dev/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import type { RegisterSWOptions } from '../type'
export type { RegisterSWOptions }

export function registerSW(_options: RegisterSWOptions = {}) {
return (_reloadPage = true) => {}
return async (_reloadPage = true) => {}
}
28 changes: 26 additions & 2 deletions src/html.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { yellow } from 'kolorist'
import {
DEV_PWA_ASSETS_NAME,
DEV_READY_NAME,
Expand Down Expand Up @@ -25,20 +26,43 @@ navigator.serviceWorker.register('${path}', { scope: '${options.scope}' })
}`.replace(/\n/g, '')
}

export function checkForHtmlHead(html: string) {
if (!html.includes('</head>')) {
if (!html.includes('<body>')) {
console.warn([
'',
yellow('PWA WARNING:'),
'</head> and <body> tags not found in the html, the service worker and web manifest will not be injected.',
].join('\n'))
return html
}
else {
console.warn([
'',
yellow('PWA WARNING:'),
'</head> not found in the html, adding it to the html tag: add empty <head></head> to your html to remove this warning.',
].join('\n'))
}
return html.replace('<body>', `<head>\n</head>\n<body>`)
}

return html
}

export function injectServiceWorker(html: string, options: ResolvedVitePWAOptions, dev: boolean) {
const manifest = generateWebManifest(options, dev)

if (!dev) {
const script = generateRegisterSW(options, dev)
if (script) {
return html.replace(
return checkForHtmlHead(html).replace(
'</head>',
`${manifest}${script}</head>`,
)
}
}

return html.replace(
return checkForHtmlHead(html).replace(
'</head>',
`${manifest}</head>`,
)
Expand Down
5 changes: 3 additions & 2 deletions src/pwa-assets/html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { generateHtmlMarkup } from '@vite-pwa/assets-generator/api/generate-html-markup'
import type { PWAPluginContext } from '../context'
import { checkForHtmlHead } from '../html'
import type { AssetsGeneratorContext, PWAHtmlAssets } from './types'
import { mapLink } from './utils'

Expand All @@ -11,7 +12,7 @@ export function transformIndexHtml(
if (assetsGeneratorContext.injectThemeColor) {
const manifest = ctx.options.manifest
if (manifest && 'theme_color' in manifest && manifest.theme_color) {
html = html.replace(
html = checkForHtmlHead(html).replace(
'</head>',
`\n<meta name="theme-color" content="${manifest.theme_color}"></head>`,
)
Expand All @@ -21,7 +22,7 @@ export function transformIndexHtml(
if (assetsGeneratorContext.includeHtmlHeadLinks) {
const link = generateHtmlMarkup(assetsGeneratorContext.assetsInstructions)
if (link.length)
html = html.replace('</head>', `\n${link.join('\n')}</head>`)
html = checkForHtmlHead(html).replace('</head>', `\n${link.join('\n')}</head>`)
}

return html
Expand Down

0 comments on commit 88b2e45

Please sign in to comment.