Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(OK): canonicalize docs www to apex, exclude /.well-known (AASA) (#1704)

Andrew Mikofalvy7d1485b1cf61
diff --git a/docs/src/middleware.test.ts b/docs/src/middleware.test.ts
new file mode 100644
index 000000000..5710df65c
--- /dev/null
+++ b/docs/src/middleware.test.ts
@@ -0,0 +1,62 @@
+import { describe, expect, test } from 'bun:test';
+import { NextRequest } from 'next/server';
+import { middleware } from './middleware.ts';
+
+function run(host: string, pathAndQuery: string) {
+ const req = new NextRequest(`https://${host}${pathAndQuery}`, { headers: { host } });
+ return middleware(req);
+}
+
+const APEX = 'openknowledge.ai';
+const WWW = 'www.openknowledge.ai';
+const AASA = '/.well-known/apple-app-site-association';
+
+describe('middleware: www -> apex canonicalization', () => {
+ test('www + AASA passes through (no redirect) so Apple gets a direct 200', () => {
+ const res = run(WWW, AASA);
+ expect(res.headers.get('location')).toBeNull();
+ expect(res.status).toBe(200);
+ });
+
+ test('the whole /.well-known/* prefix is excluded on www, not just AASA', () => {
+ const res = run(WWW, '/.well-known/assetlinks.json');
+ expect(res.headers.get('location')).toBeNull();
+ expect(res.status).toBe(200);
+ });
+
+ test('www + a normal page redirects 308 to apex', () => {
+ const res = run(WWW, '/docs/get-started/quickstart');
+ expect(res.status).toBe(308);
+ expect(res.headers.get('location')).toBe(`https://${APEX}/docs/get-started/quickstart`);
+ });
+
+ test('www + root redirects 308 to apex root', () => {
+ const res = run(WWW, '/');
+ expect(res.status).toBe(308);
+ expect(res.headers.get('location')).toBe(`https://${APEX}/`);
+ });
+
+ test('www redirect preserves the query string', () => {
+ const res = run(WWW, '/d/abc123?ref=slack');
+ expect(res.status).toBe(308);
+ expect(res.headers.get('location')).toBe(`https://${APEX}/d/abc123?ref=slack`);
+ });
+
+ test('apex + a normal page is untouched (no redirect)', () => {
+ const res = run(APEX, '/docs/get-started/quickstart');
+ expect(res.headers.get('location')).toBeNull();
+ expect(res.status).toBe(200);
+ });
+
+ test('apex + AASA is untouched (the working host stays working)', () => {
+ const res = run(APEX, AASA);
+ expect(res.headers.get('location')).toBeNull();
+ expect(res.status).toBe(200);
+ });
+
+ test('preview/other hosts are not canonicalized to apex', () => {
+ const res = run('open-knowledge-git-feat.vercel.app', '/docs');
+ expect(res.headers.get('location')).toBeNull();
+ expect(res.status).toBe(200);
+ });
+});
diff --git a/docs/src/middleware.ts b/docs/src/middleware.ts
new file mode 100644
index 000000000..effbbcb30
--- /dev/null
+++ b/docs/src/middleware.ts
@@ -0,0 +1,24 @@
+import { type NextRequest, NextResponse } from 'next/server';
+
+const APEX_HOST = 'openknowledge.ai';
+const WWW_HOST = `www.${APEX_HOST}`;
+
+export function middleware(request: NextRequest): NextResponse {
+ const host = (request.headers.get('host') ?? request.nextUrl.host).split(':')[0];
+
+ if (request.nextUrl.pathname.startsWith('/.well-known/')) {
+ return NextResponse.next();
+ }
+
+ if (host === WWW_HOST) {
+ const url = request.nextUrl.clone();
+ url.hostname = APEX_HOST;
+ return NextResponse.redirect(url, 308);
+ }
+
+ return NextResponse.next();
+}
+
+export const config = {
+ matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
+};
diff --git a/knip.config.ts b/knip.config.ts
index 4e618aae7..676e4b77a 100644
--- a/knip.config.ts
+++ b/knip.config.ts
@@ -62,6 +62,7 @@ export default {
'packages/server/src/share/git-context.ts': ['types'],
'docs/src/app/d/[encoded]/opengraph-image.test.ts': ['files'],
'docs/src/lib/share-splash.test.ts': ['files'],
+ 'docs/src/middleware.test.ts': ['files'],
'packages/server/src/git-preflight.ts': ['exports'],
'packages/cli/src/commands/diagnose-health.ts': ['exports'],
'packages/cli/src/commands/diagnose-health-checks/git.ts': ['exports'],