rsnext/packages/next/server/require.ts
Javi Velasco f354f46b3f
Deprecate nested Middleware in favor of root middleware (#36772)
This PR deprecates declaring a middleware under `pages` in favour of the project root naming it after `middleware` instead of `_middleware`. This is in the context of having a simpler execution model for middleware and also ships some refactor work. There is a ton of a code to be simplified after this deprecation but I think it is best to do it progressively.

With this PR, when in development, we will **fail** whenever we find a nested middleware but we do **not** include it in the compiler so if the project is using it, it will no longer work. For production we will **fail** too so it will not be possible to build and deploy a deprecated middleware. The error points to a page that should also be reviewed as part of **documentation**.

Aside from the deprecation, this migrates all middleware tests to work with a single middleware. It also splits tests into multiple folders to make them easier to isolate and work with. Finally it ships some small code refactor and simplifications.
2022-05-19 15:46:21 +00:00

110 lines
2.7 KiB
TypeScript

import { promises } from 'fs'
import { join } from 'path'
import {
FONT_MANIFEST,
PAGES_MANIFEST,
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
VIEW_PATHS_MANIFEST,
} from '../shared/lib/constants'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
export function pageNotFoundError(page: string): Error {
const err: any = new Error(`Cannot find module for page: ${page}`)
err.code = 'ENOENT'
return err
}
export function getPagePath(
page: string,
distDir: string,
serverless: boolean,
dev?: boolean,
locales?: string[],
rootEnabled?: boolean
): string {
const serverBuildPath = join(
distDir,
serverless && !dev ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
let rootPathsManifest: undefined | PagesManifest
if (rootEnabled) {
if (page === '/_root') {
return join(serverBuildPath, 'root.js')
}
rootPathsManifest = require(join(serverBuildPath, VIEW_PATHS_MANIFEST))
}
const pagesManifest = require(join(
serverBuildPath,
PAGES_MANIFEST
)) as PagesManifest
try {
page = denormalizePagePath(normalizePagePath(page))
} catch (err) {
console.error(err)
throw pageNotFoundError(page)
}
const checkManifest = (manifest: PagesManifest) => {
let curPath = manifest[page]
if (!manifest[curPath] && locales) {
const manifestNoLocales: typeof pagesManifest = {}
for (const key of Object.keys(manifest)) {
manifestNoLocales[normalizeLocalePath(key, locales).pathname] =
pagesManifest[key]
}
curPath = manifestNoLocales[page]
}
return curPath
}
let pagePath: string | undefined
if (rootPathsManifest) {
pagePath = checkManifest(rootPathsManifest)
}
if (!pagePath) {
pagePath = checkManifest(pagesManifest)
}
if (!pagePath) {
throw pageNotFoundError(page)
}
return join(serverBuildPath, pagePath)
}
export function requirePage(
page: string,
distDir: string,
serverless: boolean,
rootEnabled?: boolean
): any {
const pagePath = getPagePath(
page,
distDir,
serverless,
false,
undefined,
rootEnabled
)
if (pagePath.endsWith('.html')) {
return promises.readFile(pagePath, 'utf8')
}
return require(pagePath)
}
export function requireFontManifest(distDir: string, serverless: boolean) {
const serverBuildPath = join(
distDir,
serverless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
const fontManifest = require(join(serverBuildPath, FONT_MANIFEST))
return fontManifest
}