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

Commit e131edb

Browse files
committed
Force redirect for notion redirects
1 parent b81d623 commit e131edb

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Diff for: src/scripts/generate-redirects.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import fs from 'fs/promises'
77

88
import redirects from './redirects.mjs'
99

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

1315
console.log('Redirects:\n', lines)

Diff for: src/scripts/redirects.mjs

+14-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,21 @@ export function cleanupRedirects(movedPages) {
3131
/**
3232
* Reads Notion specific redirections from data/notion_migration.yaml.
3333
*
34-
* @returns {redirections: {source: string, destination: string}[]}
34+
* @returns {redirections: {source: string, destination: string, force: true}[]}
3535
*/
3636
async function readNotionMigrationRedirects() {
37-
return load(await readFile('data/notion_migration.yaml', 'utf8'))
37+
const data = load(await readFile('data/notion_migration.yaml', 'utf8'))
38+
39+
// While NextJS is okay if content exists when setting up a redirection,
40+
// Netlify doesn't: it will skip the redirection entirely if it finds
41+
// existing content.
42+
// To go around that, we add a new field 'force' that the script that
43+
// generate the final _redirects file used by Netlify uses to append
44+
// 301! on that entry, effectively forcing the redirection.
45+
for ( let entry of data.redirections ) {
46+
entry["force"] = true
47+
}
48+
return data
3849
}
3950

4051
/**
@@ -46,12 +57,11 @@ export default async function redirects() {
4657
const movedPages = await getMovedPagesFromHistory()
4758
const notionRedirections = await readNotionMigrationRedirects()
4859
return cleanupRedirects([
49-
...notionRedirections.redirections,
5060
...movedPages,
5161
// Add custom redirects
5262
{
5363
source: '/careers',
5464
destination: 'https://about.sourcegraph.com/jobs',
5565
},
56-
])
66+
]).concat(notionRedirections.redirections) // we skip the cleanup because notion redirects are always going outside.
5767
}

0 commit comments

Comments
 (0)