mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem `apps/www/lib/redirects.js` was a single file with static and dynamic rules mixed together, and the redirects had no automated checks. `/docs` redirects already moved to `apps/docs/lib/redirects/`, so www was the odd one out. ## Solution I applied the docs layout to www without changing how redirects are served: - `apps/www/lib/redirects/static-redirects.json`: 220 one-to-one entries, including fixed redirects to external `https://` URLs. - `apps/www/lib/redirects/dynamic-redirects.js`: 14 entries with params, splats, regex, escapes, or `has`. - `apps/www/lib/redirects/index.js`: combines both (static first) for `next.config.mjs` and `scripts/generateMdContent.mjs`. - `apps/www/lib/redirects/redirects.test.ts`: checks for loops, chains, duplicate sources, invalid sources and destinations (anchors, missing `/` or `https://`), dynamic entries in the static file, and internal destinations that do not exist. - README for the folder, plus updates to `apps/www/README.md`, `DEVELOPERS.md`, `CODEOWNERS`, and the `www-tests` path filter and sparse checkout. Bulk redirects are not part of this PR: www has 234 redirects, well under the route limit, so they stay in `next.config.mjs`. ### Missing-destination check Internal destinations are checked against the same sources the site serves from: - `app/` and `pages/` routes, and `public/` files. - Blog, customer, event, and alternative slugs, derived the way `lib/posts.tsx` derives them. Events with `disable_page_build` do not count. - Feature and `/go` page slugs. - `.md` destinations must be a page that `generateMdContent.mjs` publishes. An existing HTML page is not enough. - `/docs/guides` destinations, against the docs content and guide routes. Other zones and build-generated files are not checked. A destination with params is probed with one sample value, so value-specific loops or chains are not caught. The README lists these limits. ### Behavior notes - The rule set matches `master`, with one exception: `/blog/2021/07/30/1-the-supabase-hackathon` now points at the canonical slug `/blog/1-the-supabase-hackathon`. The old destination `/blog/the-supabase-hackathon` also resolves today, because the blog route matches posts by filename suffix. The check requires canonical slugs. - First-match results on `supabase.com` are identical on 241 probe paths. - Static entries now precede dynamic ones. On the `supabase.design` host, 25 static sources that used to sit after its catch-all rule now come before it. Those paths now take two hops to reach the design system instead of one. - Comments on static entries were dropped, because JSON has no comments. ## To test 1. Open the www preview and visit `/about`. It redirects to `/company`. 2. Visit `/buildersday`. It redirects to `/builderday`. 3. Visit `/sign-in`. It redirects to the dashboard sign-in page. 4. Visit `/blog/2021/07/30/1-the-supabase-hackathon`. It lands on the hackathon post. ## Linear - fixes GROWTH-1571
292 lines
8.9 KiB
JavaScript
292 lines
8.9 KiB
JavaScript
// @ts-check
|
|
|
|
/**
|
|
* Scans content/md/ + _blog/ + _customers/ + _events/ and emits a TypeScript
|
|
* module exporting MD_CONTENT (slug → markdown) and MD_PAGES (allowlist Set).
|
|
* The static import keeps content traceable by @vercel/nft, no runtime fs reads.
|
|
*/
|
|
|
|
import { promises as fs } from 'fs'
|
|
import path from 'path'
|
|
import { fileURLToPath } from 'url'
|
|
import matter from 'gray-matter'
|
|
|
|
import redirects from '../lib/redirects/index.js'
|
|
import { mdxBodyToMarkdown } from './lib/mdxToMarkdown.mjs'
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
const wwwDir = path.join(__dirname, '..')
|
|
const contentDir = path.join(wwwDir, 'content/md')
|
|
const changelogMdDir = path.join(wwwDir, 'public/changelog')
|
|
const outputPath = path.join(wwwDir, 'app/api-v2/md/content.generated.ts')
|
|
|
|
// Matches lib/posts.tsx FILENAME_SUBSTRING — strips YYYY-MM-DD- (11 chars).
|
|
const DATE_PREFIX = 11
|
|
|
|
// Slugs handled by a dynamic generator in the route handler. Listed here so
|
|
// MD_PAGES still includes them (middleware relies on the allowlist).
|
|
const DYNAMIC_SLUGS = ['pricing']
|
|
|
|
function pickFields(data, fields) {
|
|
const picked = {}
|
|
for (const field of fields) {
|
|
const value = data[field]
|
|
if (value == null || value === '' || (Array.isArray(value) && value.length === 0)) continue
|
|
picked[field] = value
|
|
}
|
|
return picked
|
|
}
|
|
|
|
const MDX_SECTIONS = [
|
|
{
|
|
dir: '_blog',
|
|
urlPrefix: 'blog',
|
|
stripDatePrefix: true,
|
|
frontmatterFields: ['title', 'description', 'author', 'date', 'tags', 'categories'],
|
|
},
|
|
{
|
|
dir: '_customers',
|
|
urlPrefix: 'customers',
|
|
stripDatePrefix: false,
|
|
frontmatterFields: [
|
|
'name',
|
|
'title',
|
|
'description',
|
|
'company_url',
|
|
'industry',
|
|
'region',
|
|
'company_size',
|
|
'supabase_products',
|
|
'date',
|
|
],
|
|
},
|
|
{
|
|
dir: '_events',
|
|
urlPrefix: 'events',
|
|
stripDatePrefix: true,
|
|
frontmatterFields: [
|
|
'title',
|
|
'subtitle',
|
|
'description',
|
|
'type',
|
|
'date',
|
|
'end_date',
|
|
'timezone',
|
|
'duration',
|
|
'onDemand',
|
|
],
|
|
skipIf: (data) => data.disable_page_build === true,
|
|
},
|
|
]
|
|
|
|
async function collectMdFiles(dir, prefix = '') {
|
|
const results = []
|
|
let dirents
|
|
try {
|
|
dirents = await fs.readdir(dir, { withFileTypes: true })
|
|
} catch (err) {
|
|
if (err.code === 'ENOENT') return results
|
|
throw err
|
|
}
|
|
for (const dirent of dirents) {
|
|
const slug = prefix ? `${prefix}/${dirent.name}` : dirent.name
|
|
if (dirent.isDirectory()) {
|
|
results.push(...(await collectMdFiles(path.join(dir, dirent.name), slug)))
|
|
} else if (dirent.name.endsWith('.md')) {
|
|
results.push(slug.replace(/\.md$/, ''))
|
|
}
|
|
}
|
|
return results
|
|
}
|
|
|
|
// _events double-underscore filenames (`2024-08-30__launch-...`) produce slugs
|
|
// with a leading underscore. The HTML page is at `/events/_launch-...`, so the
|
|
// .md slug must keep the underscore to match.
|
|
function deriveSlug(filename, stripDatePrefix) {
|
|
const base = filename.replace(/\.mdx$/, '')
|
|
return stripDatePrefix ? base.substring(DATE_PREFIX) : base
|
|
}
|
|
|
|
async function ingestMdxSection(section) {
|
|
const dir = path.join(wwwDir, section.dir)
|
|
let filenames
|
|
try {
|
|
filenames = await fs.readdir(dir)
|
|
} catch (err) {
|
|
if (err.code === 'ENOENT') {
|
|
console.warn(` ⚠ Section directory missing: ${section.dir}`)
|
|
return []
|
|
}
|
|
throw err
|
|
}
|
|
|
|
const mdxFilenames = filenames.filter((f) => f.endsWith('.mdx'))
|
|
const results = await Promise.all(
|
|
mdxFilenames.map(async (filename) => {
|
|
const fullPath = path.join(dir, filename)
|
|
try {
|
|
const raw = await fs.readFile(fullPath, 'utf-8')
|
|
const parsed = matter(raw)
|
|
const data = parsed.data ?? {}
|
|
if (section.skipIf?.(data)) return null
|
|
|
|
const slug = `${section.urlPrefix}/${deriveSlug(filename, section.stripDatePrefix)}`
|
|
const body = await mdxBodyToMarkdown(parsed.content)
|
|
const content = matter.stringify(body, pickFields(data, section.frontmatterFields))
|
|
return { slug, content, data }
|
|
} catch (err) {
|
|
throw new Error(`${section.dir}/${filename}: ${err.message}`, { cause: err })
|
|
}
|
|
})
|
|
)
|
|
return results.filter((entry) => entry !== null)
|
|
}
|
|
|
|
function sortSlugs(a, b) {
|
|
if (a === 'index') return -1
|
|
if (b === 'index') return 1
|
|
return a.localeCompare(b)
|
|
}
|
|
|
|
const staticSlugs = (await collectMdFiles(contentDir)).sort(sortSlugs)
|
|
|
|
if (staticSlugs.length === 0) {
|
|
console.error('❌ No .md files found in content/md/')
|
|
process.exit(1)
|
|
}
|
|
|
|
const staticEntries = await Promise.all(
|
|
staticSlugs.map(async (slug) => ({
|
|
slug,
|
|
content: await fs.readFile(path.join(contentDir, `${slug}.md`), 'utf-8'),
|
|
}))
|
|
)
|
|
|
|
const mdxEntries = []
|
|
for (const section of MDX_SECTIONS) {
|
|
console.log(`📚 Ingesting ${section.dir}...`)
|
|
const sectionEntries = await ingestMdxSection(section)
|
|
console.log(` ${sectionEntries.length} entries`)
|
|
mdxEntries.push(...sectionEntries.map(({ slug, content }) => ({ slug, content })))
|
|
if (section.urlPrefix === 'customers') {
|
|
const links = sectionEntries
|
|
.sort((a, b) => String(b.data.date).localeCompare(String(a.data.date)))
|
|
.map((e) => `- [${e.data.name}](https://supabase.com/${e.slug}.md): ${e.data.title}`)
|
|
mdxEntries.push({
|
|
slug: 'customers',
|
|
content: `# Supabase Customer Stories\n\n${links.join('\n')}\n`,
|
|
})
|
|
}
|
|
}
|
|
|
|
const allEntries = [...staticEntries, ...mdxEntries]
|
|
|
|
const redirectedSlugs = new Set(
|
|
redirects
|
|
.filter((redirect) => !redirect.has && !redirect.missing && /^\/[^:*]+$/.test(redirect.source))
|
|
.map((redirect) => redirect.source.slice(1))
|
|
)
|
|
|
|
// The index slug is exempt: its HTML page is /, not /index, so a /index
|
|
// redirect never refers to the homepage and must not strip its markdown.
|
|
const liveEntries = allEntries.filter(
|
|
(entry) => entry.slug === 'index' || !redirectedSlugs.has(entry.slug)
|
|
)
|
|
const excludedSlugs = allEntries
|
|
.filter((entry) => entry.slug !== 'index' && redirectedSlugs.has(entry.slug))
|
|
.map((entry) => entry.slug)
|
|
if (excludedSlugs.length > 0) {
|
|
console.log(`🚫 Excluded ${excludedSlugs.length} redirected slugs: ${excludedSlugs.join(', ')}`)
|
|
}
|
|
|
|
if (!liveEntries.some((entry) => entry.slug === 'index')) {
|
|
console.error('❌ Missing content/md/index.md — middleware maps / to the index slug.')
|
|
process.exit(1)
|
|
}
|
|
|
|
const dynamicCollisions = staticSlugs.filter((s) => DYNAMIC_SLUGS.includes(s))
|
|
if (dynamicCollisions.length > 0) {
|
|
console.error(
|
|
`❌ Slug collision: [${dynamicCollisions.join(', ')}] reserved for a dynamic generator.`
|
|
)
|
|
process.exit(1)
|
|
}
|
|
|
|
const sectionRoots = MDX_SECTIONS.map((s) => s.urlPrefix)
|
|
const sectionRootCollisions = [...staticSlugs, ...DYNAMIC_SLUGS].filter((s) =>
|
|
sectionRoots.includes(s)
|
|
)
|
|
if (sectionRootCollisions.length > 0) {
|
|
console.error(
|
|
`❌ Section-root collision: [${sectionRootCollisions.join(', ')}] shadows an MDX section.`
|
|
)
|
|
process.exit(1)
|
|
}
|
|
|
|
const seen = new Set()
|
|
for (const entry of allEntries) {
|
|
if (seen.has(entry.slug)) {
|
|
console.error(`❌ Duplicate slug emitted: ${entry.slug}`)
|
|
process.exit(1)
|
|
}
|
|
seen.add(entry.slug)
|
|
}
|
|
|
|
// public/changelog/*.md is written by generateStaticContent.mjs earlier in
|
|
// content:build:core; absent locally e.g. when CHANGELOG_SYNC_APP_* secrets are unset.
|
|
const changelogSlugs = (await collectMdFiles(changelogMdDir, 'changelog')).sort()
|
|
|
|
// The middleware matches request slugs against these keys verbatim, and its
|
|
// tests mock this set — a dropped prefix would ship silently with green tests.
|
|
if (changelogSlugs.some((s) => !s.startsWith('changelog/'))) {
|
|
console.error('❌ Changelog slugs must carry the changelog/ prefix the middleware matches on.')
|
|
process.exit(1)
|
|
}
|
|
|
|
if (changelogSlugs.length === 0) {
|
|
if (process.env.VERCEL) {
|
|
console.error(
|
|
'❌ No changelog slugs found in public/changelog — changelog generation produced nothing.'
|
|
)
|
|
process.exit(1)
|
|
}
|
|
console.warn(
|
|
'⚠️ No changelog slugs found in public/changelog — changelog md negotiation off in this build'
|
|
)
|
|
}
|
|
|
|
let changelogIndexExists = false
|
|
try {
|
|
await fs.access(path.join(wwwDir, 'public/changelog.md'))
|
|
changelogIndexExists = true
|
|
} catch (err) {
|
|
if (err.code !== 'ENOENT') throw err
|
|
}
|
|
if (changelogIndexExists) {
|
|
changelogSlugs.unshift('changelog')
|
|
}
|
|
|
|
const contentEntries = liveEntries
|
|
.map((e) => ` [${JSON.stringify(e.slug)}, ${JSON.stringify(e.content)}]`)
|
|
.join(',\n')
|
|
|
|
const allPageSlugs = [...liveEntries.map((e) => e.slug), ...DYNAMIC_SLUGS]
|
|
const pageEntries = allPageSlugs.map((s) => ` ${JSON.stringify(s)}`).join(',\n')
|
|
|
|
const output = `// AUTO-GENERATED by scripts/generateMdContent.mjs — do not edit
|
|
export const MD_CONTENT = new Map<string, string>([
|
|
${contentEntries},
|
|
])
|
|
|
|
export const MD_PAGES = new Set<string>([
|
|
${pageEntries},
|
|
])
|
|
|
|
export const CHANGELOG_PAGES = new Set<string>(${JSON.stringify(changelogSlugs, null, 2)})
|
|
`
|
|
|
|
await fs.writeFile(outputPath, output, 'utf-8')
|
|
console.log(
|
|
`✅ Generated ${outputPath} (${liveEntries.length} files, ${allPageSlugs.length} pages, ${changelogSlugs.length} changelog)`
|
|
)
|