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

Add support for fetchOptions to <CheckForApplicationUpdate> #9436

Merged
merged 13 commits into from
Nov 13, 2023
7 changes: 7 additions & 0 deletions docs/CheckForApplicationUpdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const App = () => (
| `disabled` | Optional | boolean | `false` in `production` mode | Whether the automatic check is disabled |
| `notification` | Optional | ReactElement | | The notification to display to the user when an update is available |
| `url` | Optional | string | current URL | The URL to download to check for code update |
| `fetchOptions` | Optional | `RequestInit | undefined` | `undefined` | The options passed to `fetch` when checking for an update |

## `interval`

Expand Down Expand Up @@ -138,6 +139,12 @@ export const MyLayout = ({ children, ...props }: LayoutProps) => (
);
```

## `fetchOptions`

You can also customize the request [options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options) passed along to fetch function when detecting updates.

Tip: Depending on your server-side HTTP cache settings, you may want to set the fetchOptions to `{ cache: "no-cache" }` to check if the resource has changed.

## Internationalization

You can customize the texts of the default notification by overriding the following keys:
Expand Down
24 changes: 18 additions & 6 deletions packages/ra-core/src/util/useCheckForApplicationUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useEvent } from './useEvent';
* @param {UseCheckForApplicationUpdateOptions} options The options
* @param {Function} options.onNewVersionAvailable The function to call when a new version of the application is available.
* @param {string} options.url Optional. The URL to download to check for code update. Defaults to the current URL.
* @param {RequestInit} options.fetchOptions Optional. The options passed to fetch function when checking for update.
* @param {number} options.interval Optional. The interval in milliseconds between two checks. Defaults to 3600000 (1 hour).
* @param {boolean} options.disabled Optional. Whether the check should be disabled. Defaults to false.
*/
Expand All @@ -18,6 +19,7 @@ export const useCheckForApplicationUpdate = (
) => {
const {
url = window.location.href,
fetchOptions,
interval: delay = ONE_HOUR,
onNewVersionAvailable: onNewVersionAvailableProp,
disabled = process.env.NODE_ENV !== 'production',
Expand All @@ -28,18 +30,19 @@ export const useCheckForApplicationUpdate = (
useEffect(() => {
if (disabled) return;

getHashForUrl(url).then(hash => {
getHashForUrl(url, fetchOptions).then(hash => {
if (hash != null) {
currentHash.current = hash;
}
});
}, [disabled, url]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [disabled, url, JSON.stringify(fetchOptions)]);

useEffect(() => {
if (disabled) return;

const interval = setInterval(() => {
getHashForUrl(url)
getHashForUrl(url, fetchOptions)
.then(hash => {
if (hash != null && currentHash.current !== hash) {
// Store the latest hash to avoid calling the onNewVersionAvailable function multiple times
Expand All @@ -53,12 +56,20 @@ export const useCheckForApplicationUpdate = (
});
}, delay);
return () => clearInterval(interval);
}, [delay, onNewVersionAvailable, disabled, url]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
delay,
onNewVersionAvailable,
disabled,
url,
// eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(fetchOptions),
]);
};

const getHashForUrl = async (url: string) => {
const getHashForUrl = async (url: string, fetchOptions?: RequestInit) => {
try {
const response = await fetch(url);
const response = await fetch(url, fetchOptions);
if (!response.ok) return null;
const text = await response.text();
return hash(text);
Expand Down Expand Up @@ -90,5 +101,6 @@ export interface UseCheckForApplicationUpdateOptions {
onNewVersionAvailable: () => void;
interval?: number;
url?: string;
fetchOptions?: RequestInit;
disabled?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ApplicationUpdatedNotification } from './ApplicationUpdatedNotification
* @param {boolean} options.disabled Optional. Whether the check should be disabled. Defaults to false.
* @param {string|ReactElement} props.notification The notification to display to the user. Displayed only if `updateMode` is manual. Defaults to `<ApplicationUpdatedNotification />`.
* @param {string} options.url Optional. The URL to download to check for code update. Defaults to the current URL.
* @param {RequestInit} options.fetchOptions Optional. The options passed to fetch function when checking for update.
* @param {number} options.interval Optional. The interval in milliseconds between two checks. Defaults to 3600000 (1 hour).
*
* @example <caption>Basic usage</caption>
Expand Down
Loading