Skip to content

Commit

Permalink
better handling of redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
mruwnik committed Dec 8, 2024
1 parent 28073f8 commit 59cae3c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/routes/$redirects.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {LoaderFunctionArgs} from '@remix-run/cloudflare'
import {redirect} from '@remix-run/cloudflare'
import {loadRedirects} from '~/server-utils/stampy'
import {cleanRedirectPath, loadRedirects} from '~/server-utils/stampy'

export const loader = async ({request, params}: LoaderFunctionArgs) => {
try {
const {data: redirects} = await loadRedirects(request)
const to = params['*'] && redirects[params['*'].replace(/^\/+/, '')]
const to = params['*'] && redirects[cleanRedirectPath(params['*'])]
if (to) return redirect(to)
} catch (e) {
console.error(e)
Expand Down
20 changes: 18 additions & 2 deletions app/routes/questions.$questionId.$.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import {Suspense, useEffect, useState} from 'react'
import {useLocation, useSearchParams} from 'react-router-dom'
import {Await, useLoaderData, useParams} from '@remix-run/react'
import {defer, type LoaderFunctionArgs} from '@remix-run/cloudflare'
import {defer, redirect, type LoaderFunctionArgs} from '@remix-run/cloudflare'
import Page from '~/components/Page'
import Article from '~/components/Article'
import Button from '~/components/Button'
import Error from '~/components/Error'
import XIcon from '~/components/icons-generated/X'
import ChevronRight from '~/components/icons-generated/ChevronRight'
import {ArticlesNav} from '~/components/ArticlesNav/ArticleNav'
import {QuestionStatus, loadQuestionDetail} from '~/server-utils/stampy'
import {
QuestionStatus,
loadQuestionDetail,
loadRedirects,
cleanRedirectPath,
} from '~/server-utils/stampy'
import useToC from '~/hooks/useToC'
import useGlossary from '~/hooks/useGlossary'
import useOnSiteQuestions from '~/hooks/useOnSiteQuestions'
Expand All @@ -21,6 +26,17 @@ import {ArticlesNavManualList} from '~/components/ArticlesNav/ArticleNavManualLi
export const LINK_WITHOUT_DETAILS_CLS = 'link-without-details'

export const loader = async ({request, params}: LoaderFunctionArgs) => {
const {data: redirects} = await loadRedirects(request)

// Check if we need to redirect to a new path
const path = new URL(request.url).pathname
const cleanedPath = cleanRedirectPath(path)
const to = redirects[cleanedPath]
if (to) {
return redirect(to)
}

// Otherwise, load the question
const {questionId} = params
if (!questionId) {
throw new Response('Missing question title', {status: 400})
Expand Down
4 changes: 3 additions & 1 deletion app/server-utils/stampy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,16 @@ export const incAnswerColumn = async (column: string, pageid: PageId, subtract:
export const makeColumnIncrementer = (column: string) => (pageid: PageId, subtract: boolean) =>
incAnswerColumn(column, pageid, subtract)

export const cleanRedirectPath = (path: string) => path.replace(/^\/+/, '').replace(/\/$/, '')

export const loadRedirects = withCache('redirects', async (): Promise<Redirects> => {
const rows = (await getCodaRows(REDIRECTS_TABLE)) as RedirectsRow[]
return rows
.filter((row) => !!row.values.To && !!row.values.From)
.reduce(
(acc, {values}) => ({
...acc,
[extractText(values.From).replace(/^\/+/, '')]: extractText(values.To).replace(/^\/+/, '/'),
[cleanRedirectPath(values.From)]: extractText(values.To).replace(/^\/+/, '/'),
}),
{}
)
Expand Down

0 comments on commit 59cae3c

Please sign in to comment.