From fdb9d64644f1952ab32719e2599bfb130a18ef5f Mon Sep 17 00:00:00 2001 From: Matthieu Bergel Date: Fri, 9 Feb 2024 13:29:59 +0000 Subject: [PATCH 1/4] refactor: bake redirects from db --- baker/redirects.ts | 26 +++++++++++--------------- baker/syncRedirectsToGrapher.ts | 6 +++--- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/baker/redirects.ts b/baker/redirects.ts index 534601ebf11..d730792d0fe 100644 --- a/baker/redirects.ts +++ b/baker/redirects.ts @@ -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 = [ @@ -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 ] @@ -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 () => { @@ -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), ]) ) } diff --git a/baker/syncRedirectsToGrapher.ts b/baker/syncRedirectsToGrapher.ts index 25e903035fb..458551f2b26 100644 --- a/baker/syncRedirectsToGrapher.ts +++ b/baker/syncRedirectsToGrapher.ts @@ -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 @@ -17,8 +17,8 @@ export const syncRedirectsToGrapher = async (): Promise => { 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() From 0cbd766af2e38974d967c338a9de81f1967e8efd Mon Sep 17 00:00:00 2001 From: Matthieu Bergel Date: Fri, 9 Feb 2024 13:41:24 +0000 Subject: [PATCH 2/4] refactor: resolve internal redirects from db --- baker/redirects.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/baker/redirects.ts b/baker/redirects.ts index d730792d0fe..85938c3c15e 100644 --- a/baker/redirects.ts +++ b/baker/redirects.ts @@ -1,5 +1,4 @@ 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" @@ -101,14 +100,12 @@ export const stripTrailingSlash = (url: string) => { } 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) => [ - stripTrailingSlash(row.url), - stripTrailingSlash(row.action_data), + redirectsFromDb.map((row) => [ + stripTrailingSlash(row.source), + stripTrailingSlash(row.target), ]) ) } From 9d53fab7ef48bfa273725e47289394b0baad0cbb Mon Sep 17 00:00:00 2001 From: Matthieu Bergel Date: Sun, 18 Feb 2024 09:47:05 +0000 Subject: [PATCH 3/4] refactor(redirects): remove trailing slash processing --- baker/redirects.ts | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/baker/redirects.ts b/baker/redirects.ts index 85938c3c15e..8e4f50e96f6 100644 --- a/baker/redirects.ts +++ b/baker/redirects.ts @@ -58,11 +58,9 @@ export const getRedirects = async () => { ] // 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) => - `${stripTrailingSlash(row.source)} ${stripTrailingSlash( - row.target - )} ${row.code}` + (row) => `${row.source} ${row.target} ${row.code}` ) // Add newlines in between so we get some more overview @@ -102,12 +100,7 @@ export const stripTrailingSlash = (url: string) => { export const getWordpressRedirectsMap = async () => { const redirectsFromDb = await getRedirectsFromDb() - return new Map( - redirectsFromDb.map((row) => [ - stripTrailingSlash(row.source), - stripTrailingSlash(row.target), - ]) - ) + return new Map(redirectsFromDb.map((row) => [row.source, row.target])) } export const getGrapherAndWordpressRedirectsMap = memoize( From a7f9c83449abc21ea2038a670bd328e3a9f2f687 Mon Sep 17 00:00:00 2001 From: Matthieu Bergel Date: Sun, 18 Feb 2024 09:54:37 +0000 Subject: [PATCH 4/4] refactor(redirects): move stripTrailingSlash fn --- baker/redirects.ts | 6 ------ baker/syncRedirectsToGrapher.ts | 8 +++++++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/baker/redirects.ts b/baker/redirects.ts index 8e4f50e96f6..46872540763 100644 --- a/baker/redirects.ts +++ b/baker/redirects.ts @@ -91,12 +91,6 @@ export const getGrapherRedirectsMap = async ( ) } -export const stripTrailingSlash = (url: string) => { - if (url === "/") return url - - return url.replace(/\/$/, "") // remove trailing slash: /abc/ -> /abc -} - export const getWordpressRedirectsMap = async () => { const redirectsFromDb = await getRedirectsFromDb() diff --git a/baker/syncRedirectsToGrapher.ts b/baker/syncRedirectsToGrapher.ts index 458551f2b26..d92452bd720 100644 --- a/baker/syncRedirectsToGrapher.ts +++ b/baker/syncRedirectsToGrapher.ts @@ -1,7 +1,7 @@ import * as db from "../db/db" import * as wpdb from "../db/wpdb" import { getRedirectsFromDb } from "../db/model/Redirect.js" -import { stripTrailingSlash, resolveRedirectFromMap } from "./redirects.js" +import { resolveRedirectFromMap } from "./redirects.js" import { Redirect, Url } from "@ourworldindata/utils" // A close cousing of the getWordpressRedirectsMap() function from @@ -12,6 +12,12 @@ 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 => { const allWordpressRedirectsRaw = await wpdb.FOR_SYNC_ONLY_getRedirects()