Skip to content

Commit

Permalink
refactor: bake redirects from db
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbrgl committed Feb 16, 2024
1 parent 90c76db commit bade737
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
26 changes: 11 additions & 15 deletions baker/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { memoize, JsonError, Url } from "@ourworldindata/utils"
import { isCanonicalInternalUrl } from "./formatting.js"
import { resolveExplorerRedirect } from "./replaceExplorerRedirects.js"
import { logErrorAndMaybeSendToBugsnag } from "../serverUtils/errorLog.js"
import { getRedirectsFromDb } from "../db/model/Redirect.js"

export const getRedirects = async () => {
const staticRedirects = [
Expand Down Expand Up @@ -57,22 +58,19 @@ export const getRedirects = async () => {
"/grapher/exports/* https://assets.ourworldindata.org/grapher/exports/:splat 301",
]

// Redirects from Wordpress admin UI
const wpRedirectRows = await wpdb.singleton.query(
`SELECT url, action_data, action_code FROM wp_redirection_items WHERE status = 'enabled'`
)
const wpRedirects = wpRedirectRows.map(
// Get redirects from the database (exported from the Wordpress DB)
const redirectsFromDb = (await getRedirectsFromDb()).map(
(row) =>
`${formatWpUrl(row.url)} ${formatWpUrl(row.action_data)} ${
row.action_code
}`
`${stripTrailingSlash(row.source)} ${stripTrailingSlash(
row.target
)} ${row.code}`
)

// Add newlines in between so we get some more overview
return [
...staticRedirects,
"",
...wpRedirects,
...redirectsFromDb,
"",
...dynamicRedirects, // Cloudflare requires all dynamic redirects to be at the very end of the _redirects file
]
Expand All @@ -96,12 +94,10 @@ export const getGrapherRedirectsMap = async (
)
}

export const formatWpUrl = (url: string) => {
export const stripTrailingSlash = (url: string) => {
if (url === "/") return url

return url
.replace(/__/g, "/") // replace __: abc__xyz -> abc/xyz
.replace(/\/$/, "") // remove trailing slash: /abc/ -> /abc
return url.replace(/\/$/, "") // remove trailing slash: /abc/ -> /abc
}

export const getWordpressRedirectsMap = async () => {
Expand All @@ -111,8 +107,8 @@ export const getWordpressRedirectsMap = async () => {

return new Map(
wordpressRedirectRows.map((row) => [
formatWpUrl(row.url),
formatWpUrl(row.action_data),
stripTrailingSlash(row.url),
stripTrailingSlash(row.action_data),
])
)
}
Expand Down
6 changes: 3 additions & 3 deletions baker/syncRedirectsToGrapher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as db from "../db/db"
import * as wpdb from "../db/wpdb"
import { getRedirectsFromDb } from "../db/model/Redirect.js"
import { formatWpUrl, resolveRedirectFromMap } from "./redirects.js"
import { stripTrailingSlash, resolveRedirectFromMap } from "./redirects.js"
import { Redirect, Url } from "@ourworldindata/utils"

// A close cousing of the getWordpressRedirectsMap() function from
Expand All @@ -17,8 +17,8 @@ export const syncRedirectsToGrapher = async (): Promise<void> => {

const allWordpressRedirects = allWordpressRedirectsRaw.map((r) => ({
...r,
source: formatWpUrl(r.source),
target: formatWpUrl(r.target),
source: stripTrailingSlash(r.source),
target: stripTrailingSlash(r.target),
}))

const existingRedirectsFromDb = await getRedirectsFromDb()
Expand Down

0 comments on commit bade737

Please sign in to comment.