-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from remix-pwa/dev
Moving multiple PRs & fixes to release
- Loading branch information
Showing
17 changed files
with
505 additions
and
306 deletions.
There are no files selected for viewing
This file contains 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 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,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; | ||
}; |
This file contains 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 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 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 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 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,5 +1,12 @@ | ||
import type { LinkHTMLAttributes } from 'react'; | ||
import React from 'react'; | ||
|
||
export const ManifestLink = ({ manifestUrl = '/manifest.webmanifest' }: { manifestUrl?: string }) => { | ||
return <link rel="manifest" href={manifestUrl} />; | ||
export const ManifestLink = ({ | ||
crossOrigin, | ||
manifestUrl = '/manifest.webmanifest', | ||
}: { | ||
manifestUrl?: string; | ||
crossOrigin?: LinkHTMLAttributes<HTMLLinkElement>['crossOrigin']; | ||
}) => { | ||
return <link rel="webmanifest" href={manifestUrl} crossOrigin={crossOrigin} />; | ||
}; |
This file contains 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 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.