Skip to content

Commit

Permalink
Merge pull request #255 from martin-wahlberg/fix_hydration_errors_for…
Browse files Browse the repository at this point in the history
…_useNetworkConnectivity_hook

fix: stop useNetworkConnectivity from causing hydration errors.
  • Loading branch information
ShafSpecs authored Aug 23, 2024
2 parents fb90711 + 9cf2978 commit dec144d
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 28 deletions.
7 changes: 7 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## @remix-pwa/cli 1.2.5 (2024-08-11)


### Bug Fixes

* **cli:** added new `--dev` flag to the `upgrade` command - for updating to latest `dev` candidate d4f4414

## @remix-pwa/cli 1.2.5-dev.1 (2024-08-11)


Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remix-pwa/cli",
"version": "1.2.5-dev.1",
"version": "1.2.5",
"description": "An elegant CLI for everything Remix PWA 💖",
"repository": {
"type": "git",
Expand Down
45 changes: 23 additions & 22 deletions packages/client/hooks/useNetworkConnectivity.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
/* eslint-disable n/no-callback-literal */
import { useEffect, useState } from 'react';
import { useEffect, useSyncExternalStore } from 'react';

import { isWindowAvailable } from '../lib/user-agent.js';
const subscribeToNetworkConnectivity = (callback: () => void) => {
window.addEventListener('online', callback);
window.addEventListener('offline', callback);
return () => {
window.removeEventListener('online', callback);
window.removeEventListener('offline', callback);
};
};

const getNetworkConnectivitySnapshot = () => window.navigator.onLine;

const getNetworkConnectivityServerSnapshot = () => false;

export const useNetworkConnectivity = (
options: {
onOnline?: (isOnline: boolean) => void;
onOffline?: (isOnline: boolean) => void;
} = {}
) => {
const [isOnline, setIsOnline] = useState(isWindowAvailable() ? navigator.onLine : false);

const handleOnline = () => {
setIsOnline(true);
options.onOnline && options.onOnline(true);
};

const handleOffline = () => {
setIsOnline(false);
options.onOffline && options.onOffline(false);
};
const isOnline = useSyncExternalStore(
subscribeToNetworkConnectivity,
getNetworkConnectivitySnapshot,
getNetworkConnectivityServerSnapshot
);

useEffect(() => {
if (isWindowAvailable()) {
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);

return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
};
if (isOnline) {
options.onOnline && options.onOnline(true);
} else if (options.onOnline) {
options.onOffline && options.onOffline(false);
}
}, []);
}, [isOnline, options]);

return isOnline;
};
12 changes: 12 additions & 0 deletions packages/dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# @remix-pwa/dev 3.1.0 (2024-08-11)


### Bug Fixes

* **dev:** pre-rc-candidate: adding (minimal) global support for Remix SPA mode a035c49


### Features

* **dev:** added injectable vars cac1206

# @remix-pwa/dev 3.1.0-dev.1 (2024-08-11)


Expand Down
2 changes: 1 addition & 1 deletion packages/dev/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remix-pwa/dev",
"version": "3.1.0-dev.1",
"version": "3.1.0",
"description": "An esbuild compiler for Service Workers in Remix.run",
"repository": {
"type": "git",
Expand Down
7 changes: 7 additions & 0 deletions packages/sw/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## @remix-pwa/sw 3.0.9 (2024-08-11)


### Bug Fixes

* **sw:** added `matchOptions` for adding querying options to cache operations c87e507

## @remix-pwa/sw 3.0.9-dev.1 (2024-08-11)


Expand Down
2 changes: 1 addition & 1 deletion packages/sw/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remix-pwa/sw",
"version": "3.0.9-dev.1",
"version": "3.0.9",
"description": "Service Worker APIs and utilities for Remix PWA",
"repository": {
"type": "git",
Expand Down
10 changes: 10 additions & 0 deletions packages/sync/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## @remix-pwa/sync 3.0.4 (2024-08-11)





### Dependencies

* **@remix-pwa/sw:** upgraded to 3.0.9

## @remix-pwa/sync 3.0.4-dev.1 (2024-08-11)


Expand Down
4 changes: 2 additions & 2 deletions packages/sync/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remix-pwa/sync",
"version": "3.0.4-dev.1",
"version": "3.0.4",
"description": "A Background Sync addon for Remix PWA",
"repository": {
"type": "git",
Expand Down Expand Up @@ -34,7 +34,7 @@
"rimraf": "^6.0.1"
},
"dependencies": {
"@remix-pwa/sw": "^3.0.9-dev.1",
"@remix-pwa/sw": "^3.0.9",
"idb": "^8.0.0"
}
}
7 changes: 7 additions & 0 deletions packages/worker-runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## @remix-pwa/worker-runtime 2.1.4 (2024-08-11)


### Bug Fixes

* **worker-runtime:** added minimal support for runtimes in Remix SPA mode 6dd95e6

## @remix-pwa/worker-runtime 2.1.4-dev.1 (2024-08-11)


Expand Down
2 changes: 1 addition & 1 deletion packages/worker-runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remix-pwa/worker-runtime",
"version": "2.1.4-dev.1",
"version": "2.1.4",
"description": "A vanilla JavaScript worker runtime for Remix service workers",
"license": "MIT",
"sideEffects": false,
Expand Down

0 comments on commit dec144d

Please sign in to comment.