From 0089f12f826382067bdced2b42957373cc7b527d Mon Sep 17 00:00:00 2001 From: userquin Date: Tue, 16 Jan 2024 00:36:36 +0100 Subject: [PATCH 1/5] feat: add prompt for update behavior --- example/package.json | 1 + .../pwa-prompt/pwa-prompt.module.css | 34 +++++++++++ .../src/components/pwa-prompt/pwa-prompt.tsx | 42 ++++++++++++++ example/src/routes/layout.tsx | 2 + example/vite.config.ts | 6 +- package.json | 13 +++++ scripts/postbuild.mjs | 6 ++ src/assets/options.ts | 2 + src/plugins/client.ts | 1 + src/prompt-for-update.ts | 46 +++++++++++++++ src/sw.ts | 56 +++++++++---------- src/types.ts | 6 ++ vite.config.ts | 7 ++- 13 files changed, 191 insertions(+), 31 deletions(-) create mode 100644 example/src/components/pwa-prompt/pwa-prompt.module.css create mode 100644 example/src/components/pwa-prompt/pwa-prompt.tsx create mode 100644 src/prompt-for-update.ts diff --git a/example/package.json b/example/package.json index 3ae02e6..99c37bf 100644 --- a/example/package.json +++ b/example/package.json @@ -10,6 +10,7 @@ ], "scripts": { "build": "qwik build", + "build-prompt": "PROMPT=true qwik build", "build.client": "vite build", "build.preview": "vite build --ssr src/entry.preview.tsx", "build.server": "vite build -c adapters/cloudflare-pages/vite.config.ts", diff --git a/example/src/components/pwa-prompt/pwa-prompt.module.css b/example/src/components/pwa-prompt/pwa-prompt.module.css new file mode 100644 index 0000000..bb338c6 --- /dev/null +++ b/example/src/components/pwa-prompt/pwa-prompt.module.css @@ -0,0 +1,34 @@ +.ReloadPrompt-container { + padding: 0; + margin: 0; + width: 0; + display: block; + height: 0; +} +.ReloadPrompt-date { + visibility: hidden; +} +.ReloadPrompt-toast { + color: var(--qwik-light-blue); + position: fixed; + right: 0; + bottom: 0; + margin: 16px; + padding: 12px; + border: 1px solid #8885; + border-radius: 4px; + z-index: 1; + text-align: left; + box-shadow: 3px 4px 5px 0 #8885; + background-color: var(--qwik-dark-background); +} +.ReloadPrompt-toast-message { + margin-bottom: 8px; +} +.ReloadPrompt-toast-button { + border: 1px solid #8885; + outline: none; + margin-right: 5px; + border-radius: 2px; + padding: 3px 10px; +} diff --git a/example/src/components/pwa-prompt/pwa-prompt.tsx b/example/src/components/pwa-prompt/pwa-prompt.tsx new file mode 100644 index 0000000..391a947 --- /dev/null +++ b/example/src/components/pwa-prompt/pwa-prompt.tsx @@ -0,0 +1,42 @@ +import styles from "./pwa-prompt.module.css"; +import { $, component$ } from "@builder.io/qwik"; +import { usePromptForUpdate } from "@qwikdev/pwa/prompt-for-update"; + +export default component$(() => { + // qwik pwa prompt for update + const { prompt } = usePromptForUpdate(); + + const hidePrompt = $(() => { + prompt.value = false; + }); + const loadNewVersion = $(() => { + // @ts-ignore ignore globals for now + window.loadNewVersion(); + }); + + return ( +
+ {prompt.value && ( +
+
+ + New content available, click on reload button to update. + +
+ + +
+ )} +
+ ); +}); diff --git a/example/src/routes/layout.tsx b/example/src/routes/layout.tsx index 1ca5f36..e9b1820 100644 --- a/example/src/routes/layout.tsx +++ b/example/src/routes/layout.tsx @@ -4,6 +4,7 @@ import type { RequestHandler } from "@builder.io/qwik-city"; import Header from "~/components/starter/header/header"; import Footer from "~/components/starter/footer/footer"; +import PWAPrompt from "~/components/pwa-prompt/pwa-prompt"; import styles from "./styles.css?inline"; @@ -32,6 +33,7 @@ export default component$(() => {
+