Skip to content

Commit

Permalink
Coda configurable redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
mruwnik committed Feb 4, 2024
1 parent 26460e2 commit 3f3b813
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
23 changes: 23 additions & 0 deletions app/routes/$redirects.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {LoaderArgs} from '@remix-run/cloudflare'
import {redirect} from '@remix-run/cloudflare'
import {loadRedirects} from '~/server-utils/stampy'

export const loader = async ({request, params}: LoaderArgs) => {
try {
const {data: redirects} = await loadRedirects(request)
const to = params['*'] && redirects[params['*'].replace(/^\/+/, '')]
if (to) return redirect(to.replace(/^\/+/, '/'))
} catch (e) {
console.error(e)
}
throw new Response(null, {
status: 404,
statusText: 'Not Found',
})
}

const RenderUI = () => {
// This should never be called, but is required for Remix to treat it as an UI route
return null
}
export default RenderUI
3 changes: 0 additions & 3 deletions app/routes/help.tsx

This file was deleted.

1 change: 1 addition & 0 deletions app/server-utils/coda-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const TAGS_TABLE = 'grid-4uOTjz1Rkz'
export const WRITES_TABLE = 'table-eEhx2YPsBE'
export const GLOSSARY_TABLE = 'grid-_pSzs23jmw'
export const BANNERS_TABLE = 'grid-3WgZ9_NkvO'
export const REDIRECTS_TABLE = 'grid-3MRnDQLOm3'

type CodaRequest = {
table: string
Expand Down
27 changes: 26 additions & 1 deletion app/server-utils/stampy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
TAGS_TABLE,
WRITES_TABLE,
BANNERS_TABLE,
REDIRECTS_TABLE,
makeCodaRequest,
} from './coda-urls'

Expand Down Expand Up @@ -90,6 +91,11 @@ type Entity = {
rowId: string
tableUrl: string
}

type Redirects = {
[from: string]: string
}

type CodaRowCommon = {
id: string
type: string
Expand Down Expand Up @@ -144,7 +150,13 @@ type BannersRow = CodaRowCommon & {
'Text colour': string
}
}
type CodaRow = AnswersRow | TagsRow | GlossaryRow | BannersRow
type RedirectsRow = CodaRowCommon & {
values: {
From: string
To: string
}
}
type CodaRow = AnswersRow | TagsRow | GlossaryRow | BannersRow | RedirectsRow
export type CodaResponse = {
items: CodaRow[]
nextPageLink: string | null
Expand Down Expand Up @@ -435,3 +447,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 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),
}),
{}
)
})
7 changes: 7 additions & 0 deletions remix.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
/** @type {import('@remix-run/dev').AppConfig} */
const routes = (defineRoutes) => {
return defineRoutes((route) => {
route('/*', 'routes/$redirects.tsx')
})
}

module.exports = {
serverConditions: ['worker'],
serverMainFields: ['browser', 'module', 'main'],
serverModuleFormat: 'esm',
serverPlatform: 'neutral',
serverDependenciesToBundle: 'all',
server: './server.js',
routes,
}

0 comments on commit 3f3b813

Please sign in to comment.