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

Moving @remix-pwa/client patch upstream #238

Merged
merged 4 commits into from
May 29, 2024
Merged
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
454 changes: 405 additions & 49 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions packages/client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## @remix-pwa/client 3.0.5-dev.1 (2024-05-29)


### Bug Fixes

* **client:** added callback feature to installation prompt b17500a

## @remix-pwa/client 3.0.4 (2024-04-24)


Expand Down
54 changes: 52 additions & 2 deletions packages/client/hooks/usePWAManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { useEffect, useState } from 'react';

import { isWindowAvailable } from '../lib/user-agent.js';
Expand All @@ -7,7 +8,46 @@ type UpdateAvailable = {
newWorker: ServiceWorker | null;
};

export const usePWAManager = () => {
export type PWAManager = {
/**
* Whether an update is available or not.
*/
updateAvailable: boolean;
swUpdate: UpdateAvailable;
/**
* An asynchronous function that prompts the user to install the PWA.
*
* Takes in an optional callback that runs if the user accepts the install prompt.
*
* ## Example
*
* ```tsx
* const { promptInstall } = usePWAManager();
*
* return (
* <>
* <button onClick={promptInstall}>Install PWA</button>
* <button onClick={async() => await promptInstall(doSmthg)}>Install PWA with callback</button>
* </>
* );
* ```
*/
promptInstall: (cb?: () => void) => void;
/**
* Retrieves the current service worker registration.
*
* Returns `null` if service worker is not supported/registered.
*/
swRegistration: ServiceWorkerRegistration | null;
/**
* The user's choice on whether to install the PWA or not.
*
* Defaults to `null`.
*/
userInstallChoice: 'accepted' | 'dismissed' | null;
};

export const usePWAManager = (): PWAManager => {
const [updateAvailable, setUpdateAvailable] = useState<boolean>(false);
const [swUpdate, setSwUpdate] = useState<UpdateAvailable>({
isUpdateAvailable: false,
Expand All @@ -17,9 +57,19 @@ export const usePWAManager = () => {
const [registration, setRegistration] = useState<ServiceWorkerRegistration | null>(null);
const [userChoice, setUserChoice] = useState<'accepted' | 'dismissed' | null>(null);

const promptInstall = () => {
const promptInstall = async (cb: () => void = () => {}) => {
if (installPromptEvent) {
(installPromptEvent as any).prompt();

const { outcome: choice } = await (installPromptEvent as any).userChoice;

if (choice === 'accepted') {
cb();
setUserChoice('accepted');
} else {
setUserChoice(choice);
}

setInstallPromptEvent(null);
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remix-pwa/client",
"version": "3.0.4",
"version": "3.0.5-dev.1",
"description": "A set of utilities for client-side development to enhance the native feel of your Remix App",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"dependencies": {
"@remix-pwa/client": "3.0.4",
"@remix-pwa/push": "2.10.1",
"@remix-pwa/push": "2.10.2",
"@remix-pwa/sw": "3.0.7",
"@remix-pwa/sync": "3.0.2",
"@remix-pwa/worker-runtime": "2.1.2",
Expand Down
5 changes: 3 additions & 2 deletions playground/src/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export default function Index() {
<h1 className="text-2xl font-bold">Remix PWA - Worker Actions & Loaders</h1>
<button
type="submit"
onClick={() => {
onClick={async () => {
// alert("You're logged in!\n\nActually nothing happened 😅. Yet.");
promptInstall();
const doSMthg = () => console.log('Hehehe')
await promptInstall(doSMthg);
}}
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-amber-600 hover:bg-amber-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-amber-500"
>
Expand Down
10 changes: 5 additions & 5 deletions playground/src/app/routes/sync-away.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WorkerActionArgs } from "@remix-pwa/sw";
import { queueToServer } from "@remix-pwa/sync";
import { } from "@remix-pwa/sync";
import { redirect } from "@remix-run/node";
import type { ActionFunction } from "@remix-run/node";
import { Form, useActionData } from "@remix-run/react";
Expand Down Expand Up @@ -28,10 +28,10 @@ export const workerAction = async ({ context }: WorkerActionArgs) => {
await fetchFromServer() as unknown as Response;
} catch (error) {
console.error(error);
queueToServer({
name: 'offline-action',
request: event.request.clone(),
})
// queueToServer({
// name: 'offline-action',
// request: event.request.clone(),
// })
}

return new Response(JSON.stringify({
Expand Down