Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(www): markdown 404 for markdown-negotiated paths (#49596)

Pamela Chia164de2c347c3
diff --git a/apps/www/app/api-v2/md-404/[[...path]]/route.ts b/apps/www/app/api-v2/md-404/[[...path]]/route.ts
new file mode 100644
index 00000000000..4aa71ab7dd5
--- /dev/null
+++ b/apps/www/app/api-v2/md-404/[[...path]]/route.ts
@@ -0,0 +1,30 @@
+import { NextResponse } from 'next/server'
+
+const MARKDOWN_404_HEADERS = {
+ 'Content-Type': 'text/markdown; charset=utf-8',
+ 'X-Content-Type-Options': 'nosniff',
+ 'Cache-Control': 'no-store',
+ Vary: 'Accept',
+}
+
+function buildMarkdown404Body(requestPath: string): string {
+ const safePath = requestPath.replace(/[^\x20-\x7E]/g, '').replaceAll('`', '')
+ return `# 404 Not Found
+
+\`${safePath}\` does not exist on supabase.com.
+
+Explore instead:
+
+- [Documentation](https://supabase.com/docs)
+- [Sitemap](https://supabase.com/sitemap.xml)
+- [llms.txt](https://supabase.com/llms.txt)
+`
+}
+
+export async function GET(_request: Request, { params }: { params: Promise<{ path?: string[] }> }) {
+ const { path } = await params
+ return new NextResponse(buildMarkdown404Body('/' + (path ?? []).join('/')), {
+ status: 404,
+ headers: MARKDOWN_404_HEADERS,
+ })
+}
diff --git a/apps/www/next.config.mjs b/apps/www/next.config.mjs
index 9286c3c07af..0f4d2489277 100644
--- a/apps/www/next.config.mjs
+++ b/apps/www/next.config.mjs
@@ -80,7 +80,7 @@ const nextConfig = {
*/
outputFileTracingIncludes: {
'/llms.txt': ['../docs/content/guides/*'],
- '/llms-full.txt': ['../docs/public/markdown/guides/**/*.md' ],
+ '/llms-full.txt': ['../docs/public/markdown/guides/**/*.md'],
},
reactStrictMode: true,
images: {
@@ -194,7 +194,20 @@ const nextConfig = {
]
},
async rewrites() {
- return rewrites
+ return {
+ afterFiles: rewrites,
+ fallback: [
+ {
+ source: '/:path(.*\\.md)',
+ destination: '/api-v2/md-404/:path',
+ },
+ {
+ source: '/:path*',
+ has: [{ type: 'header', key: 'accept', value: '.*text/(markdown|\\*).*' }],
+ destination: '/api-v2/md-404/:path*',
+ },
+ ],
+ }
},
async redirects() {
// For /docs/guides/ redirects, auto-generate .md variants so renamed/deleted pages
diff --git a/apps/www/next.config.test.ts b/apps/www/next.config.test.ts
index e88f7141ece..083c7a260d5 100644
--- a/apps/www/next.config.test.ts
+++ b/apps/www/next.config.test.ts
@@ -27,10 +27,11 @@ describe('next.config.mjs', () => {
it('routes the library and permanently redirects the legacy UI URLs', async () => {
const { default: config } = (await import('./next.config.mjs')) as { default: NextConfig }
- const rewrites = (await config.rewrites?.()) || []
+ const rewrites = await config.rewrites?.()
+ const afterFiles = (rewrites && 'afterFiles' in rewrites && rewrites.afterFiles) || []
const redirects = (await config.redirects?.()) || []
- expect(rewrites).toEqual(
+ expect(afterFiles).toEqual(
expect.arrayContaining([
expect.objectContaining({ source: '/library' }),
expect.objectContaining({ source: '/library/:path*' }),
@@ -61,6 +62,33 @@ describe('next.config.mjs', () => {
).toBeLessThan(redirects.findIndex((redirect) => redirect.source === '/ui/:path*'))
})
+ it('routes unmatched markdown-negotiated paths to the md-404 handler via fallback rewrites', async () => {
+ const { default: config } = (await import('./next.config.mjs')) as { default: NextConfig }
+ const rewrites = await config.rewrites?.()
+ const fallback = (rewrites && 'fallback' in rewrites && rewrites.fallback) || []
+
+ const mdSuffixRule = fallback.find((rule) => rule.destination === '/api-v2/md-404/:path')
+ const acceptRule = fallback.find((rule) => rule.destination === '/api-v2/md-404/:path*')
+
+ expect(mdSuffixRule).toBeDefined()
+ expect(acceptRule?.has).toEqual([
+ { type: 'header', key: 'accept', value: '.*text/(markdown|\\*).*' },
+ ])
+
+ expect(getPathMatch(mdSuffixRule!.source)('/definitely-not-a-page.md')).toBeTruthy()
+ expect(getPathMatch(mdSuffixRule!.source)('/nested/definitely/not-a-page.md')).toBeTruthy()
+ expect(getPathMatch(mdSuffixRule!.source)('/definitely-not-a-page')).toBe(false)
+ expect(getPathMatch(acceptRule!.source)('/definitely-not-a-page')).toBeTruthy()
+
+ const acceptHeaderRegex = new RegExp(`^${acceptRule!.has![0].value}$`)
+ expect(acceptHeaderRegex.test('text/markdown')).toBe(true)
+ expect(acceptHeaderRegex.test('text/html, text/markdown;q=0.9')).toBe(true)
+ expect(acceptHeaderRegex.test('text/*')).toBe(true)
+ expect(
+ acceptHeaderRegex.test('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
+ ).toBe(false)
+ })
+
it('permanently redirects the legacy root markdown aliases to /index.md', async () => {
const { default: config } = (await import('./next.config.mjs')) as { default: NextConfig }
const redirects = (await config.redirects?.()) || []