From 07669c6c2b4b7f4ada8997b7b89e65c065b69090 Mon Sep 17 00:00:00 2001 From: Tim Hostetler <6970899+thostetler@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:16:52 -0500 Subject: [PATCH] Fix unnecessary requests issue Seems that sometimes the app can get into a request retry loop and it can cause cascading requests. Part of the issue is that in the case of 401s a separate (from RC) handler performs it's own re-request. This disables retrying for now until I can figure out the right mix. --- global.d.ts | 27 +++++++++++++++++++++++++++ src/api/config.ts | 5 +++++ src/lib/useCreateQueryClient.ts | 16 ++++++++++++++-- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/global.d.ts b/global.d.ts index 3b7abca3b..8a4f3b4f3 100644 --- a/global.d.ts +++ b/global.d.ts @@ -19,3 +19,30 @@ declare module 'iron-session' { bot?: boolean; } } + +declare global { + namespace NodeJS { + interface ProcessEnv { + [key: string]: string | undefined; + BASE_CANONICAL_URL: string; + API_HOST_CLIENT: string; + API_HOST_SERVER: string; + NEXT_PUBLIC_API_HOST_CLIENT: string; + COOKIE_SECRET: string; + ADS_SESSION_COOKIE_NAME: string; + SCIX_SESSION_COOKIE_NAME: string; + NEXT_PUBLIC_ORCID_CLIENT_ID: string; + NEXT_PUBLIC_ORCID_API_URL: string; + NEXT_PUBLIC_ORCID_REDIRECT_URI: string; + REDIS_HOST: string; + REDIS_PORT: string; + REDIS_PASSWORD: string; + VERIFIED_BOTS_ACCESS_TOKEN: string; + UNVERIFIABLE_BOTS_ACCESS_TOKEN: string; + MALICIOUS_BOTS_ACCESS_TOKEN: string; + NEXT_PUBLIC_GTM_ID: string; + NEXT_PUBLIC_RECAPTCHA_SITE_KEY: string; + NEXT_PUBLIC_API_MOCKING: string; + } + } +} diff --git a/src/api/config.ts b/src/api/config.ts index 9d4862ee8..0c29d0491 100644 --- a/src/api/config.ts +++ b/src/api/config.ts @@ -12,6 +12,11 @@ const resolveApiBaseUrl = (defaultBaseUrl = ''): string => { return 'http://localhost'; } + // use a known URL for development + if (process.env.NODE_ENV === 'development' && typeof process.env.NEXT_PUBLIC_API_HOST_CLIENT === 'string') { + return process.env.NEXT_PUBLIC_API_HOST_CLIENT; + } + const config = getConfig() as AppRuntimeConfig; if (typeof config === 'undefined') { diff --git a/src/lib/useCreateQueryClient.ts b/src/lib/useCreateQueryClient.ts index 70aafb89a..110d3fb97 100644 --- a/src/lib/useCreateQueryClient.ts +++ b/src/lib/useCreateQueryClient.ts @@ -1,6 +1,7 @@ import axios from 'axios'; import { useState } from 'react'; -import { QueryCache, QueryClient } from '@tanstack/react-query'; +import { MutationCache, QueryCache, QueryClient } from '@tanstack/react-query'; +import { logger } from '../../logger/logger'; export const useCreateQueryClient = () => { const queryCache = new QueryCache({ @@ -11,7 +12,15 @@ export const useCreateQueryClient = () => { } if (axios.isAxiosError(error) || error instanceof Error) { - // TODO: global error, what should be done here? + logger.error({ msg: 'Query error', error, query }); + } + }, + }); + + const mutationCache = new MutationCache({ + onError: (error, mutation) => { + if (axios.isAxiosError(error) || error instanceof Error) { + logger.error({ msg: 'Mutation error', error, mutation }); } }, }); @@ -25,9 +34,12 @@ export const useCreateQueryClient = () => { refetchOnMount: false, refetchOnReconnect: false, staleTime: Infinity, + retry: false, + retryOnMount: false, }, }, queryCache, + mutationCache, }), );