Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coda configurable redirects #357

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
} 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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).replace(/^\/+/, '/'),
}),
{}
)
})
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,
}
Loading