39 lines
2.0 KiB
TypeScript
39 lines
2.0 KiB
TypeScript
/**
|
|
* The documented proxy matcher, compiled by Next's own code (the function `next build` uses for `config.matcher`):
|
|
* `api`, `admin` and `_next` are skipped only as a whole first segment. Before 1.17.1 `/administracja`, `/admin-guide`
|
|
* or `/apis` skipped the proxy as well (IAWL idea 36: a deny rule by name must end at "/").
|
|
*/
|
|
import { readFileSync, realpathSync } from 'node:fs'
|
|
import { createRequire } from 'node:module'
|
|
import path from 'node:path'
|
|
import { describe, expect, test } from 'vitest'
|
|
|
|
import { DEFAULT_PROXY_MATCHER } from '../src/modules/i18n/createProxy.js'
|
|
|
|
const root = path.resolve(import.meta.dirname, '..')
|
|
const next = realpathSync(path.join(root, 'node_modules/next'))
|
|
const { getMiddlewareMatchers } = createRequire(import.meta.url)(path.join(next, 'dist/build/analysis/get-page-static-info.js')) as {
|
|
getMiddlewareMatchers: (matchers: string[], nextConfig: object) => { regexp: string }[]
|
|
}
|
|
const runs = (matcher: string) => {
|
|
const re = new RegExp(getMiddlewareMatchers([matcher], {})[0].regexp)
|
|
return (pathname: string) => re.test(pathname)
|
|
}
|
|
|
|
describe('the proxy matcher', () => {
|
|
test('skips /api, /admin and /_next as whole segments and files; runs on pages that only START with those letters', () => {
|
|
const proxy = runs(DEFAULT_PROXY_MATCHER[0])
|
|
for (const skipped of ['/api', '/api/users', '/api/ipal/hit', '/admin', '/admin/', '/admin/collections/users', '/_next/static/chunk.js', '/robots.txt', '/sitemap.xml']) {
|
|
expect([skipped, proxy(skipped)]).toEqual([skipped, false])
|
|
}
|
|
for (const page of ['/', '/o-nas', '/pl/o-nas', '/administracja', '/admin-guide', '/apis', '/api-docs', '/_nextish', '/md/pl/o-nas']) {
|
|
expect([page, proxy(page)]).toEqual([page, true])
|
|
}
|
|
})
|
|
|
|
test('the reference site uses the same literal (Next reads it statically, it cannot be imported)', () => {
|
|
const proxyFile = readFileSync(path.join(root, 'reference/src/proxy.ts'), 'utf8')
|
|
expect(proxyFile).toContain(`matcher: ['${DEFAULT_PROXY_MATCHER[0].replace(/\\/g, '\\\\')}']`)
|
|
})
|
|
})
|