import type { NextConfig } from 'next' import { getPathMatch } from 'next/dist/shared/lib/router/utils/path-match' import { describe, expect, it, onTestFinished, vi } from 'vitest' vi.mock('@sentry/nextjs', () => ({ withSentryConfig: (configFn: any) => (typeof configFn === 'function' ? configFn() : configFn), })) describe('next.config.mjs', () => { it('expect the headers to always have X-Robots-Tag', async () => { const { default: config } = (await import('./next.config.mjs')) as { default: NextConfig } const headers = (await config.headers?.()) || [] expect(headers).toEqual( expect.arrayContaining([ expect.objectContaining({ source: '/(docs|blog|kb|library)/:path*', headers: [{ key: 'X-Robots-Tag', value: 'all' }], }), expect.objectContaining({ source: '/dashboard/:path((?!sign-in$|sign-up$).*)', headers: [{ key: 'X-Robots-Tag', value: 'noindex' }], }), ]) ) }) it('keeps sign-in and sign-up indexable while other dashboard paths stay noindex', async () => { const { default: config } = (await import('./next.config.mjs')) as { default: NextConfig } const headers = (await config.headers?.()) || [] const noindexRule = headers.find((header) => header.source.startsWith('/dashboard/')) const matches = getPathMatch(noindexRule!.source) expect(matches('/dashboard/sign-in')).toBe(false) expect(matches('/dashboard/sign-up')).toBe(false) expect(matches('/dashboard/org')).toBeTruthy() expect(matches('/dashboard/project/abc')).toBeTruthy() }) it('serves the published CLI schemas with CORS and a short cache', async () => { const { default: config } = (await import('./next.config.mjs')) as { default: NextConfig } const headers = (await config.headers?.()) || [] const schemasHeaders = headers.find((header) => header.source === '/schemas/:path*') expect(schemasHeaders?.headers).toEqual( expect.arrayContaining([ { key: 'access-control-allow-origin', value: '*' }, { key: 'cache-control', value: 'public, max-age=3600' }, ]) ) expect(getPathMatch(schemasHeaders!.source)('/schemas/config/v1.json')).toBeTruthy() }) 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 afterFiles = (rewrites && 'afterFiles' in rewrites && rewrites.afterFiles) || [] const redirects = (await config.redirects?.()) || [] expect(afterFiles).toEqual( expect.arrayContaining([ expect.objectContaining({ source: '/library' }), expect.objectContaining({ source: '/library/:path*' }), ]) ) expect(redirects).toEqual( expect.arrayContaining([ expect.objectContaining({ source: '/images/customers/logos/light/:path*', destination: '/images/customers/logos/on-dark/:path*', permanent: true, }), expect.objectContaining({ source: '/images/customers/logos/:slug((?!dreambase-mark\\.png)[^/.]+).png', destination: '/images/customers/logos/on-light/:slug.png', permanent: true, }), expect.objectContaining({ source: '/ui', destination: '/library', permanent: true }), expect.objectContaining({ source: '/ui/:path*', destination: '/library/:path*', permanent: true, }), ]) ) expect( redirects.findIndex((redirect) => redirect.source === '/ui/docs/ai-editors-rules/prompts') ).toBeLessThan(redirects.findIndex((redirect) => redirect.source === '/ui/:path*')) }) it('proxies the InstantDB migration app with its basePath prefix intact', async () => { const { default: config } = (await import('./next.config.mjs')) as { default: NextConfig } const rewrites = await config.rewrites?.() const afterFiles = (rewrites && 'afterFiles' in rewrites && rewrites.afterFiles) || [] expect(afterFiles).toEqual( expect.arrayContaining([ expect.objectContaining({ source: '/migrate/instantdb', destination: 'https://instantdb-to-supabase.vercel.app/migrate/instantdb', }), expect.objectContaining({ source: '/migrate/instantdb/:path*', destination: 'https://instantdb-to-supabase.vercel.app/migrate/instantdb/:path*', }), ]) ) }) it('proxies Builder Day with its basePath prefix intact', async () => { const { default: config } = (await import('./next.config.mjs')) as { default: NextConfig } const rewrites = await config.rewrites?.() const afterFiles = (rewrites && 'afterFiles' in rewrites && rewrites.afterFiles) || [] expect(afterFiles).toEqual( expect.arrayContaining([ expect.objectContaining({ source: '/builderday', destination: 'https://builderday.vercel.app/builderday', }), expect.objectContaining({ source: '/builderday/:path*', destination: 'https://builderday.vercel.app/builderday/:path*', }), ]) ) }) it('forwards zone prefetches as the page path, so each zone adds the rsc suffix once', async () => { // later tests must not get the production rewrites from the module cache onTestFinished(() => { vi.unstubAllEnvs() vi.resetModules() }) vi.resetModules() const zones = [ { zone: '/docs', env: 'NEXT_PUBLIC_DOCS_URL', url: 'https://docs.example.com/docs' }, { zone: '/library', env: 'NEXT_PUBLIC_LIBRARY_URL', url: 'https://library.example.com/library', }, { zone: '/design-system', env: 'NEXT_PUBLIC_DESIGN_SYSTEM_URL', url: 'https://design-system.example.com/design-system', }, ] vi.stubEnv('NEXT_PUBLIC_VERCEL_ENV', 'production') for (const { env, url } of zones) vi.stubEnv(env, url) const { default: rewrites } = (await import('./lib/rewrites.js')) as { default: Array<{ source: string; destination: string }> } // the first rule rewrites every path to itself, so routing goes on to the next match const forward = (path: string) => rewrites .filter((rule) => rule.source !== '/:path*') .map((rule) => ({ destination: rule.destination, // next matches custom routes with these options params: getPathMatch(rule.source, { strict: true, removeUnnamedParams: true })(path), })) .find((match) => match.params) for (const { zone, url } of zones) { expect( forward(`${zone}/guides/auth/architecture.segments/_tree.segment.rsc`), zone ).toMatchObject({ destination: `${url}/:path`, params: { path: 'guides/auth/architecture' }, }) expect(forward(`${zone}.segments/_tree.segment.rsc`), zone).toMatchObject({ destination: url, }) expect(forward(`${zone}.rsc`), zone).toMatchObject({ destination: url }) expect(forward(`${zone}/guides/auth/architecture`), zone).toMatchObject({ destination: `${url}/: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?.()) || [] for (const source of ['/.md', '/homepage.md', '/llms/homepage.txt']) { expect(redirects).toEqual( expect.arrayContaining([ expect.objectContaining({ source, destination: '/index.md', permanent: true }), ]) ) } expect(getPathMatch('/.md')('/.md')).toBeTruthy() expect(getPathMatch('/.md')('/foo.md')).toBe(false) expect(redirects).toEqual( expect.arrayContaining([ expect.objectContaining({ source: '/index', destination: '/', permanent: true }), ]) ) expect(getPathMatch('/index')('/index.md')).toBe(false) }) it('permanently redirects /docs.md to the docs markdown sitemap', async () => { const { default: config } = (await import('./next.config.mjs')) as { default: NextConfig } const redirects = (await config.redirects?.()) || [] expect(redirects).toEqual( expect.arrayContaining([ expect.objectContaining({ source: '/docs.md', destination: '/docs/markdown/sitemap.md', permanent: true, }), ]) ) }) it('preserves the filename when redirecting legacy customer logos', async () => { const { default: config } = (await import('./next.config.mjs')) as { default: NextConfig } const redirects = (await config.redirects?.()) || [] const darkModeRedirect = redirects.find( (redirect) => redirect.source === '/images/customers/logos/light/:path*' ) const lightModeRedirect = redirects.find( (redirect) => redirect.source === '/images/customers/logos/:slug((?!dreambase-mark\\.png)[^/.]+).png' ) expect(darkModeRedirect).toBeDefined() expect(lightModeRedirect).toBeDefined() const matchLegacyDark = getPathMatch(darkModeRedirect!.source) const matchLegacyLight = getPathMatch(lightModeRedirect!.source) expect(matchLegacyDark('/images/customers/logos/light/good-tape.png')).toEqual({ path: ['good-tape.png'], }) expect(matchLegacyLight('/images/customers/logos/good-tape.png')).toEqual({ slug: 'good-tape', }) expect(matchLegacyLight('/images/customers/logos/dreambase-mark.png')).toBe(false) expect(matchLegacyLight('/images/customers/logos/dreambase-marketing.png')).toEqual({ slug: 'dreambase-marketing', }) }) })