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

Add site redirects for recent chart redirects #4568

Merged
merged 1 commit into from
Feb 18, 2025
Merged
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
32 changes: 32 additions & 0 deletions baker/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,48 @@ export const getRedirects = async (knex: db.KnexReadonlyTransaction) => {
(row) => `${row.source} ${row.target} ${row.code}`
)

const recentGrapherRedirects = (await getRecentGrapherRedirects(knex)).map(
(row) => `/grapher/${row.source} /grapher/${row.target} 302`
)

// Add newlines in between so we get some more overview
return [
...staticRedirects,
"",
...recentGrapherRedirects,
"",
...redirectsFromDb,
"",
...dynamicRedirects, // Cloudflare requires all dynamic redirects to be at the very end of the _redirects file
]
}

async function getRecentGrapherRedirects(knex: db.KnexReadonlyTransaction) {
// Prevent Cloudflare from serving outdated grapher pages, which can remain
// in the cache for up to a week. This is necessary, since we take into
// consideration the grapher redirects only when the route returns a 404,
// which won't be the case if the page is still cached.
//
// https://developers.cloudflare.com/pages/configuration/serving-pages/#asset-retention
return await db.knexRaw<{
source: string
target: string
}>(
knex,
`-- sql
SELECT
chart_slug_redirects.slug as source,
chart_configs.slug as target
FROM chart_slug_redirects
INNER JOIN charts ON charts.id=chart_id
INNER JOIN chart_configs ON chart_configs.id=charts.configId
WHERE
COALESCE(chart_slug_redirects.updatedAt, chart_slug_redirects.createdAt)
> (NOW() - INTERVAL 1 WEEK)
`
)
}

export const getGrapherRedirectsMap = async (
knex: db.KnexReadonlyTransaction,
urlPrefix: string = "/grapher/"
Expand Down
Loading