Unverified Commit 36ca1598 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Support static file robots.txt and sitemap.xml as metadata route (#46963)

Support top level static `robots.txt` and `sitemap.xml` as metadata
route in app directory. When those files are placed in top root
directory

Refactored a bit the page files matching logic, to reuse it between dev
server and build

Closes NEXT-267
parent 715f96fb
......@@ -132,8 +132,9 @@ import {
import { webpackBuild } from './webpack-build'
import { NextBuildContext } from './build-context'
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
import { isAppRouteRoute } from '../lib/is-app-route-route'
import { isAppRouteRoute, isMetadataRoute } from '../lib/is-app-route-route'
import { createClientRouterFilter } from '../lib/create-client-router-filter'
import { createValidFileMatcher } from '../server/lib/find-page-file'
export type SsgRoute = {
initialRevalidateSeconds: number | false
......@@ -491,15 +492,17 @@ export default async function build(
NextBuildContext.buildSpinner = buildSpinner
const validFileMatcher = createValidFileMatcher(
config.pageExtensions,
appDir
)
const pagesPaths =
!appDirOnly && pagesDir
? await nextBuildSpan
.traceChild('collect-pages')
.traceAsyncFn(() =>
recursiveReadDir(
pagesDir,
new RegExp(`\\.(?:${config.pageExtensions.join('|')})$`)
)
recursiveReadDir(pagesDir, validFileMatcher.isPageFile)
)
: []
......@@ -509,12 +512,7 @@ export default async function build(
appPaths = await nextBuildSpan
.traceChild('collect-app-paths')
.traceAsyncFn(() =>
recursiveReadDir(
appDir,
new RegExp(
`^(page|route)\\.(?:${config.pageExtensions.join('|')})$`
)
)
recursiveReadDir(appDir, validFileMatcher.isAppRouterPage)
)
}
......@@ -2442,7 +2440,9 @@ export default async function build(
appConfig.revalidate === 0 ||
exportConfig.initialPageRevalidationMap[page] === 0
const isRouteHandler = isAppRouteRoute(originalAppPath)
const isRouteHandler =
isAppRouteRoute(originalAppPath) ||
isMetadataRoute(originalAppPath)
routes.forEach((route) => {
if (isDynamicRoute(page) && route === page) return
......
......@@ -1708,6 +1708,7 @@ export default async function getBaseWebpackConfig(
'next-app-loader',
'next-font-loader',
'next-invalid-import-error-loader',
'next-metadata-route-loader',
].reduce((alias, loader) => {
// using multiple aliases to replace `resolveLoader.modules`
alias[loader] = path.join(__dirname, 'webpack', 'loaders', loader)
......
......@@ -10,9 +10,26 @@ import { verifyRootLayout } from '../../../lib/verifyRootLayout'
import * as Log from '../../../build/output/log'
import { APP_DIR_ALIAS } from '../../../lib/constants'
import { buildMetadata, discoverStaticMetadataFiles } from './metadata/discover'
import {
isAppRouteRoute,
isMetadataRoute,
} from '../../../lib/is-app-route-route'
export type AppLoaderOptions = {
name: string
pagePath: string
appDir: string
appPaths: string[] | null
pageExtensions: string[]
basePath: string
assetPrefix: string
rootDir?: string
tsconfigPath?: string
isDev?: boolean
}
type AppLoader = webpack.LoaderDefinitionFunction<AppLoaderOptions>
const isNotResolvedError = (err: any) => err.message.includes("Can't resolve")
import { isAppRouteRoute } from '../../../lib/is-app-route-route'
const FILE_TYPES = {
layout: 'layout',
......@@ -39,21 +56,29 @@ export type ComponentsType = {
}
async function createAppRouteCode({
name,
pagePath,
resolver,
}: {
name: string
pagePath: string
resolver: PathResolver
}): Promise<string> {
// Split based on any specific path separators (both `/` and `\`)...
const routeName = name.split('/').pop()!
const splittedPath = pagePath.split(/[\\/]/)
// Then join all but the last part with the same separator, `/`...
const segmentPath = splittedPath.slice(0, -1).join('/')
// Then add the `/route` suffix...
const matchedPagePath = `${segmentPath}/route`
const matchedPagePath = `${segmentPath}/${routeName}`
// This, when used with the resolver will give us the pathname to the built
// route handler file.
const resolvedPagePath = await resolver(matchedPagePath)
let resolvedPagePath = (await resolver(matchedPagePath))!
if (isMetadataRoute(name)) {
resolvedPagePath = `next-metadata-route-loader!${resolvedPagePath}`
}
// TODO: verify if other methods need to be injected
// TODO: validate that the handler exports at least one of the supported methods
......@@ -249,20 +274,6 @@ function createAbsolutePath(appDir: string, pathToTurnAbsolute: string) {
)
}
export type AppLoaderOptions = {
name: string
pagePath: string
appDir: string
appPaths: string[] | null
pageExtensions: string[]
basePath: string
assetPrefix: string
rootDir?: string
tsconfigPath?: string
isDev?: boolean
}
type AppLoader = webpack.LoaderDefinitionFunction<AppLoaderOptions>
const nextAppLoader: AppLoader = async function nextAppLoader() {
const loaderOptions = this.getOptions() || {}
const {
......@@ -283,10 +294,12 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
}
const extensions = pageExtensions.map((extension) => `.${extension}`)
const resolveOptions: any = {
...NODE_RESOLVE_OPTIONS,
extensions,
}
const resolve = this.getResolve(resolveOptions)
const normalizedAppPaths =
......@@ -344,8 +357,8 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
}
}
if (isAppRouteRoute(name)) {
return createAppRouteCode({ pagePath, resolver })
if (isAppRouteRoute(name) || isMetadataRoute(name)) {
return createAppRouteCode({ name, pagePath, resolver })
}
const {
......
import type webpack from 'webpack'
import path from 'path'
const staticFileRegex = /[\\/](robots\.txt|sitemap\.xml)/
function isStaticRoute(resourcePath: string) {
return staticFileRegex.test(resourcePath)
}
function getContentType(resourcePath: string) {
const filename = path.basename(resourcePath)
const [name] = filename.split('.')
if (name === 'sitemap') return 'application/xml'
if (name === 'robots') return 'text/plain'
return 'text/plain'
}
const nextMetadataRouterLoader: webpack.LoaderDefinitionFunction = function (
content: string
) {
const { resourcePath } = this
const code = isStaticRoute(resourcePath)
? `import { NextResponse } from 'next/server'
const content = ${JSON.stringify(content)}
const contentType = ${JSON.stringify(getContentType(resourcePath))}
export function GET() {
return new NextResponse(content, {
status: 200,
headers: {
'Content-Type': contentType,
},
})
}
export const dynamic = 'force-static'
`
: // TODO: handle the defined configs in routes file
`export { default as GET } from ${JSON.stringify(resourcePath)}`
return code
}
export default nextMetadataRouterLoader
......@@ -31,7 +31,10 @@ import {
} from '../loaders/utils'
import { traverseModules } from '../utils'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
import { isAppRouteRoute } from '../../../lib/is-app-route-route'
import {
isAppRouteRoute,
isMetadataRoute,
} from '../../../lib/is-app-route-route'
import { getProxiedPluginState } from '../../build-context'
interface Options {
......@@ -181,7 +184,8 @@ export class FlightClientEntryPlugin {
if (
name.startsWith('pages/') ||
// Skip for route.js entries
(name.startsWith('app/') && isAppRouteRoute(name))
(name.startsWith('app/') &&
(isAppRouteRoute(name) || isMetadataRoute(name)))
) {
continue
}
......
......@@ -40,7 +40,7 @@ import { mockRequest } from '../server/lib/mock-request'
import { RouteKind } from '../server/future/route-kind'
import { NodeNextRequest, NodeNextResponse } from '../server/base-http/node'
import { StaticGenerationContext } from '../server/future/route-handlers/app-route-route-handler'
import { isAppRouteRoute } from '../lib/is-app-route-route'
import { isAppRouteRoute, isMetadataRoute } from '../lib/is-app-route-route'
loadRequireHook()
......@@ -167,7 +167,8 @@ export default async function exportPage({
let renderAmpPath = ampPath
let query = { ...originalQuery }
let params: { [key: string]: string | string[] } | undefined
const isRouteHandler = isAppDir && isAppRouteRoute(page)
const isRouteHandler =
isAppDir && (isAppRouteRoute(page) || isMetadataRoute(page))
if (isAppDir) {
outDir = join(distDir, 'server/app')
......
export function isAppRouteRoute(route: string): boolean {
return route.endsWith('/route')
}
// TODO: support more metadata routes
const staticMetadataRoutes = ['robots.txt', 'sitemap.xml']
export function isMetadataRoute(route: string): boolean {
// Remove the 'app/' or '/' prefix, only check the route name since they're only allowed in root app directory
const filename = route.replace(/^app\//, '').replace(/^\//, '')
return staticMetadataRoutes.includes(filename)
}
......@@ -8,10 +8,10 @@ import { join } from 'path'
export async function recursiveReadDir(
/** Directory to read */
dir: string,
/** Filter for the file name, only the name part is considered, not the full path */
filter: RegExp,
/** Filter for the file name, only the name part is considered, not the full path */
ignore?: RegExp,
/** Filter for the file path */
filter: (absoluteFilePath: string) => boolean,
/** Filter for the file path */
ignore?: (absoluteFilePath: string) => boolean,
/** This doesn't have to be provided, it's used for the recursion */
arr: string[] = [],
/** Used to replace the initial path, only the relative path is left, it's faster than path.relative. */
......@@ -22,7 +22,8 @@ export async function recursiveReadDir(
await Promise.all(
result.map(async (part: Dirent) => {
const absolutePath = join(dir, part.name)
if (ignore && ignore.test(part.name)) return
const relativePath = absolutePath.replace(rootDir, '')
if (ignore && ignore(absolutePath)) return
// readdir does not follow symbolic links
// if part is a symbolic link, follow it using stat
......@@ -37,11 +38,11 @@ export async function recursiveReadDir(
return
}
if (!filter.test(part.name)) {
if (!filter(absolutePath)) {
return
}
arr.push(absolutePath.replace(rootDir, ''))
arr.push(relativePath)
})
)
......
......@@ -29,11 +29,13 @@ export async function getTypeScriptIntent(
// project for the user when we detect TypeScript files. So, we need to check
// the `pages/` directory for a TypeScript file.
// Checking all directories is too slow, so this is a happy medium.
const tsFilesRegex = /.*\.(ts|tsx)$/
const excludedRegex = /(node_modules|.*\.d\.ts$)/
for (const dir of intentDirs) {
const typescriptFiles = await recursiveReadDir(
dir,
/.*\.(ts|tsx)$/,
/(node_modules|.*\.d\.ts)/
(name) => tsFilesRegex.test(name),
(name) => excludedRegex.test(name)
)
if (typescriptFiles.length) {
return { firstTimeSetup: true }
......
......@@ -48,7 +48,7 @@ import { eventCliSession } from '../../telemetry/events'
import { Telemetry } from '../../telemetry/storage'
import { setGlobal } from '../../trace'
import HotReloader from './hot-reloader'
import { findPageFile, isLayoutsLeafPage } from '../lib/find-page-file'
import { createValidFileMatcher, findPageFile } from '../lib/find-page-file'
import { getNodeOptionsWithoutInspect } from '../lib/utils'
import {
UnwrapPromise,
......@@ -358,8 +358,9 @@ export default class DevServer extends Server {
return
}
const regexPageExtension = new RegExp(
`\\.+(?:${this.nextConfig.pageExtensions.join('|')})$`
const validFileMatcher = createValidFileMatcher(
this.nextConfig.pageExtensions,
this.appDir
)
let resolved = false
......@@ -474,7 +475,7 @@ export default class DevServer extends Server {
if (
meta?.accuracy === undefined ||
!regexPageExtension.test(fileName)
!validFileMatcher.isPageFile(fileName)
) {
continue
}
......@@ -543,7 +544,7 @@ export default class DevServer extends Server {
}
if (isAppPath) {
if (!isLayoutsLeafPage(fileName, this.nextConfig.pageExtensions)) {
if (!validFileMatcher.isAppRouterPage(fileName)) {
continue
}
......
......@@ -337,8 +337,8 @@ export class AppRouteRouteHandler implements RouteHandler<AppRouteRouteMatch> {
RequestStore,
RequestContext
> = new RequestAsyncStorageWrapper(),
private readonly staticAsyncLocalStorageWrapper = new StaticGenerationAsyncStorageWrapper(),
private readonly moduleLoader: ModuleLoader = new NodeModuleLoader()
protected readonly staticAsyncLocalStorageWrapper = new StaticGenerationAsyncStorageWrapper(),
protected readonly moduleLoader: ModuleLoader = new NodeModuleLoader()
) {}
private resolve(method: string, mod: AppRouteModule): AppRouteHandlerFn {
......@@ -427,12 +427,12 @@ export class AppRouteRouteHandler implements RouteHandler<AppRouteRouteMatch> {
// TODO-APP: this is temporarily used for edge.
request?: Request
): Promise<Response> {
// This is added by the webpack loader, we load it directly from the module.
const { requestAsyncStorage, staticGenerationAsyncStorage } = module
// Get the handler function for the given method.
const handle = this.resolve(req.method, module)
// This is added by the webpack loader, we load it directly from the module.
const { requestAsyncStorage, staticGenerationAsyncStorage } = module
const requestContext: RequestContext =
process.env.NEXT_RUNTIME === 'edge'
? { req, res }
......
......@@ -13,7 +13,7 @@ export const enum RouteKind {
*/
APP_PAGE = 'APP_PAGE',
/**
* `APP_ROUTE` represents all the API routes that are under `app/` with the
* `APP_ROUTE` represents all the API routes and metadata routes that are under `app/` with the
* filename of `route.{j,t}s{,x}`.
*/
APP_ROUTE = 'APP_ROUTE',
......
import path from '../../../shared/lib/isomorphic/path'
import { isAppRouteRoute } from '../../../lib/is-app-route-route'
import {
isAppRouteRoute,
isMetadataRoute,
} from '../../../lib/is-app-route-route'
import {
APP_PATHS_MANIFEST,
SERVER_DIRECTORY,
......@@ -25,7 +28,9 @@ export class AppRouteRouteMatcherProvider extends ManifestRouteMatcherProvider<A
manifest: Manifest
): Promise<ReadonlyArray<AppRouteRouteMatcher>> {
// This matcher only matches app routes.
const pages = Object.keys(manifest).filter((page) => isAppRouteRoute(page))
const pages = Object.keys(manifest).filter(
(page) => isAppRouteRoute(page) || isMetadataRoute(page)
)
// Format the routes.
const matchers: Array<AppRouteRouteMatcher> = []
......
......@@ -24,9 +24,17 @@ export class DevAppRouteRouteMatcherProvider extends FileCacheRouteMatcherProvid
) {
super(appDir, reader)
// Match any route file that ends with `/route.${extension}` under the app
// directory.
this.expression = new RegExp(`[/\\\\]route\\.(?:${extensions.join('|')})$`)
// Match any route file that ends with `/route.${extension}` under the app directory.
// Match top level robots file that ends with `/robots.${extension}` under the app directory.
this.expression = new RegExp(
`[/\\\\]route\\.(?:${extensions.join(
'|'
)})$|[/\\\\]robots\\.(?:${extensions
.concat('txt')
.join('|')})?$|[/\\\\]sitemap\\.(?:${extensions
.concat('xml')
.join('|')})?$`
)
const pageNormalizer = new AbsoluteFilenameNormalizer(appDir, extensions)
......
......@@ -68,10 +68,53 @@ export async function findPageFile(
return existingPath
}
// Determine if the file is leaf node page file under layouts,
// The filename should start with 'page' and end with one of the allowed extensions
export function isLayoutsLeafPage(filePath: string, pageExtensions: string[]) {
return new RegExp(
`(^page|[\\\\/]page|^route|[\\\\/]route)\\.(?:${pageExtensions.join('|')})$`
).test(filePath)
/**
*
* createValidFileMatcher receives configured page extensions and return helpers to determine:
* `isLayoutsLeafPage`: if a file is a valid page file or routes file under app directory
* `isTrackedFiles`: if it's a tracked file for webpack watcher
*
*/
export function createValidFileMatcher(
pageExtensions: string[],
appDirPath: string | undefined
) {
const validExtensionFileRegex = new RegExp(
`\\.+(?:${pageExtensions.join('|')})$`
)
const leafOnlyPageFileRegex = new RegExp(
`(^(page|route)|[\\\\/](page|route))\\.(?:${pageExtensions.join('|')})$`
)
// TODO: support other metadata routes
// regex for /robots.txt|((j|t)sx?)
// regex for /sitemap.xml|((j|t)sx?)
const metadataRoutesRelativePathRegex = new RegExp(
`^[\\\\/](robots)\\.(?:${pageExtensions.concat('txt').join('|')})$` +
'|' +
`^[\\\\/](sitemap)\\.(?:${pageExtensions.concat('xml').join('|')})$`
)
function isMetadataRouteFile(filePath: string) {
if (!appDirPath) return false
const relativePath = filePath.replace(appDirPath, '')
return metadataRoutesRelativePathRegex.test(relativePath)
}
// Determine if the file is leaf node page file or route file under layouts,
// 'page.<extension>' | 'route.<extension>'
function isAppRouterPage(filePath: string) {
return leafOnlyPageFileRegex.test(filePath) || isMetadataRouteFile(filePath)
}
function isPageFile(filePath: string) {
return (
validExtensionFileRegex.test(filePath) || isMetadataRouteFile(filePath)
)
}
return {
isPageFile,
isAppRouterPage,
isMetadataRouteFile,
}
}
......@@ -1277,6 +1277,10 @@ export default class NextNodeServer extends BaseServer {
)
if (handled) return { finished: true }
}
// else if (match.definition.kind === RouteKind.METADATA_ROUTE) {
// handled = await this.handlers.handle(match, req, res)
// if (handled) return { finished: true }
// }
}
try {
......
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://vercel.com/</loc>
<lastmod>2023-03-06T18:04:14.008Z</lastmod>
</url>
</urlset>
# This robots.txt not in root app dir should never be triggered
User-Agent: *
Disallow:
Disallow: /pocket
Allow: /blog/*
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://vercel.com/</loc>
<lastmod>2023-03-06T18:04:14.008Z</lastmod>
</url>
</urlset>
......@@ -644,6 +644,28 @@ createNextDescribe(
})
})
describe('static routes', () => {
it('should support root dir robots.txt', async () => {
const res = await next.fetch('/robots.txt')
expect(res.headers.get('content-type')).toBe('text/plain')
expect(await res.text()).toContain('User-Agent: *\nDisallow:')
const invalidRobotsResponse = await next.fetch('/title/robots.txt')
expect(invalidRobotsResponse.status).toBe(404)
})
it('should support root dir sitemap.xml', async () => {
const res = await next.fetch('/sitemap.xml')
expect(res.headers.get('content-type')).toBe('application/xml')
const sitemap = await res.text()
expect(sitemap).toContain('<?xml version="1.0" encoding="UTF-8"?>')
expect(sitemap).toContain(
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
)
const invalidSitemapResponse = await next.fetch('/title/sitemap.xml')
expect(invalidSitemapResponse.status).toBe(404)
})
})
describe('react cache', () => {
it('should have same title and page value on initial load', async () => {
const browser = await next.browser('/cache-deduping')
......
......@@ -187,9 +187,8 @@ describe('Build Output', () => {
})
it('should not emit extracted comments', async () => {
const files = await recursiveReadDir(
join(appDir, '.next'),
/\.txt|\.LICENSE\./
const files = await recursiveReadDir(join(appDir, '.next'), (f) =>
/\.txt|\.LICENSE\./.test(f)
)
expect(files).toEqual([])
})
......
......@@ -10,7 +10,7 @@ function runTests() {
it('includes sourcemaps for all browser files', async () => {
const browserFiles = await recursiveReadDir(
join(appDir, '.next', 'static'),
/.*/
(f) => /.*/.test(f)
)
const jsFiles = browserFiles.filter(
(file) => file.endsWith('.js') && file.includes('/pages/')
......
......@@ -645,13 +645,13 @@ describe('Production Usage', () => {
const cssStaticAssets = await recursiveReadDir(
join(__dirname, '..', '.next', 'static'),
/\.css$/
(f) => /\.css$/.test(f)
)
expect(cssStaticAssets.length).toBeGreaterThanOrEqual(1)
expect(cssStaticAssets[0]).toMatch(/[\\/]css[\\/]/)
const mediaStaticAssets = await recursiveReadDir(
join(__dirname, '..', '.next', 'static'),
/\.svg$/
(f) => /\.svg$/.test(f)
)
expect(mediaStaticAssets.length).toBeGreaterThanOrEqual(1)
expect(mediaStaticAssets[0]).toMatch(/[\\/]media[\\/]/)
......
......@@ -98,7 +98,9 @@ module.exports = (context) => {
const buildId = readFileSync(join(__dirname, '../.next/BUILD_ID'), 'utf8')
const readPath = join(__dirname, `../.next/static/${buildId}`)
const buildFiles = await recursiveReadDir(readPath, /\.js$/)
const buildFiles = await recursiveReadDir(readPath, (f) =>
/\.js$/.test(f)
)
if (buildFiles.length < 1) {
throw new Error('Could not locate any build files')
......
/* eslint-env jest */
import {
findPageFile,
isLayoutsLeafPage,
createValidFileMatcher,
} from 'next/dist/server/lib/find-page-file'
import { normalizePagePath } from 'next/dist/shared/lib/page-path/normalize-page-path'
......@@ -45,26 +45,46 @@ describe('findPageFile', () => {
})
})
describe('isLayoutsLeafPage', () => {
const pageExtensions = ['tsx', 'ts', 'jsx', 'js']
it('should determine either server or client component page file as leaf node page', () => {
expect(isLayoutsLeafPage('page.js', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('./page.js', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('./page.jsx', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('/page.ts', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('/path/page.tsx', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('\\path\\page.tsx', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('.\\page.jsx', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('\\page.js', pageExtensions)).toBe(true)
describe('createPageFileMatcher', () => {
describe('isAppRouterPage', () => {
const pageExtensions = ['tsx', 'ts', 'jsx', 'js']
const fileMatcher = createValidFileMatcher(pageExtensions, '')
it('should determine either server or client component page file as leaf node page', () => {
expect(fileMatcher.isAppRouterPage('page.js')).toBe(true)
expect(fileMatcher.isAppRouterPage('./page.js')).toBe(true)
expect(fileMatcher.isAppRouterPage('./page.jsx')).toBe(true)
expect(fileMatcher.isAppRouterPage('/page.ts')).toBe(true)
expect(fileMatcher.isAppRouterPage('/path/page.tsx')).toBe(true)
expect(fileMatcher.isAppRouterPage('\\path\\page.tsx')).toBe(true)
expect(fileMatcher.isAppRouterPage('.\\page.jsx')).toBe(true)
expect(fileMatcher.isAppRouterPage('\\page.js')).toBe(true)
})
it('should determine other files under layout routes as non leaf node', () => {
expect(fileMatcher.isAppRouterPage('./not-a-page.js')).toBe(false)
expect(fileMatcher.isAppRouterPage('not-a-page.js')).toBe(false)
expect(fileMatcher.isAppRouterPage('./page.component.jsx')).toBe(false)
expect(fileMatcher.isAppRouterPage('layout.js')).toBe(false)
expect(fileMatcher.isAppRouterPage('page')).toBe(false)
})
})
it('should determine other files under layout routes as non leaf node', () => {
expect(isLayoutsLeafPage('./not-a-page.js', pageExtensions)).toBe(false)
expect(isLayoutsLeafPage('not-a-page.js', pageExtensions)).toBe(false)
expect(isLayoutsLeafPage('./page.component.jsx', pageExtensions)).toBe(
false
)
expect(isLayoutsLeafPage('layout.js', pageExtensions)).toBe(false)
expect(isLayoutsLeafPage('page', pageExtensions)).toBe(false)
describe('isMetadataRouteFile', () => {
const pageExtensions = ['tsx', 'ts', 'jsx', 'js']
const fileMatcher = createValidFileMatcher(pageExtensions, 'app')
it('should determine top level metadata routes', () => {
expect(fileMatcher.isMetadataRouteFile('app/route.js')).toBe(false)
expect(fileMatcher.isMetadataRouteFile('app/page.js')).toBe(false)
expect(fileMatcher.isMetadataRouteFile('pages/index.js')).toBe(false)
expect(fileMatcher.isMetadataRouteFile('app/robots.txt')).toBe(true)
expect(fileMatcher.isMetadataRouteFile('app/path/robots.txt')).toBe(false)
expect(fileMatcher.isMetadataRouteFile('app/sitemap.xml')).toBe(true)
expect(fileMatcher.isMetadataRouteFile('app/path/sitemap.xml')).toBe(
false
)
})
})
})
......@@ -16,7 +16,9 @@ describe('recursiveDelete', () => {
await recursiveCopy(resolveDataDir, testResolveDataDir)
await fs.symlink('./aa', join(testResolveDataDir, 'symlink'))
await recursiveDelete(testResolveDataDir)
const result = await recursiveReadDir(testResolveDataDir, /.*/)
const result = await recursiveReadDir(testResolveDataDir, (f) =>
/.*/.test(f)
)
expect(result.length).toBe(0)
} finally {
await recursiveDelete(testResolveDataDir)
......@@ -32,13 +34,17 @@ describe('recursiveDelete', () => {
// preserve cache dir
await recursiveDelete(testpreservefileDir, /^cache/)
const result = await recursiveReadDir(testpreservefileDir, /.*/)
const result = await recursiveReadDir(testpreservefileDir, (f) =>
/.*/.test(f)
)
expect(result.length).toBe(1)
} finally {
// Ensure test cleanup
await recursiveDelete(testpreservefileDir)
const cleanupResult = await recursiveReadDir(testpreservefileDir, /.*/)
const cleanupResult = await recursiveReadDir(testpreservefileDir, (f) =>
/.*/.test(f)
)
expect(cleanupResult.length).toBe(0)
}
})
......
......@@ -8,7 +8,7 @@ const dirWithPages = join(resolveDataDir, 'readdir', 'pages')
describe('recursiveReadDir', () => {
it('should work', async () => {
const result = await recursiveReadDir(dirWithPages, /\.js/)
const result = await recursiveReadDir(dirWithPages, (f) => /\.js/.test(f))
const pages = [
/^[\\/]index\.js/,
/^[\\/]prefered\.js/,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment