Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service-workers and search params #769

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/frameworks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ You can find all the `vite-plugin-pwa` virtual modules declarations in the follo
declare module 'virtual:pwa-register' {
export interface RegisterSWOptions {
immediate?: boolean
searchQueryParams?: string | string[][] | URLSearchParams | Record<string, any>
onNeedRefresh?: () => void
onOfflineReady?: () => void
/**
Expand Down
1 change: 1 addition & 0 deletions docs/frameworks/preact.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ declare module 'virtual:pwa-register/preact' {

export interface RegisterSWOptions {
immediate?: boolean
searchQueryParams?: string | string[][] | URLSearchParams | Record<string, any>
onNeedRefresh?: () => void
onOfflineReady?: () => void
/**
Expand Down
1 change: 1 addition & 0 deletions docs/frameworks/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ declare module 'virtual:pwa-register/react' {

export interface RegisterSWOptions {
immediate?: boolean
searchQueryParams?: string | string[][] | URLSearchParams | Record<string, any>
onNeedRefresh?: () => void
onOfflineReady?: () => void
/**
Expand Down
1 change: 1 addition & 0 deletions docs/frameworks/solidjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ declare module 'virtual:pwa-register/solid' {

export interface RegisterSWOptions {
immediate?: boolean
searchQueryParams?: string | string[][] | URLSearchParams | Record<string, any>
onNeedRefresh?: () => void
onOfflineReady?: () => void
/**
Expand Down
1 change: 1 addition & 0 deletions docs/frameworks/svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ declare module 'virtual:pwa-register/svelte' {

export interface RegisterSWOptions {
immediate?: boolean
searchQueryParams?: string | string[][] | URLSearchParams | Record<string, any>
onNeedRefresh?: () => void
onOfflineReady?: () => void
/**
Expand Down
1 change: 1 addition & 0 deletions docs/frameworks/vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ declare module 'virtual:pwa-register/vue' {

export interface RegisterSWOptions {
immediate?: boolean
searchQueryParams?: string | string[][] | URLSearchParams | Record<string, any>
onNeedRefresh?: () => void
onOfflineReady?: () => void
/**
Expand Down
1 change: 1 addition & 0 deletions examples/assets-generator/src/pwa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ console.log(pwaAssetsIcons)

registerSW({
immediate: true,
searchParams: { version: '1.0' },
onNeedRefresh() {
console.log('onNeedRefresh message should not appear')
},
Expand Down
1 change: 1 addition & 0 deletions examples/preact-router/src/ReloadPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function ReloadPrompt() {
needRefresh: [needRefresh, setNeedRefresh],
updateServiceWorker,
} = useRegisterSW({
searchParams: { version: '1.0' },
onRegisteredSW(swUrl, r) {
console.log(`Service Worker at: ${swUrl}`)
// @ts-expect-error just ignore
Expand Down
1 change: 1 addition & 0 deletions examples/react-router/src/ReloadPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function ReloadPrompt() {
needRefresh: [needRefresh, setNeedRefresh],
updateServiceWorker,
} = useRegisterSW({
searchParams: { version: '1.0' },
onRegisteredSW(swUrl, r) {
console.log(`Service Worker at: ${swUrl}`)
// @ts-expect-error just ignore
Expand Down
1 change: 1 addition & 0 deletions examples/solid-router/src/ReloadPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ReloadPrompt: Component = () => {
updateServiceWorker,
} = useRegisterSW({
immediate: true,
searchParams: { version: '1.0' },
onRegisteredSW(swUrl, r) {
console.log(`Service Worker at: ${swUrl}`)
// @ts-expect-error just ignore
Expand Down
1 change: 1 addition & 0 deletions examples/svelte-routify/src/lib/ReloadPrompt.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
needRefresh,
updateServiceWorker,
} = useRegisterSW({
searchParams: { version: '1.0' },
onRegisteredSW(swUrl, r) {
// eslint-disable-next-line no-console
console.log(`Service Worker at: ${swUrl}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
needRefresh,
updateServiceWorker
} = useRegisterSW({
searchParams: { version: '1.0' },
onRegistered(r) {
if (reloadSW === 'true') {
r && setInterval(() => {
Expand Down
1 change: 1 addition & 0 deletions examples/vanilla-js-custom-sw/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ app.innerHTML = `

registerSW({
immediate: true,
searchParams: { version: '1.0' },
onNeedRefresh() {
console.log('onNeedRefresh message should not appear')
},
Expand Down
1 change: 1 addition & 0 deletions examples/vanilla-ts-no-ip/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ app.innerHTML = `

registerSW({
immediate: true,
searchParams: { version: '1.0' },
onNeedRefresh() {
console.log('onNeedRefresh message should not appear')
},
Expand Down
1 change: 1 addition & 0 deletions examples/vue-router/src/ReloadPrompt.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
updateServiceWorker,
} = useRegisterSW({
immediate: true,
searchParams: { version: '1.0' },
onRegisteredSW(swUrl, r) {
console.log(`Service Worker at: ${swUrl}`)
if (reloadSW === 'true') {
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/preact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type { RegisterSWOptions }
export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
searchParams,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -20,6 +21,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
const [updateServiceWorker] = useState(() => {
return registerSW({
immediate,
searchParams,
onOfflineReady() {
setOfflineReady(true)
onOfflineReady?.()
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type { RegisterSWOptions }
export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
searchParams,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -20,6 +21,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
const [updateServiceWorker] = useState(() => {
return registerSW({
immediate,
searchParams,
onOfflineReady() {
setOfflineReady(true)
onOfflineReady?.()
Expand Down
9 changes: 7 additions & 2 deletions src/client/build/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type { RegisterSWOptions }
export function registerSW(options: RegisterSWOptions = {}) {
const {
immediate = false,
searchParams,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -35,11 +36,15 @@ export function registerSW(options: RegisterSWOptions = {}) {
}
}

// concatenate the service-worker url and the query search params (if is not empty)
const flattenedSearchParams = new URLSearchParams(searchParams).toString();
const swScriptURL = ['__SW__', flattenedSearchParams].filter(Boolean).join("?");

async function register() {
if ('serviceWorker' in navigator) {
wb = await import('workbox-window').then(({ Workbox }) => {
// __SW__, __SCOPE__ and __TYPE__ will be replaced by virtual module
return new Workbox('__SW__', { scope: '__SCOPE__', type: '__TYPE__' })
return new Workbox(swScriptURL, { scope: '__SCOPE__', type: '__TYPE__' })
}).catch((e) => {
onRegisterError?.(e)
return undefined
Expand Down Expand Up @@ -119,7 +124,7 @@ export function registerSW(options: RegisterSWOptions = {}) {
// register the service worker
wb.register({ immediate }).then((r) => {
if (onRegisteredSW)
onRegisteredSW('__SW__', r)
onRegisteredSW(swScriptURL, r)
else
onRegistered?.(r)
}).catch((e) => {
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/solid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type { RegisterSWOptions }
export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
searchParams,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {

const updateServiceWorker = registerSW({
immediate,
searchParams,
onOfflineReady() {
setOfflineReady(true)
onOfflineReady?.()
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type { RegisterSWOptions }
export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
searchParams,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {

const updateServiceWorker = registerSW({
immediate,
searchParams,
onOfflineReady() {
offlineReady.set(true)
onOfflineReady?.()
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type { RegisterSWOptions }
export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
searchParams,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {

const updateServiceWorker = registerSW({
immediate,
searchParams,
onNeedRefresh() {
needRefresh.value = true
onNeedRefresh?.()
Expand Down
1 change: 1 addition & 0 deletions src/client/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface RegisterSWOptions {
immediate?: boolean
searchParams?: string | string[][] | URLSearchParams | Record<string, any>
onNeedRefresh?: () => void
onOfflineReady?: () => void
/**
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface RegisterSWOptions {
immediate?: boolean
searchParams?: string | string[][] | URLSearchParams | Record<string, any>
onNeedRefresh?: () => void
onOfflineReady?: () => void
/**
Expand Down