Skip to content

Commit

Permalink
Merge pull request #611 from audioverse-org/vercel-timeouts
Browse files Browse the repository at this point in the history
Fix Vercel timeouts
  • Loading branch information
narthur authored Jan 1, 2025
2 parents bf0e3e3 + 4a0db8d commit 19d09bf
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 76 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/graphql-codegen/dist
**/__generated__
**/__generated__/**
.temp*

# next-pwa
/public/sw.js
Expand Down
3 changes: 1 addition & 2 deletions src/components/organisms/passageNavigation/bookGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import clsx from 'clsx';
import React from 'react';

import { Book } from '.';
import { PassageNavigationFragment } from './__generated__/index';
import styles from './bookGrid.module.scss';
import ChapterGrid, { ChapterId } from './chapterGrid';

type Props = {
books: Array<PassageNavigationFragment>;
books: Array<Book>;
selectedBook: Book;
selectBook: (book: Book) => void;
chapterId: ChapterId;
Expand Down
3 changes: 1 addition & 2 deletions src/components/organisms/passageNavigation/bookList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import clsx from 'clsx';
import React from 'react';

import { Book } from '.';
import { PassageNavigationFragment } from './__generated__/index';
import styles from './bookList.module.scss';
import ChapterGrid, { ChapterId } from './chapterGrid';

type Props = {
books: Array<PassageNavigationFragment>;
books: Array<Book>;
selectedBook: Book;
selectBook: (book: Book) => void;
chapterId: ChapterId;
Expand Down
3 changes: 1 addition & 2 deletions src/components/organisms/passageNavigation/chapterGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import React from 'react';

import Link from '~components/atoms/linkWithoutPrefetch';

import { PassageNavigationFragment } from './__generated__/index';
import { Chapter } from '.';
import styles from './chapterGrid.module.scss';

type Chapter = NonNullable<PassageNavigationFragment['recordings']['nodes']>[0];
export type ChapterId = Chapter['id'] | null;

type Props = {
Expand Down
26 changes: 22 additions & 4 deletions src/components/organisms/passageNavigation/index.graphql
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
fragment passageNavigation on Sequence {
fragment passageNavigationChapter on Recording {
id
title
canonicalPath(useFuturePath: true)
collection {
id
contentType
}
}

fragment passageNavigationBook on Sequence {
id
title
recordings(first: 150) {
nodes {
id
title
...andMiniplayer
...passageNavigationChapter
}
}
}

fragment passageNavigationVersion on Collection {
id
title
sequences {
nodes {
...passageNavigationBook
}
}
}
20 changes: 14 additions & 6 deletions src/components/organisms/passageNavigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,28 @@ import BibleVersionTypeLockup from '~src/components/molecules/bibleVersionTypeLo
import Button from '~src/components/molecules/button';
import Dropdown from '~src/components/molecules/dropdown';
import { PlaybackContext } from '~src/components/templates/andPlaybackContext';
import { GetAudiobibleIndexDataQuery } from '~src/containers/bible/__generated__';
import { BIBLE_BOOKS } from '~src/lib/constants';
import { getBibleAcronym } from '~src/lib/getBibleAcronym';
import { useLocalStorage } from '~src/lib/hooks/useLocalStorage';

import {
PassageNavigationBookFragment,
PassageNavigationChapterFragment,
PassageNavigationVersionFragment,
} from './__generated__';
import BookGrid from './bookGrid';
import BookList from './bookList';
import styles from './index.module.scss';

export type Version = NonNullable<
GetAudiobibleIndexDataQuery['collections']['nodes']
>[0];
export type Book = NonNullable<Version['sequences']['nodes']>[0];
export type Chapter = NonNullable<Book['recordings']['nodes']>[0];
// export type Version = NonNullable<
// GetAudiobibleIndexDataQuery['collections']['nodes']
// >[0];
// export type Book = NonNullable<Version['sequences']['nodes']>[0];
// export type Chapter = NonNullable<Book['recordings']['nodes']>[0];

export type Version = PassageNavigationVersionFragment;
export type Book = PassageNavigationBookFragment;
export type Chapter = PassageNavigationChapterFragment;

type Props = {
versions: Array<Version>;
Expand Down
2 changes: 1 addition & 1 deletion src/containers/bible/index.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ query getAudiobibleIndexData($language: Language!) {
title
sequences(first: 66, orderBy: [{ field: ID, direction: ASC }]) {
nodes {
...passageNavigation
...passageNavigationBook
}
}
}
Expand Down
84 changes: 50 additions & 34 deletions src/lib/api/fetchApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pMemoize from 'p-memoize';

import { getCurrentRequest } from '~lib/api/storeRequest';
import { getSessionToken } from '~lib/cookies';
import { sleep } from '~lib/sleep';
Expand All @@ -21,6 +23,53 @@ async function getResponse(
});
}

const fetchJson = pMemoize(
async ({
headers,
query,
variables,
}: {
headers: HeadersInit;
query: string;
variables: unknown;
}) => {
let res = await getResponse(headers, query, variables);
let text = await res.text();

if (!res.ok) {
await sleep({ ms: 10000 });
res = await getResponse(headers, query, variables);
text = await res.text();
}

if (!res.ok) {
console.error({ text, res, query, variables, headers });
throw new Error(`HTTP request failed: ${res.status} ${res.statusText}`);
}

let json;

try {
json = JSON.parse(text);
} catch (error) {
console.error({ error, text, res, query, variables, headers });
throw error;
}

if (json.errors) {
console.error({
query,
variables,
headers,
errors: json.errors,
});
throw json;
}

return json.data;
},
);

export async function fetchApi<TData>(
query: string,
{ variables = {} } = {},
Expand All @@ -34,38 +83,5 @@ export async function fetchApi<TData>(
headers['x-av-session'] = sessionToken;
}

let res = await getResponse(headers, query, variables);
let text = await res.text();

if (!res.ok) {
await sleep({ ms: 10000 });
res = await getResponse(headers, query, variables);
text = await res.text();
}

if (!res.ok) {
console.error({ text, res, query, variables, headers });
throw new Error(`HTTP request failed: ${res.status} ${res.statusText}`);
}

let json;

try {
json = JSON.parse(text);
} catch (error) {
console.error({ error, text, res, query, variables, headers });
throw error;
}

if (json.errors) {
console.error({
query,
variables,
headers,
errors: json.errors,
});
throw json;
}

return json.data;
return fetchJson({ headers, query, variables });
}
44 changes: 19 additions & 25 deletions src/lib/getBibles.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {
CollectionContentType,
Language,
RecordingContentType,
SequenceContentType,
} from '~src/__generated__/graphql';
import pMemoize from 'p-memoize';

import { CollectionContentType, Language } from '~src/__generated__/graphql';
import { BibleIndexProps } from '~src/containers/bible';
import { getAudiobibleIndexData } from '~src/containers/bible/__generated__';
import { BOOK_ID_MAP } from '~src/services/fcbh/constants';
Expand Down Expand Up @@ -49,23 +46,10 @@ async function transform(
.bookId(bookId)
.chapterNumber(bbChapter.number)
.get(),
duration: bbChapter.duration,
recordingContentType: RecordingContentType.BibleChapter,
sequence: {
id: bbChapter.id,
title,
contentType: SequenceContentType.BibleBook,
},
audioFiles: [],
videoFiles: [],
videoStreams: [],
collection: {
id: bible.id,
title: bible.title,
contentType: CollectionContentType.BibleVersion,
},
speakers: [],
sponsor: null,
};
},
);
Expand Down Expand Up @@ -121,10 +105,20 @@ function concatBibles(
);
}

export default async function getBibles(languageId: Language) {
const fcbh = await getFcbhBibles(languageId);
const api = await getApiBibles(languageId);
const all = concatBibles(fcbh, api);
const getBibles = pMemoize(
async (
languageId: Language,
): Promise<{
fcbh: ApiBible[] | null;
api: ApiBible[] | null;
all: ApiBible[];
}> => {
const fcbh = await getFcbhBibles(languageId);
const api = await getApiBibles(languageId);
const all = concatBibles(fcbh, api);

return { fcbh, api, all };
}
return { fcbh, api, all };
},
);

export default getBibles;
5 changes: 5 additions & 0 deletions src/pages/[language]/bibles/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// import fs from 'fs';
import {
GetStaticPathsResult,
GetStaticPropsContext,
Expand Down Expand Up @@ -31,6 +32,10 @@ export async function getStaticProps({
};
}

// write `all` to a file
// fs.writeFileSync('.temp.bibles.json', JSON.stringify(all, null, 2));
// throw new Error('write `all` to a file');

return {
props: {
versions: all,
Expand Down

0 comments on commit 19d09bf

Please sign in to comment.