-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add prompt for update behavior #5
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
Draft
userquin
wants to merge
5
commits into
main
Choose a base branch
from
userquin/feat-add-prompt-for-update-behavior
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0089f12
feat: add prompt for update behavior
userquin 747c5e0
chore: add hint for global loadNewVersion in prompt for update module
userquin 8c8d0a5
chore: add hint for prompt configuration in prompt for update module
userquin 7cabf6f
chore: add preview for prompt script
userquin b0468dc
chore: expose prompt to prevent behavior on auto-update
userquin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ( | ||
| <div class={styles["ReloadPrompt-container"]}> | ||
| {prompt.value && ( | ||
| <div class={styles["ReloadPrompt-toast"]}> | ||
| <div class={styles["ReloadPrompt-message"]}> | ||
| <span> | ||
| New content available, click on reload button to update. | ||
| </span> | ||
| </div> | ||
| <button | ||
| class={styles["ReloadPrompt-toast-button"]} | ||
| onClick$={() => loadNewVersion()} | ||
| > | ||
| Reload | ||
| </button> | ||
| <button | ||
| class={styles["ReloadPrompt-toast-button"]} | ||
| onClick$={() => hidePrompt()} | ||
| > | ||
| Close | ||
| </button> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,25 @@ | ||
| import { initializeContext, type QwikPWAContext } from "../context"; | ||
| import type { Plugin } from "vite"; | ||
|
|
||
| const VIRTUAL_CLIENT_MODE = "virtual:qwik-pwa/client-mode"; | ||
| const RESOLVED_VIRTUAL_CLIENT_MODE = `\0${VIRTUAL_CLIENT_MODE}`; | ||
|
|
||
| export default function MainPlugin(ctx: QwikPWAContext): Plugin { | ||
| return { | ||
| name: "qwik-pwa:main", | ||
| enforce: "pre", | ||
| configResolved(config) { | ||
| initializeContext(ctx, config); | ||
| }, | ||
| resolveId(id) { | ||
| if (id === VIRTUAL_CLIENT_MODE) return RESOLVED_VIRTUAL_CLIENT_MODE; | ||
| }, | ||
| load(id) { | ||
| if (id === RESOLVED_VIRTUAL_CLIENT_MODE) { | ||
| return `export const promptForUpdate = ${ | ||
| ctx.userOptions.promptForUpdate === true | ||
| };`; | ||
| } | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { $, useOnDocument, useSignal } from "@builder.io/qwik"; | ||
| // @ts-ignore | ||
| import { promptForUpdate } from "virtual:qwik-pwa/client-mode"; | ||
|
|
||
| export function usePromptForUpdate() { | ||
| const prompt = useSignal(false); | ||
|
|
||
| useOnDocument( | ||
| "load", | ||
| $(() => { | ||
| // if using auto-update, remove the prompt logic | ||
| if (promptForUpdate === true && "serviceWorker" in navigator) { | ||
| let swr: ServiceWorkerRegistration | null; | ||
| // @ts-ignore ignore globals for now | ||
| window.loadNewVersion = () => { | ||
| console.log("loadNewVersion", { | ||
| prompt: prompt.value, | ||
| waiting: swr?.waiting, | ||
| state: swr?.waiting?.state, | ||
| }); | ||
| if (prompt.value && swr?.waiting?.state === "installed") { | ||
| swr.waiting.postMessage({ type: "SKIP_WAITING" }); | ||
| } | ||
| }; | ||
| navigator.serviceWorker.ready.then((registration) => { | ||
| swr = registration; | ||
| registration.addEventListener("updatefound", () => { | ||
| const newSW = registration.installing; | ||
| newSW?.addEventListener("statechange", () => { | ||
| console.log("newSW.state", newSW?.state); | ||
| if (newSW?.state === "activated") { | ||
| window.location.reload(); | ||
| } | ||
| if (newSW?.state === "installed") { | ||
| // New service worker is installed, but awaiting activation | ||
| prompt.value = true; | ||
| } | ||
| }); | ||
| }); | ||
| // if already installed, but still not activated | ||
| // for example, when the user cancels the prompt and then refreshes the page | ||
| if (registration.waiting?.state === "installed") { | ||
| prompt.value = true; | ||
| } | ||
| }); | ||
| } | ||
| }), | ||
| ); | ||
|
|
||
| return { prompt }; | ||
| } | ||
|
|
||
| /* can we do this here in qwik? | ||
| declare global { | ||
| interface Window { | ||
| loadNewVersion: () => void; | ||
| } | ||
| } | ||
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should provide sw precaching manifest at build time, we don't need this logic at runtime (in client plugin we have all the required data to do it).
We also need to check why
nr build + nr serveornr build-prompt + nr serveare different fromnr previewornr preview-prompt(buildcommand and/orserveis about SSG (??)?)