From c0a9d99df7f8ae147e34b8773634ca309257804c Mon Sep 17 00:00:00 2001 From: Peter Hozak Date: Tue, 2 Apr 2024 18:58:57 +0200 Subject: [PATCH 1/4] revert removal of catch from 2b255e8 --- app/root.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/root.tsx b/app/root.tsx index ab04df2f..b1bca492 100644 --- a/app/root.tsx +++ b/app/root.tsx @@ -97,7 +97,10 @@ export const loader = async ({request}: Parameters[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, From 9865c54bef424f9c24c37ad003705484d78cc442 Mon Sep 17 00:00:00 2001 From: Peter Hozak Date: Tue, 2 Apr 2024 19:01:06 +0200 Subject: [PATCH 2/4] move cache route out of questions path to avoid error from fetchQuestion --- app/routes/{questions.cache.tsx => cache.tsx} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/routes/{questions.cache.tsx => cache.tsx} (100%) diff --git a/app/routes/questions.cache.tsx b/app/routes/cache.tsx similarity index 100% rename from app/routes/questions.cache.tsx rename to app/routes/cache.tsx From 61dd30019a20ff8c976c3212f8e535a522560838 Mon Sep 17 00:00:00 2001 From: Peter Hozak Date: Tue, 2 Apr 2024 20:35:20 +0200 Subject: [PATCH 3/4] delete unused code and call reloadInBackgroundIfNeeded from effect #576 --- app/routes/questions.$questionId.$.tsx | 41 ++++++++------------------ app/server-utils/stampy.ts | 2 +- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/app/routes/questions.$questionId.$.tsx b/app/routes/questions.$questionId.$.tsx index 34c1c2e4..2fb456b9 100644 --- a/app/routes/questions.$questionId.$.tsx +++ b/app/routes/questions.$questionId.$.tsx @@ -27,35 +27,17 @@ export const loader = async ({request, params}: LoaderFunctionArgs) => { try { const dataPromise = loadQuestionDetail(request, questionId) - .then(({data}) => data) .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 Discord 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> = 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...', @@ -80,7 +62,7 @@ export default function RenderArticle() { const [showNav, setShowNav] = useState(false) // Used on mobile const params = useParams() const pageid = params.questionId ?? '😱' - const {data, tags} = useLoaderData() + const {question, tags} = useLoaderData() const {findSection, getArticle, getPath} = useToC() const section = findSection(location?.state?.section || pageid) @@ -97,11 +79,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 ( @@ -145,18 +128,18 @@ export default function RenderArticle() { /> } > - - {([resolvedValue, tags]) => { - if (resolvedValue instanceof Response) { - return - } else if (!(resolvedValue as any)?.pageid) { + + {([resolvedQuestion, resolvedTags]) => { + if (resolvedQuestion instanceof Response || !('data' in resolvedQuestion)) { + return + } else if (!resolvedQuestion.data.pageid) { return ( ) } else { return (
diff --git a/app/server-utils/stampy.ts b/app/server-utils/stampy.ts index 437fa45b..60289201 100644 --- a/app/server-utils/stampy.ts +++ b/app/server-utils/stampy.ts @@ -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 From a78f41dae8147cc38eeeee97aac7f701707c4524 Mon Sep 17 00:00:00 2001 From: Peter Hozak Date: Tue, 2 Apr 2024 20:50:43 +0200 Subject: [PATCH 4/4] fix missing glossary alias --- app/components/Article/Contents.tsx | 2 +- app/routes/questions.$questionId.$.tsx | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/components/Article/Contents.tsx b/app/components/Article/Contents.tsx index e2ec1866..9a511750 100644 --- a/app/components/Article/Contents.tsx +++ b/app/components/Article/Contents.tsx @@ -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) { diff --git a/app/routes/questions.$questionId.$.tsx b/app/routes/questions.$questionId.$.tsx index 2fb456b9..b2af8c04 100644 --- a/app/routes/questions.$questionId.$.tsx +++ b/app/routes/questions.$questionId.$.tsx @@ -26,8 +26,7 @@ export const loader = async ({request, params}: LoaderFunctionArgs) => { } try { - const dataPromise = loadQuestionDetail(request, questionId) - .catch(raise500) + const dataPromise = loadQuestionDetail(request, questionId).catch(raise500) const tagsPromise = loadTags(request) .then(({data}) => data) .catch(raise500)