Skip to content

Commit

Permalink
Update the API request timeouts for SSR
Browse files Browse the repository at this point in the history
Changed the serverside API call timeout to 1.5 seconds (down from 30
seconds, 5% of the clientside calls)
Clientside should refetch it and show a loading state if the query fails
to load in time.

This timeout value will need to be tested, but the percieved speed is
definitely improved with a short server-side timeout.

For URL array params switch from `repeat` to `comma` and update tests
that broke.
  • Loading branch information
thostetler committed Mar 24, 2024
1 parent 2d677b8 commit 6252a7e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/api/config.ts
Expand Up @@ -2,6 +2,7 @@ import { AppRuntimeConfig } from '@types';
import { AxiosRequestConfig } from 'axios';
import getConfig from 'next/config';
import qs from 'qs';
import { APP_DEFAULTS } from '@config';

/**
* Figure out which config to pick, based on the current environment
Expand Down Expand Up @@ -30,12 +31,12 @@ const resolveApiBaseUrl = (defaultBaseUrl = ''): string => {
export const defaultRequestConfig: AxiosRequestConfig = {
baseURL: resolveApiBaseUrl(),
withCredentials: true,
timeout: 30000,
timeout: typeof window === 'undefined' ? APP_DEFAULTS.SSR_API_TIMEOUT : APP_DEFAULTS.API_TIMEOUT,
paramsSerializer: {
serialize: (params) =>
qs.stringify(params, {
indices: false,
arrayFormat: 'repeat',
arrayFormat: 'comma',
format: 'RFC1738',
sort: (a, b) => a - b,
skipNulls: true,
Expand Down
4 changes: 2 additions & 2 deletions src/components/ClassicForm/__tests__/helpers.test.ts
Expand Up @@ -111,7 +111,7 @@ describe('Classic Form Query Handling', () => {
])(`getBibstems(%s) -> %s`, (bibstems, expected) => expect(getBibstems(bibstems)).toBe(expected));

test('getSearchQuery handles empty input', () => {
expect(getSearchQuery({} as IRawClassicFormState)).toBe('q=%2A%3A%2A&sort=score+desc&sort=date+desc&p=1');
expect(getSearchQuery({} as IRawClassicFormState)).toBe('q=%2A%3A%2A&sort=score+desc%2Cdate+desc&p=1');
});

test('getSearchQuery properly generates search query', () => {
Expand All @@ -135,6 +135,6 @@ describe('Classic Form Query Handling', () => {
expect(result.get('q')).toBe(
`collection:(astronomy physics) pubdate:[2020-12 TO 2022-01] author:("Smith, A" "Jones, B" ="Jones, Bob") object:(IRAS HIP) property:(refereed article) title:("Black Hole" -"Milky Way" -star) abs:("Event Horizon" Singularity) bibstem:(PhRvL) -bibstem:(Apj)`,
);
expect(result.getAll('sort')).toStrictEqual(['score desc', 'date desc']);
expect(result.getAll('sort')).toStrictEqual(['score desc,date desc']);
});
});
2 changes: 2 additions & 0 deletions src/config.ts
Expand Up @@ -16,6 +16,8 @@ export const APP_DEFAULTS = {
BIBTEX_DEFAULT_AUTHOR_CUTOFF: 10,
RESULT_ITEM_PUB_CUTOFF: 50,
EMPTY_QUERY: '*:*',
API_TIMEOUT: 30000,
SSR_API_TIMEOUT: 1500,
} as const;

export const GOOGLE_RECAPTCHA_KEY = '6Lex_aQUAAAAAMwJFbdGFeigshN7mRQdbXoGQ7-N';
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Expand Up @@ -316,7 +316,7 @@ export const makeSearchParams = (params: SafeSearchUrlParams, options: { omit?:
export const stringifySearchParams = (params: Record<string, unknown>, options?: qs.IStringifyOptions) =>
qs.stringify(params, {
indices: false,
arrayFormat: 'repeat',
arrayFormat: 'comma',
format: 'RFC1738',
sort: (a, b) => a - b,
skipNulls: true,
Expand Down

0 comments on commit 6252a7e

Please sign in to comment.