/** * Which URL paths the storefront middleware (`src/middleware.ts`) leaves alone, which it treats as a * static file, and which are pages. * * ⛔ This file imports NOTHING on purpose: `scripts/check-middleware-paths.mjs` loads it as plain * TypeScript in Node to run the rule against a table of URLs. Keep it that way. * * CF-027: the middleware used to skip ANY path containing a dot (`pathname.includes('.')`, and a * matcher with the same rule), so `/products/a.b` and `/account/orders/x.y` were rendered with no * Content-Security-Policy and no `/account` gate. A dot means "static file" only in three places: * * - `skip` `/api/*`, `/_next/*`, `/.well-known/*` (served by route handlers that must not be * rewritten under a locale) and a ROOT-level dotted file (`/llms.txt`, * `/indexnow-key.txt`, `/agents.md`, `/robots.txt`, `/favicon.ico`, anything at the top * of `public/`). No route of this template has a dotted name at one segment, and there * is no root-level dynamic segment, so nothing is lost. * - `static` a NESTED path whose LAST segment ends in a static extension (`/images/hero.jpg`, * `/blog/rss.xml`). It must not be locale-rewritten (the file and the rss handler live * at that exact path), but it still gets the CSP and the `/account` gate, because a * dynamic route can render HTML for `/products/x.json`. * - `page` everything else, dots included (`/products/a.b`). * * `middleware.ts.ejs` has to keep its `config.matcher` a string LITERAL (Next reads it statically), so * the matcher cannot import this. `check-middleware-paths.mjs` compiles the literal with Next's own * compiler and fails when it stops agreeing with `classifyPath`. */ export type PathKind = 'skip' | 'static' | 'page'; /** Extensions of files a storefront serves as-is. Deliberately has no `html` page, no `php`, no `aspx`. */ export const STATIC_EXTENSIONS = [ 'html', 'htm', 'css', 'js', 'mjs', 'map', 'json', 'yaml', 'yml', 'md', 'txt', 'xml', 'csv', 'pdf', 'jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'svg', 'ico', 'bmp', 'ttf', 'otf', 'woff', 'woff2', 'eot', 'mp3', 'wav', 'mp4', 'webm', 'mov', 'ogg', 'm4v', 'zip', 'gz', 'tgz', 'webmanifest', ] as const; const STATIC_EXTENSION = new RegExp(`\\.(?:${STATIC_EXTENSIONS.join('|')})$`, 'i'); const ROOT_FILE = /^\/[^/]+\.[^/]+$/; export function classifyPath(pathname: string): PathKind { if (pathname === '/api' || pathname.startsWith('/api/')) return 'skip'; if (pathname.startsWith('/_next/')) return 'skip'; if (pathname.startsWith('/.well-known/')) return 'skip'; if (ROOT_FILE.test(pathname)) return 'skip'; if (STATIC_EXTENSION.test(pathname)) return 'static'; return 'page'; }