@vite-pwa/nuxt 0.3.4 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  declare module 'virtual:nuxt-pwa-configuration' {
2
2
  export const enabled: boolean
3
+ export const display: 'fullscreen' | 'standalone' | 'minimal-ui' | 'browser'
3
4
  export const installPrompt: string | undefined
4
5
  export const periodicSyncForUpdates: number
5
6
  }
package/dist/module.json CHANGED
@@ -5,5 +5,5 @@
5
5
  "nuxt": "^3.6.5",
6
6
  "bridge": false
7
7
  },
8
- "version": "0.3.4"
8
+ "version": "0.3.6"
9
9
  }
package/dist/module.mjs CHANGED
@@ -1,10 +1,12 @@
1
1
  import { join } from 'node:path';
2
- import { writeFile, mkdir } from 'node:fs/promises';
2
+ import { lstat, writeFile, mkdir } from 'node:fs/promises';
3
3
  import { defineNuxtModule, createResolver, getNuxtVersion, addPlugin, addComponent, extendWebpackConfig } from '@nuxt/kit';
4
4
  import { VitePWA } from 'vite-plugin-pwa';
5
+ import { createHash } from 'node:crypto';
6
+ import { createReadStream } from 'node:fs';
5
7
  import { resolve } from 'pathe';
6
8
 
7
- const version = "0.3.4";
9
+ const version = "0.3.6";
8
10
 
9
11
  function configurePWAOptions(nuxt3_8, options, nuxt, nitroConfig) {
10
12
  if (!options.outDir) {
@@ -53,11 +55,11 @@ function configurePWAOptions(nuxt3_8, options, nuxt, nitroConfig) {
53
55
  config.globPatterns.push(`${appManifestFolder}**/*.json`);
54
56
  }
55
57
  if (!nuxt.options.dev && !config.manifestTransforms)
56
- config.manifestTransforms = [createManifestTransform(nuxt.options.app.baseURL ?? "/", appManifestFolder)];
58
+ config.manifestTransforms = [createManifestTransform(nuxt.options.app.baseURL ?? "/", options.outDir, appManifestFolder)];
57
59
  }
58
- function createManifestTransform(base, appManifestFolder) {
60
+ function createManifestTransform(base, publicFolder, appManifestFolder) {
59
61
  return async (entries) => {
60
- entries.filter((e) => e && e.url.endsWith(".html")).forEach((e) => {
62
+ entries.filter((e) => e.url.endsWith(".html")).forEach((e) => {
61
63
  const url = e.url.startsWith("/") ? e.url.slice(1) : e.url;
62
64
  if (url === "index.html") {
63
65
  e.url = base;
@@ -69,9 +71,32 @@ function createManifestTransform(base, appManifestFolder) {
69
71
  });
70
72
  if (appManifestFolder) {
71
73
  const regExp = /(\/)?[0-9a-f]{8}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{12}\.json$/i;
72
- entries.filter((e) => e && e.url.startsWith(appManifestFolder) && regExp.test(e.url)).forEach((e) => {
74
+ entries.filter((e) => e.url.startsWith(appManifestFolder) && regExp.test(e.url)).forEach((e) => {
73
75
  e.revision = null;
74
76
  });
77
+ const latest = `${appManifestFolder}latest.json`;
78
+ const latestJson = resolve(publicFolder, latest);
79
+ const data = await lstat(latestJson).catch(() => void 0);
80
+ if (data?.isFile()) {
81
+ const revision = await new Promise((resolve2, reject) => {
82
+ const cHash = createHash("MD5");
83
+ const stream = createReadStream(latestJson);
84
+ stream.on("error", (err) => {
85
+ reject(err);
86
+ });
87
+ stream.on("data", (chunk) => cHash.update(chunk));
88
+ stream.on("end", () => {
89
+ resolve2(cHash.digest("hex"));
90
+ });
91
+ });
92
+ const latestEntry = entries.find((e) => e.url === latest);
93
+ if (latestEntry)
94
+ latestEntry.revision = revision;
95
+ else
96
+ entries.push({ url: latest, revision, size: data.size });
97
+ } else {
98
+ entries = entries.filter((e) => e.url !== latest);
99
+ }
75
100
  }
76
101
  return { manifest: entries, warnings: [] };
77
102
  };
@@ -192,8 +217,10 @@ const module = defineNuxtModule({
192
217
  },
193
218
  load(id) {
194
219
  if (id === resolvedConfiguration) {
220
+ const display = typeof options.manifest !== "boolean" ? options.manifest?.display ?? "standalone" : "standalone";
195
221
  const installPrompt = typeof client.installPrompt === "undefined" || client.installPrompt === false ? void 0 : client.installPrompt === true || client.installPrompt.trim() === "" ? "vite-pwa:hide-install" : client.installPrompt.trim();
196
222
  return `export const enabled = ${client.registerPlugin}
223
+ export const display = '${display}'
197
224
  export const installPrompt = ${JSON.stringify(installPrompt)}
198
225
  export const periodicSyncForUpdates = ${typeof client.periodicSyncForUpdates === "number" ? client.periodicSyncForUpdates : 0}
199
226
  `;
@@ -1,6 +1,6 @@
1
1
  import { nextTick, reactive, ref } from "vue";
2
2
  import { useRegisterSW } from "virtual:pwa-register/vue";
3
- import { installPrompt, periodicSyncForUpdates } from "virtual:nuxt-pwa-configuration";
3
+ import { display, installPrompt, periodicSyncForUpdates } from "virtual:nuxt-pwa-configuration";
4
4
  import { defineNuxtPlugin } from "#imports";
5
5
  const plugin = defineNuxtPlugin(() => {
6
6
  const registrationError = ref(false);
@@ -9,8 +9,14 @@ const plugin = defineNuxtPlugin(() => {
9
9
  const hideInstall = ref(!installPrompt ? true : localStorage.getItem(installPrompt) === "true");
10
10
  const ua = navigator.userAgent;
11
11
  const ios = ua.match(/iPhone|iPad|iPod/);
12
- const standalone = window.matchMedia("(display-mode: standalone)").matches;
13
- const isInstalled = !!(standalone || ios && !ua.match(/Safari/));
12
+ const useDisplay = display === "standalone" || display === "minimal-ui" ? `${display}` : "standalone";
13
+ const standalone = window.matchMedia(`(display-mode: ${useDisplay})`).matches;
14
+ const isInstalled = ref(!!(standalone || ios && !ua.match(/Safari/)));
15
+ const isPWAInstalled = ref(isInstalled.value);
16
+ window.matchMedia(`(display-mode: ${useDisplay})`).addEventListener("change", (e) => {
17
+ if (!isPWAInstalled.value && e.matches)
18
+ isPWAInstalled.value = true;
19
+ });
14
20
  let swRegistration;
15
21
  const getSWRegistration = () => swRegistration;
16
22
  const registerPeriodicSync = (swUrl, r, timeout) => {
@@ -96,6 +102,7 @@ const plugin = defineNuxtPlugin(() => {
96
102
  provide: {
97
103
  pwa: reactive({
98
104
  isInstalled,
105
+ isPWAInstalled,
99
106
  showInstallPrompt,
100
107
  cancelInstall,
101
108
  install,
@@ -2,7 +2,11 @@ import type { Ref } from 'vue'
2
2
  import type { UnwrapNestedRefs } from 'vue'
3
3
 
4
4
  export interface PwaInjection {
5
+ /**
6
+ * @deprecated use `isPWAInstalled` instead
7
+ */
5
8
  isInstalled: boolean
9
+ isPWAInstalled: Ref<boolean>
6
10
  showInstallPrompt: Ref<boolean>
7
11
  cancelInstall: () => void
8
12
  install: () => Promise<void>
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@vite-pwa/nuxt",
3
3
  "type": "module",
4
- "version": "0.3.4",
5
- "packageManager": "pnpm@8.11.0",
4
+ "version": "0.3.6",
5
+ "packageManager": "pnpm@8.12.1",
6
6
  "description": "Zero-config PWA for Nuxt 3",
7
7
  "author": "antfu <anthonyfu117@hotmail.com>",
8
8
  "license": "MIT",