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

Commit 48b41eb

Browse files
committed
Fix redirections to Notion when deployed
1 parent 111ca71 commit 48b41eb

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

Diff for: data/notion_migration.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# - 'source': URL path to redirect, as currently visible on handbook.sourcegraph.com
33
# - 'destination': New URL top direct to. If a public 'sourcegraph.notion.site' page is available, prefer that instead of the internal Notion URL.
44
redirections:
5-
- source: /departments/engineering/teams/devinfra
5+
- source: /departments/engineering/teams/devinfra/
66
destination: https://sourcegraph.notion.site/Developer-Infrastructure-Team-a46433b93bb2445abc1966c93a570a26
77
# Core Services
88
- source: /departments/engineering/teams/core-services/

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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 redirectLines = (await redirects()).map((entry) => `${entry.source} ${entry.destination}${entry.force ? ' 301!' : ''}`).join('\n')
1111
const lines = `\n# Generated by generate-redirects.mjs from Git history\n${redirectLines}\n`
1212

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

Diff for: src/scripts/redirects.mjs

+14-5
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 ( const entry of data.redirections ) {
46+
entry.force = true
47+
}
48+
return data
3849
}
3950

4051
/**
@@ -47,12 +58,10 @@ export default async function redirects() {
4758
const notionRedirections = await readNotionMigrationRedirects()
4859
return cleanupRedirects([
4960
...movedPages,
50-
...notionRedirections.redirections,
51-
5261
// Add custom redirects
5362
{
5463
source: '/careers',
5564
destination: 'https://about.sourcegraph.com/jobs',
5665
},
57-
])
66+
]).concat(notionRedirections.redirections) // we skip the cleanup because notion redirects are always going outside.
5867
}

0 commit comments

Comments
 (0)