Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Force redirect for notion redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchabran committed Jun 20, 2024
1 parent b81d623 commit e131edb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/scripts/generate-redirects.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import fs from 'fs/promises'

import redirects from './redirects.mjs'

const redirectLines = (await redirects()).map(({ source, destination }) => `${source} ${destination}`).join('\n')
const foo = await redirects()

const redirectLines = (await redirects()).map((entry) => `${entry.source} ${entry.destination}${entry.force ? ' 302!' : ''}`).join('\n')
const lines = `\n# Generated by generate-redirects.mjs from Git history\n${redirectLines}\n`

console.log('Redirects:\n', lines)
Expand Down
18 changes: 14 additions & 4 deletions src/scripts/redirects.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,21 @@ export function cleanupRedirects(movedPages) {
/**
* Reads Notion specific redirections from data/notion_migration.yaml.
*
* @returns {redirections: {source: string, destination: string}[]}
* @returns {redirections: {source: string, destination: string, force: true}[]}
*/
async function readNotionMigrationRedirects() {
return load(await readFile('data/notion_migration.yaml', 'utf8'))
const data = load(await readFile('data/notion_migration.yaml', 'utf8'))

// While NextJS is okay if content exists when setting up a redirection,
// Netlify doesn't: it will skip the redirection entirely if it finds
// existing content.
// To go around that, we add a new field 'force' that the script that
// generate the final _redirects file used by Netlify uses to append
// 301! on that entry, effectively forcing the redirection.
for ( let entry of data.redirections ) {
entry["force"] = true
}
return data
}

/**
Expand All @@ -46,12 +57,11 @@ export default async function redirects() {
const movedPages = await getMovedPagesFromHistory()
const notionRedirections = await readNotionMigrationRedirects()
return cleanupRedirects([
...notionRedirections.redirections,
...movedPages,
// Add custom redirects
{
source: '/careers',
destination: 'https://about.sourcegraph.com/jobs',
},
])
]).concat(notionRedirections.redirections) // we skip the cleanup because notion redirects are always going outside.
}

0 comments on commit e131edb

Please sign in to comment.