Skip to content

Commit

Permalink
Merge pull request #578 from StampyAI/cache-fixes
Browse files Browse the repository at this point in the history
Cache fixes
  • Loading branch information
mruwnik committed Apr 2, 2024
2 parents 931feb7 + a78f41d commit 4aecbf9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app/components/Article/Contents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const glossaryInjecter = (pageid: string, glossary: Glossary) => {
return (html: string) =>
Object.values(glossary)
.filter((item) => item.pageid != pageid)
.sort((a, b) => b.alias.length - a.alias.length)
.sort((a, b) => (b.alias?.length ?? 0) - (a.alias?.length ?? 0))
.reduce((html, {term, alias}) => {
const match = new RegExp(`(^|[^\\w-])(${alias})($|[^\\w-])`, 'i')
if (!seen.has(term) && html.search(match) >= 0) {
Expand Down
5 changes: 4 additions & 1 deletion app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ export const loader = async ({request}: Parameters<LoaderFunction>[0]) => {
const embed = !!request.url.match(/embed/)
const showSearch = !request.url.match(/onlyInitial/)

const question = await fetchQuestion(request)
const question = await fetchQuestion(request).catch((e) => {
console.error('\n\nUnexpected error in loader\n', e)
return null
})

return {
question,
Expand Down
File renamed without changes.
44 changes: 13 additions & 31 deletions app/routes/questions.$questionId.$.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,17 @@ export const loader = async ({request, params}: LoaderFunctionArgs) => {
}

try {
const dataPromise = loadQuestionDetail(request, questionId)
.then(({data}) => data)
.catch(raise500)
const dataPromise = loadQuestionDetail(request, questionId).catch(raise500)
const tagsPromise = loadTags(request)
.then(({data}) => data)
.catch(raise500)
return defer({data: dataPromise, tags: tagsPromise})
return defer({question: dataPromise, tags: tagsPromise})
} catch (error: unknown) {
const msg = `No question found with ID ${questionId}. Please go to <a href="https://discord.com/invite/Bt8PaRTDQC">Discord</a> and report where you found this link.`
throw new Response(msg, {status: 404})
}
}

export function fetchQuestion(pageid: string) {
const url = `/questions/${encodeURIComponent(pageid)}`
return fetch(url)
.then(async (response) => {
const json: Awaited<ReturnType<typeof loadQuestionDetail>> = await response.json()
if ('error' in json) console.error(json.error)
const {data, timestamp} = json

reloadInBackgroundIfNeeded(url, timestamp)

return data
})
.catch((e) => {
throw raise500(e)
})
}

const dummyQuestion = (title: string | undefined) =>
({
text: 'Loading...',
Expand All @@ -80,7 +61,7 @@ export default function RenderArticle() {
const [showNav, setShowNav] = useState(false) // Used on mobile
const params = useParams()
const pageid = params.questionId ?? '😱'
const {data, tags} = useLoaderData<typeof loader>()
const {question, tags} = useLoaderData<typeof loader>()
const {findSection, getArticle, getPath} = useToC()
const section = findSection(location?.state?.section || pageid)

Expand All @@ -97,11 +78,12 @@ export default function RenderArticle() {
}, [location.key])

useEffect(() => {
data.then((val) => {
const question = val as Question
question.then((val) => {
const {data: question, timestamp} = val as {data: Question; timestamp: string}
reloadInBackgroundIfNeeded(location.pathname, timestamp)
if (question.title) document.title = question.title
})
}, [data])
}, [question, location])

return (
<Page modal={showNav}>
Expand Down Expand Up @@ -145,18 +127,18 @@ export default function RenderArticle() {
/>
}
>
<Await resolve={Promise.all([data, tags])}>
{([resolvedValue, tags]) => {
if (resolvedValue instanceof Response) {
return <Error error={resolvedValue} />
} else if (!(resolvedValue as any)?.pageid) {
<Await resolve={Promise.all([question, tags])}>
{([resolvedQuestion, resolvedTags]) => {
if (resolvedQuestion instanceof Response || !('data' in resolvedQuestion)) {
return <Error error={resolvedQuestion} />
} else if (!resolvedQuestion.data.pageid) {
return (
<Error error={{statusText: 'Could not fetch question', status: 'emptyArticle'}} />
)
} else {
return (
<Article
question={updateTags(resolvedValue as Question, tags as Tag[])}
question={updateTags(resolvedQuestion.data as Question, resolvedTags as Tag[])}
glossary={glossary}
className={showNav ? 'desktop-only' : ''}
/>
Expand Down
2 changes: 1 addition & 1 deletion app/server-utils/stampy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type Banner = {
}
export type GlossaryEntry = {
term: string
alias: string
alias?: string // not present in actual Coda response
pageid: PageId
contents: string
image: string
Expand Down

0 comments on commit 4aecbf9

Please sign in to comment.