Skip to content

Commit

Permalink
refactor: inject pwa client plugin from module (#1758)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Feb 16, 2023
1 parent 523578b commit 4364894
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composables/web-share-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function useWebShareTarget(listener?: (message: MessageEvent) => void) {

onBeforeMount(() => {
// PWA must be installed to use share target
if (useNuxtApp().$pwa.isInstalled && 'serviceWorker' in navigator) {
if (useNuxtApp().$pwa?.isInstalled && 'serviceWorker' in navigator) {
if (listener)
navigator.serviceWorker.addEventListener('message', listener)

Expand Down
17 changes: 16 additions & 1 deletion modules/pwa/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mkdir, writeFile } from 'node:fs/promises'
import { defineNuxtModule } from '@nuxt/kit'
import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
import type { VitePluginPWAAPI } from 'vite-plugin-pwa'
import { VitePWA } from 'vite-plugin-pwa'
import type { Plugin } from 'vite'
Expand All @@ -19,6 +19,8 @@ export default defineNuxtModule<VitePWANuxtOptions>({
scope: nuxt.options.app.baseURL,
}),
async setup(options, nuxt) {
const resolver = createResolver(import.meta.url)

let vitePwaClientPlugin: Plugin | undefined
const resolveVitePluginPWAAPI = (): VitePluginPWAAPI | undefined => {
return vitePwaClientPlugin?.api
Expand All @@ -35,6 +37,19 @@ export default defineNuxtModule<VitePWANuxtOptions>({
baseURL: '/',
maxAge: 0,
})
if (options.disable) {
addPlugin({ src: resolver.resolve('./runtime/pwa-plugin-stub.client') })
}
else {
// Register PWA types
nuxt.hook('prepare:types', ({ references }) => {
references.push({ types: 'vite-plugin-pwa/info' })
references.push({ types: 'vite-plugin-pwa/client' })
})
// Inject $pwa helper throughout app
addPlugin({ src: resolver.resolve('./runtime/pwa-plugin.client') })
}

// TODO: combine with configurePWAOptions?
nuxt.hook('nitro:init', (nitro) => {
options.outDir = nitro.options.output.publicDir
Expand Down
7 changes: 7 additions & 0 deletions modules/pwa/runtime/pwa-plugin-stub.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default defineNuxtPlugin(() => {
return {
provide: {
pwa: {},
},
}
})
File renamed without changes.
3 changes: 3 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ export default defineNuxtConfig({
},
sourcemap: isDevelopment,
hooks: {
'prepare:types': function ({ references }) {
references.push({ types: '@types/wicg-file-system-access' })
},
'nitro:config': function (config) {
const nuxt = useNuxt()
config.virtual = config.virtual || {}
Expand Down
2 changes: 1 addition & 1 deletion pages/share-target.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ definePageMeta({
useWebShareTarget()
const pwaIsInstalled = process.server ? false : useNuxtApp().$pwa.isInstalled
const pwaIsInstalled = process.client && !!useNuxtApp().$pwa?.isInstalled
</script>
<template>
Expand Down
4 changes: 0 additions & 4 deletions shims.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/// <reference types="@types/wicg-file-system-access" />
/// <reference types="vite-plugin-pwa/info" />
/// <reference types="vite-plugin-pwa/client" />

declare global {
namespace NodeJS {
interface Process {
Expand Down

0 comments on commit 4364894

Please sign in to comment.