mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-15 09:46:19 +01:00
✨ Add cache and stale timers for most react query
This commit is contained in:
@@ -53,6 +53,7 @@ function CalendarTile({ widget }: CalendarTileProps) {
|
||||
|
||||
const { data: medias } = useQuery({
|
||||
queryKey: ['calendar/medias', { month: month.getMonth(), year: month.getFullYear() }],
|
||||
staleTime: 1000 * 60 * 60 * 5,
|
||||
queryFn: async () =>
|
||||
(await (
|
||||
await fetch(
|
||||
|
||||
@@ -76,7 +76,7 @@ function WeatherTile({ widget }: WeatherTileProps) {
|
||||
align="center"
|
||||
style={{ height: '100%', width: '100%' }}
|
||||
>
|
||||
<Group align={'center'} position="center" spacing="xs">
|
||||
<Group align="center" position="center" spacing="xs">
|
||||
<WeatherIcon code={weather!.current_weather.weathercode} />
|
||||
<Title>
|
||||
{getPerferedUnit(
|
||||
|
||||
@@ -11,12 +11,18 @@ export const useWeatherForCity = (cityName: string) => {
|
||||
data: city,
|
||||
isLoading,
|
||||
isError,
|
||||
} = useQuery({ queryKey: ['weatherCity', { cityName }], queryFn: () => fetchCity(cityName) });
|
||||
} = useQuery({
|
||||
queryKey: ['weatherCity', { cityName }],
|
||||
queryFn: () => fetchCity(cityName),
|
||||
cacheTime: 1000 * 60 * 60 * 24, // the city is cached for 24 hours
|
||||
staleTime: Infinity, // the city is never considered stale
|
||||
});
|
||||
const weatherQuery = useQuery({
|
||||
queryKey: ['weather', { cityName }],
|
||||
queryFn: () => fetchWeather(city?.results[0]),
|
||||
enabled: !!city,
|
||||
refetchInterval: 1000 * 60 * 5, // requests the weather every 5 minutes
|
||||
cacheTime: 1000 * 60 * 60 * 6, // the weather is cached for 6 hours
|
||||
staleTime: 1000 * 60 * 5, // the weather is considered stale after 5 minutes
|
||||
});
|
||||
|
||||
return {
|
||||
@@ -41,13 +47,13 @@ const fetchCity = async (cityName: string) => {
|
||||
* @param coordinates of the location the weather should be fetched
|
||||
* @returns weather of specified coordinates
|
||||
*/
|
||||
const fetchWeather = async (coordinates?: Coordinates) => {
|
||||
if (!coordinates) return;
|
||||
async function fetchWeather(coordinates?: Coordinates) {
|
||||
if (!coordinates) return null;
|
||||
const { longitude, latitude } = coordinates;
|
||||
const res = await fetch(
|
||||
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&daily=weathercode,temperature_2m_max,temperature_2m_min¤t_weather=true&timezone=Europe%2FLondon`
|
||||
);
|
||||
return (await res.json()) as WeatherResponse;
|
||||
};
|
||||
}
|
||||
|
||||
type Coordinates = { latitude: number; longitude: number };
|
||||
|
||||
Reference in New Issue
Block a user