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

refactor: bake redirects from db #3186

Merged
merged 4 commits into from
Feb 19, 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
36 changes: 8 additions & 28 deletions baker/redirects.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as db from "../db/db.js"
import * as wpdb from "../db/wpdb.js"
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 +57,17 @@ 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(
(row) =>
`${formatWpUrl(row.url)} ${formatWpUrl(row.action_data)} ${
row.action_code
}`
// Get redirects from the database (exported from the Wordpress DB)
// Redirects are assumed to be trailing-slash-free (see syncRedirectsToGrapher.ts)
const redirectsFromDb = (await getRedirectsFromDb()).map(
(row) => `${row.source} ${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,25 +91,10 @@ export const getGrapherRedirectsMap = async (
)
}

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

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

export const getWordpressRedirectsMap = async () => {
const wordpressRedirectRows = (await wpdb.singleton.query(
`SELECT url, action_data FROM wp_redirection_items WHERE status = 'enabled'`
)) as Array<{ url: string; action_data: string }>
const redirectsFromDb = await getRedirectsFromDb()

return new Map(
wordpressRedirectRows.map((row) => [
formatWpUrl(row.url),
formatWpUrl(row.action_data),
])
)
return new Map(redirectsFromDb.map((row) => [row.source, row.target]))
}

export const getGrapherAndWordpressRedirectsMap = memoize(
Expand Down
12 changes: 9 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 { resolveRedirectFromMap } from "./redirects.js"
import { Redirect, Url } from "@ourworldindata/utils"

// A close cousing of the getWordpressRedirectsMap() function from
Expand All @@ -12,13 +12,19 @@ const getWordpressRedirectsMapFromRedirects = (
return new Map(redirects.map((r) => [r.source, r.target]))
}

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

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

export const syncRedirectsToGrapher = async (): Promise<void> => {
const allWordpressRedirectsRaw = await wpdb.FOR_SYNC_ONLY_getRedirects()

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
Loading