Unverified Commit bfd6c3ea authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Dynamic routes for toplevel robots and sitemap (#47091)

Support top-level `robots.[ext]` and `sitemap.[ext]` with dynamic api
routes

* Use isAppRoute to determine api routes and metadata routes as metadata
routes are normalized as `<metadata>/route`
* Normalize path to auto append extension to pathname for sitemap.js and
robots.js
* Add typings `SitemapFile` and `RobotsFile`
* move the normalize logic together, reuse the `absolutePathToPage`.
Changes less when touching both dev-server/hot-reloader and next-server,
use the same utils to handle paths

Closes NEXT-262

---------
parent 1c83bb86
...@@ -47,7 +47,8 @@ import { ServerRuntime } from '../../types' ...@@ -47,7 +47,8 @@ import { ServerRuntime } from '../../types'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import { encodeMatchers } from './webpack/loaders/next-middleware-loader' import { encodeMatchers } from './webpack/loaders/next-middleware-loader'
import { EdgeFunctionLoaderOptions } from './webpack/loaders/next-edge-function-loader' import { EdgeFunctionLoaderOptions } from './webpack/loaders/next-edge-function-loader'
import { isAppRouteRoute, isMetadataRoute } from '../lib/is-app-route-route' import { isAppRouteRoute } from '../lib/is-app-route-route'
import { normalizeMetadataRoute } from '../lib/metadata/get-metadata-route'
type ObjectValue<T> = T extends { [key: string]: infer V } ? V : never type ObjectValue<T> = T extends { [key: string]: infer V } ? V : never
...@@ -110,10 +111,8 @@ export function createPagesMapping({ ...@@ -110,10 +111,8 @@ export function createPagesMapping({
) )
) )
// Map metadata routes to /<metadata-route>/route, e.g. /robots.txt/route. const route = normalizeMetadataRoute(pageKey)
// This is to mark it as app route path that could be picked up like other custom app routes. result[route] = normalizedPath
result[isMetadataRoute(pageKey) ? `${pageKey}/route` : pageKey] =
normalizedPath
return result return result
}, },
{} {}
......
...@@ -132,7 +132,7 @@ import { ...@@ -132,7 +132,7 @@ import {
import { webpackBuild } from './webpack-build' import { webpackBuild } from './webpack-build'
import { NextBuildContext } from './build-context' import { NextBuildContext } from './build-context'
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep' import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
import { isAppRouteRoute, isMetadataRoute } from '../lib/is-app-route-route' import { isAppRouteRoute } from '../lib/is-app-route-route'
import { createClientRouterFilter } from '../lib/create-client-router-filter' import { createClientRouterFilter } from '../lib/create-client-router-filter'
import { createValidFileMatcher } from '../server/lib/find-page-file' import { createValidFileMatcher } from '../server/lib/find-page-file'
...@@ -2440,9 +2440,7 @@ export default async function build( ...@@ -2440,9 +2440,7 @@ export default async function build(
appConfig.revalidate === 0 || appConfig.revalidate === 0 ||
exportConfig.initialPageRevalidationMap[page] === 0 exportConfig.initialPageRevalidationMap[page] === 0
const isRouteHandler = const isRouteHandler = isAppRouteRoute(originalAppPath)
isAppRouteRoute(originalAppPath) ||
isMetadataRoute(originalAppPath)
routes.forEach((route) => { routes.forEach((route) => {
if (isDynamicRoute(page) && route === page) return if (isDynamicRoute(page) && route === page) return
......
import type { RobotsFile } from '../../../../lib/metadata/types/metadata-interface'
import { resolveRobots, resolveSitemap } from './resolve-route-data'
describe('resolveRouteData', () => {
describe('resolveRobots', () => {
it('should resolve robots.txt', () => {
const data = {
host: 'https://example.com',
sitemap: 'https://example.com/sitemap.xml',
rules: [
{
userAgent: 'Googlebot',
allow: '/',
disallow: '/admin',
crawlDelay: 2,
},
],
}
const content = resolveRobots(data)
expect(content).toMatchInlineSnapshot(`
"User-Agent: Googlebot
Allow: /
Disallow: /admin
Crawl-delay: 2
Host: https://example.com
Sitemap: https://example.com/sitemap.xml
"
`)
})
it('should error with ts when specify both wildcard userAgent and specific userAgent', () => {
const data1: RobotsFile = {
rules: [
// @ts-expect-error userAgent is required for Array<Robots>
{
allow: '/',
},
{
userAgent: 'Googlebot',
allow: '/bot',
},
],
}
const data2: RobotsFile = {
rules: {
// @ts-expect-error When apply only 1 rule, only '*' or undefined is allowed
userAgent: 'Somebot',
allow: '/',
},
}
const data3: RobotsFile = {
rules: { allow: '/' },
}
resolveRobots(data1)
resolveRobots(data2)
expect(resolveRobots(data3)).toMatchInlineSnapshot(`
"User-Agent: *
Allow: /
"
`)
})
})
describe('resolveSitemap', () => {
it('should resolve sitemap.xml', () => {
expect(
resolveSitemap([
{
url: 'https://example.com',
lastModified: '2021-01-01',
},
])
).toMatchInlineSnapshot(`
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\">
<url>
<loc>https://example.com</loc>
<lastmod>2021-01-01</lastmod>
</url>
</urlset>
"
`)
})
})
})
import type {
RobotsFile,
SitemapFile,
} from '../../../../lib/metadata/types/metadata-interface'
import { resolveAsArrayOrUndefined } from '../../../../lib/metadata/generate/utils'
// convert robots data to txt string
export function resolveRobots(data: RobotsFile): string {
let content = ''
const rules = Array.isArray(data.rules) ? data.rules : [data.rules]
for (const rule of rules) {
const userAgent = resolveAsArrayOrUndefined(rule.userAgent) || ['*']
for (const agent of userAgent) {
content += `User-Agent: ${agent}\n`
}
if (rule.allow) {
content += `Allow: ${rule.allow}\n`
}
if (rule.disallow) {
content += `Disallow: ${rule.disallow}\n`
}
if (rule.crawlDelay) {
content += `Crawl-delay: ${rule.crawlDelay}\n`
}
content += '\n'
}
if (data.host) {
content += `Host: ${data.host}\n`
}
const sitemap = resolveAsArrayOrUndefined(data.sitemap)
if (sitemap) {
// TODO-METADATA: support injecting sitemap url into robots.txt
sitemap.forEach((item) => {
content += `Sitemap: ${item}\n`
})
}
return content
}
// TODO-METADATA: support multi sitemap files
// convert sitemap data to xml string
export function resolveSitemap(data: SitemapFile): string {
let content = ''
content += '<?xml version="1.0" encoding="UTF-8"?>\n'
content += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
for (const item of data) {
content += '<url>\n'
content += `<loc>${item.url}</loc>\n`
if (item.lastModified) {
content += `<lastmod>${
item.lastModified instanceof Date
? item.lastModified.toISOString()
: item.lastModified
}</lastmod>\n`
}
content += '</url>\n'
}
content += '</urlset>\n'
return content
}
export function resolveRouteData(
data: RobotsFile | SitemapFile,
baseName: 'robots' | 'sitemap'
): string {
if (baseName === 'robots') {
return resolveRobots(data as RobotsFile)
}
if (baseName === 'sitemap') {
return resolveSitemap(data as SitemapFile)
}
return ''
}
...@@ -10,10 +10,8 @@ import { verifyRootLayout } from '../../../lib/verifyRootLayout' ...@@ -10,10 +10,8 @@ import { verifyRootLayout } from '../../../lib/verifyRootLayout'
import * as Log from '../../../build/output/log' import * as Log from '../../../build/output/log'
import { APP_DIR_ALIAS } from '../../../lib/constants' import { APP_DIR_ALIAS } from '../../../lib/constants'
import { buildMetadata, discoverStaticMetadataFiles } from './metadata/discover' import { buildMetadata, discoverStaticMetadataFiles } from './metadata/discover'
import { import { isAppRouteRoute } from '../../../lib/is-app-route-route'
isAppRouteRoute, import { isMetadataRoute } from '../../../lib/metadata/is-metadata-route'
isMetadataRoute,
} from '../../../lib/is-app-route-route'
export type AppLoaderOptions = { export type AppLoaderOptions = {
name: string name: string
...@@ -83,13 +81,13 @@ async function createAppRouteCode({ ...@@ -83,13 +81,13 @@ async function createAppRouteCode({
export const resolvedPagePath = ${JSON.stringify(resolvedPagePath)} export const resolvedPagePath = ${JSON.stringify(resolvedPagePath)}
export { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage' export { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage'
export * as serverHooks from 'next/dist/client/components/hooks-server-context' export * as serverHooks from 'next/dist/client/components/hooks-server-context'
export { staticGenerationBailout } from 'next/dist/client/components/static-generation-bailout' export { staticGenerationBailout } from 'next/dist/client/components/static-generation-bailout'
export * as headerHooks from 'next/dist/client/components/headers' export * as headerHooks from 'next/dist/client/components/headers'
export { requestAsyncStorage } from 'next/dist/client/components/request-async-storage' export { requestAsyncStorage } from 'next/dist/client/components/request-async-storage'
` `
} }
...@@ -350,7 +348,7 @@ const nextAppLoader: AppLoader = async function nextAppLoader() { ...@@ -350,7 +348,7 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
} }
} }
if (isAppRouteRoute(name) || isMetadataRoute(name)) { if (isAppRouteRoute(name)) {
return createAppRouteCode({ name, pagePath, resolver }) return createAppRouteCode({ name, pagePath, resolver })
} }
...@@ -416,7 +414,7 @@ const nextAppLoader: AppLoader = async function nextAppLoader() { ...@@ -416,7 +414,7 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
)} )}
export { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage' export { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage'
export { requestAsyncStorage } from 'next/dist/client/components/request-async-storage' export { requestAsyncStorage } from 'next/dist/client/components/request-async-storage'
export * as serverHooks from 'next/dist/client/components/hooks-server-context' export * as serverHooks from 'next/dist/client/components/hooks-server-context'
......
import type webpack from 'webpack' import type webpack from 'webpack'
import path from 'path' import path from 'path'
import { isStaticMetadataRoute } from '../../../lib/is-app-route-route' import { isStaticMetadataRoute } from '../../../lib/metadata/is-metadata-route'
function getContentType(resourcePath: string) { function getFilenameAndExtension(resourcePath: string) {
const filename = path.basename(resourcePath) const filename = path.basename(resourcePath)
const [name, ext] = filename.split('.') const [name, ext] = filename.split('.')
return { name, ext }
}
function getContentType(resourcePath: string) {
const { name, ext } = getFilenameAndExtension(resourcePath)
if (name === 'favicon' && ext === 'ico') return 'image/x-icon' if (name === 'favicon' && ext === 'ico') return 'image/x-icon'
if (name === 'sitemap') return 'application/xml' if (name === 'sitemap') return 'application/xml'
if (name === 'robots') return 'text/plain' if (name === 'robots') return 'text/plain'
...@@ -13,11 +18,13 @@ function getContentType(resourcePath: string) { ...@@ -13,11 +18,13 @@ function getContentType(resourcePath: string) {
// `import.meta.url` is the resource name of the current module. // `import.meta.url` is the resource name of the current module.
// When it's static route, it could be favicon.ico, sitemap.xml, robots.txt etc. // When it's static route, it could be favicon.ico, sitemap.xml, robots.txt etc.
// TODO-METADATA: improve the cache control strategy
const nextMetadataRouterLoader: webpack.LoaderDefinitionFunction = function () { const nextMetadataRouterLoader: webpack.LoaderDefinitionFunction = function () {
const { resourcePath } = this const { resourcePath } = this
const isStatic = isStaticMetadataRoute(resourcePath)
const code = isStaticMetadataRoute(resourcePath) const code = isStatic
? `import { NextResponse } from 'next/server' ? `\
import { NextResponse } from 'next/server'
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
...@@ -27,17 +34,35 @@ const contentType = ${JSON.stringify(getContentType(resourcePath))} ...@@ -27,17 +34,35 @@ const contentType = ${JSON.stringify(getContentType(resourcePath))}
export function GET() { export function GET() {
return new NextResponse(buffer, { return new NextResponse(buffer, {
status: 200,
headers: { headers: {
'Content-Type': contentType, 'Content-Type': contentType,
'Cache-Control': 'public, max-age=0, must-revalidate',
}, },
}) })
} }
export const dynamic = 'force-static' export const dynamic = 'force-static'
` `
: // TODO: handle the defined configs in routes file : `\
`export { default as GET } from ${JSON.stringify(resourcePath)}` import { NextResponse } from 'next/server'
import handler from ${JSON.stringify(resourcePath)}
import { resolveRouteData } from 'next/dist/build/webpack/loaders/metadata/resolve-route-data'
const contentType = ${JSON.stringify(getContentType(resourcePath))}
const name = ${JSON.stringify(getFilenameAndExtension(resourcePath).name)}
export async function GET() {
const data = await handler()
const content = resolveRouteData(data, name)
return new NextResponse(content, {
headers: {
'Content-Type': contentType,
'Cache-Control': 'public, max-age=0, must-revalidate',
},
})
}
`
return code return code
} }
......
...@@ -31,10 +31,7 @@ import { ...@@ -31,10 +31,7 @@ import {
} from '../loaders/utils' } from '../loaders/utils'
import { traverseModules } from '../utils' import { traverseModules } from '../utils'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep' import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
import { import { isAppRouteRoute } from '../../../lib/is-app-route-route'
isAppRouteRoute,
isMetadataRoute,
} from '../../../lib/is-app-route-route'
import { getProxiedPluginState } from '../../build-context' import { getProxiedPluginState } from '../../build-context'
interface Options { interface Options {
...@@ -184,8 +181,7 @@ export class FlightClientEntryPlugin { ...@@ -184,8 +181,7 @@ export class FlightClientEntryPlugin {
if ( if (
name.startsWith('pages/') || name.startsWith('pages/') ||
// Skip for route.js entries // Skip for route.js entries
(name.startsWith('app/') && (name.startsWith('app/') && isAppRouteRoute(name))
(isAppRouteRoute(name) || isMetadataRoute(name)))
) { ) {
continue continue
} }
......
...@@ -40,7 +40,7 @@ import { mockRequest } from '../server/lib/mock-request' ...@@ -40,7 +40,7 @@ import { mockRequest } from '../server/lib/mock-request'
import { RouteKind } from '../server/future/route-kind' import { RouteKind } from '../server/future/route-kind'
import { NodeNextRequest, NodeNextResponse } from '../server/base-http/node' import { NodeNextRequest, NodeNextResponse } from '../server/base-http/node'
import { StaticGenerationContext } from '../server/future/route-handlers/app-route-route-handler' import { StaticGenerationContext } from '../server/future/route-handlers/app-route-route-handler'
import { isAppRouteRoute, isMetadataRoute } from '../lib/is-app-route-route' import { isAppRouteRoute } from '../lib/is-app-route-route'
loadRequireHook() loadRequireHook()
...@@ -167,8 +167,7 @@ export default async function exportPage({ ...@@ -167,8 +167,7 @@ export default async function exportPage({
let renderAmpPath = ampPath let renderAmpPath = ampPath
let query = { ...originalQuery } let query = { ...originalQuery }
let params: { [key: string]: string | string[] } | undefined let params: { [key: string]: string | string[] } | undefined
const isRouteHandler = const isRouteHandler = isAppDir && isAppRouteRoute(page)
isAppDir && (isAppRouteRoute(page) || isMetadataRoute(page))
if (isAppDir) { if (isAppDir) {
outDir = join(distDir, 'server/app') outDir = join(distDir, 'server/app')
......
export function isAppRouteRoute(route: string): boolean { export function isAppRouteRoute(route: string): boolean {
return route.endsWith('/route') return route.endsWith('/route')
} }
// Match routes that are metadata routes, e.g. /sitemap.xml, /favicon.<ext>, /<icon>.<ext>, etc.
// TODO-METADATA: support more metadata routes with more extensions
const staticMetadataRoutes = [/robots\.txt/, /sitemap\.xml/, /favicon\.ico/]
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(/^\//, '')
.replace(/\/route$/, '')
return staticMetadataRoutes.some((r) => r.test(filename))
}
// Only match the static metadata files
// TODO-METADATA: support static metadata files under nested routes folders
export function isStaticMetadataRoute(resourcePath: string) {
return staticMetadataRoutes.some((r) => r.test(resourcePath))
}
import { isMetadataRoute, isStaticMetadataRoute } from './is-metadata-route'
/**
* Map metadata page key to the corresponding route
*
* static file page key: app/robots.txt -> /robots.xml -> /robots.txt/route
* dynamic route page key: app/robots.tsx -> /robots -> /robots.txt/route
*
* @param page
* @returns
*/
export function normalizeMetadataRoute(page: string) {
let route = page
if (isMetadataRoute(page)) {
if (!isStaticMetadataRoute(route)) {
if (route === '/sitemap') {
route += '.xml'
}
if (route === '/robots') {
route += '.txt'
}
if (route === '/favicon') {
route += '.ico'
}
}
route = `${route}/route`
}
return route
}
import path from 'path'
const regexMetadataRoutes = [
/^robots(\.txt)?/,
/^sitemap(\.xml)?/,
/^favicon(\.ico)?/,
]
const staticRegexMetadataRoutes = [
/^robots\.txt/,
/^sitemap\.xml/,
/^favicon\.ico/,
]
// Match routes that are metadata routes, e.g. /sitemap.xml, /favicon.<ext>, /<icon>.<ext>, etc.
// TODO-METADATA: support more metadata routes with more extensions
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 baseName = route
.replace(/^app\//, '')
.replace(/^\//, '')
.replace(/\/route$/, '')
return (
!baseName.endsWith('/page') &&
regexMetadataRoutes.some((r) => r.test(baseName))
)
}
// Only match the static metadata files
// TODO-METADATA: support static metadata files under nested routes folders
export function isStaticMetadataRoute(resourcePath: string) {
const filename = path.basename(resourcePath)
return staticRegexMetadataRoutes.some((r) => r.test(filename))
}
...@@ -534,5 +534,30 @@ interface ResolvedMetadata extends DeprecatedMetadataFields { ...@@ -534,5 +534,30 @@ interface ResolvedMetadata extends DeprecatedMetadataFields {
} & DeprecatedMetadataFields) } & DeprecatedMetadataFields)
} }
type RobotsFile = {
// Apply rules for all
rules:
| {
userAgent?: undefined | '*'
allow?: string | string[]
disallow?: string | string[]
crawlDelay?: number
}
// Apply rules for specific user agents
| Array<{
userAgent: string | string[]
allow?: string | string[]
disallow?: string | string[]
crawlDelay?: number
}>
sitemap?: string | string[]
host?: string
}
type SitemapFile = Array<{
url: string
lastModified?: string | Date
}>
export type ResolvingMetadata = Promise<ResolvedMetadata> export type ResolvingMetadata = Promise<ResolvedMetadata>
export { Metadata, ResolvedMetadata } export { Metadata, ResolvedMetadata, RobotsFile, SitemapFile }
...@@ -676,7 +676,7 @@ export default class HotReloader { ...@@ -676,7 +676,7 @@ export default class HotReloader {
/^(client|server|edge-server)@(app|pages|root)@(.*)/g.exec( /^(client|server|edge-server)@(app|pages|root)@(.*)/g.exec(
entryKey entryKey
) )
const [, key /* pageType*/, , page] = result! // this match should always happen const [, key /* pageType */, , page] = result! // this match should always happen
if (key === COMPILER_NAMES.client && !isClientCompilation) return if (key === COMPILER_NAMES.client && !isClientCompilation) return
if (key === COMPILER_NAMES.server && !isNodeServerCompilation) if (key === COMPILER_NAMES.server && !isNodeServerCompilation)
...@@ -763,7 +763,7 @@ export default class HotReloader { ...@@ -763,7 +763,7 @@ export default class HotReloader {
absolutePagePath: entryData.absolutePagePath, absolutePagePath: entryData.absolutePagePath,
rootDir: this.dir, rootDir: this.dir,
buildId: this.buildId, buildId: this.buildId,
bundlePath: bundlePath, bundlePath,
config: this.config, config: this.config,
isDev: true, isDev: true,
page, page,
......
...@@ -492,6 +492,7 @@ export default class DevServer extends Server { ...@@ -492,6 +492,7 @@ export default class DevServer extends Server {
const rootFile = absolutePathToPage(fileName, { const rootFile = absolutePathToPage(fileName, {
pagesDir: this.dir, pagesDir: this.dir,
extensions: this.nextConfig.pageExtensions, extensions: this.nextConfig.pageExtensions,
keepIndex: false,
}) })
const staticInfo = await getPageStaticInfo({ const staticInfo = await getPageStaticInfo({
......
import path from '../../../shared/lib/isomorphic/path' import { absolutePathToPage } from '../../../shared/lib/page-path/absolute-path-to-page'
import { ensureLeadingSlash } from '../../../shared/lib/page-path/ensure-leading-slash'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
import { removePagePathTail } from '../../../shared/lib/page-path/remove-page-path-tail'
import { Normalizer } from './normalizer' import { Normalizer } from './normalizer'
/** /**
...@@ -21,12 +18,10 @@ export class AbsoluteFilenameNormalizer implements Normalizer { ...@@ -21,12 +18,10 @@ export class AbsoluteFilenameNormalizer implements Normalizer {
) {} ) {}
public normalize(pathname: string): string { public normalize(pathname: string): string {
return removePagePathTail( return absolutePathToPage(pathname, {
normalizePathSep(ensureLeadingSlash(path.relative(this.dir, pathname))), extensions: this.extensions,
{ keepIndex: false,
extensions: this.extensions, pagesDir: this.dir,
keepIndex: false, })
}
)
} }
} }
import path from '../../../shared/lib/isomorphic/path' import path from '../../../shared/lib/isomorphic/path'
import { import { isAppRouteRoute } from '../../../lib/is-app-route-route'
isAppRouteRoute,
isMetadataRoute,
} from '../../../lib/is-app-route-route'
import { import {
APP_PATHS_MANIFEST, APP_PATHS_MANIFEST,
SERVER_DIRECTORY, SERVER_DIRECTORY,
...@@ -28,9 +25,8 @@ export class AppRouteRouteMatcherProvider extends ManifestRouteMatcherProvider<A ...@@ -28,9 +25,8 @@ export class AppRouteRouteMatcherProvider extends ManifestRouteMatcherProvider<A
manifest: Manifest manifest: Manifest
): Promise<ReadonlyArray<AppRouteRouteMatcher>> { ): Promise<ReadonlyArray<AppRouteRouteMatcher>> {
// This matcher only matches app routes. // This matcher only matches app routes.
const pages = Object.keys(manifest).filter(
(page) => isAppRouteRoute(page) || isMetadataRoute(page) const pages = Object.keys(manifest).filter((page) => isAppRouteRoute(page))
)
// Format the routes. // Format the routes.
const matchers: Array<AppRouteRouteMatcher> = [] const matchers: Array<AppRouteRouteMatcher> = []
......
...@@ -2,6 +2,7 @@ import { ensureLeadingSlash } from './ensure-leading-slash' ...@@ -2,6 +2,7 @@ import { ensureLeadingSlash } from './ensure-leading-slash'
import { normalizePathSep } from './normalize-path-sep' import { normalizePathSep } from './normalize-path-sep'
import path from '../isomorphic/path' import path from '../isomorphic/path'
import { removePagePathTail } from './remove-page-path-tail' import { removePagePathTail } from './remove-page-path-tail'
import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route'
/** /**
* Given the absolute path to the pages folder, an absolute file path for a * Given the absolute path to the pages folder, an absolute file path for a
...@@ -9,6 +10,9 @@ import { removePagePathTail } from './remove-page-path-tail' ...@@ -9,6 +10,9 @@ import { removePagePathTail } from './remove-page-path-tail'
* relative to the pages folder. It doesn't consider index tail. Example: * relative to the pages folder. It doesn't consider index tail. Example:
* - `/Users/rick/my-project/pages/foo/bar/baz.js` -> `/foo/bar/baz` * - `/Users/rick/my-project/pages/foo/bar/baz.js` -> `/foo/bar/baz`
* *
* It also handles special metadata routes mapping. Example:
* - `/Users/rick/my-project/app/sitemap.js` -> `/sitemap/route`
*
* @param filepath Absolute path to the page. * @param filepath Absolute path to the page.
* @param opts.pagesDir Absolute path to the pages folder. * @param opts.pagesDir Absolute path to the pages folder.
* @param opts.extensions Extensions allowed for the page. * @param opts.extensions Extensions allowed for the page.
...@@ -17,12 +21,12 @@ import { removePagePathTail } from './remove-page-path-tail' ...@@ -17,12 +21,12 @@ import { removePagePathTail } from './remove-page-path-tail'
export function absolutePathToPage( export function absolutePathToPage(
pagePath: string, pagePath: string,
options: { options: {
extensions: string[] extensions: string[] | readonly string[]
keepIndex?: boolean keepIndex: boolean
pagesDir: string pagesDir: string
} }
) { ) {
return removePagePathTail( const page = removePagePathTail(
normalizePathSep( normalizePathSep(
ensureLeadingSlash(path.relative(options.pagesDir, pagePath)) ensureLeadingSlash(path.relative(options.pagesDir, pagePath))
), ),
...@@ -31,4 +35,5 @@ export function absolutePathToPage( ...@@ -31,4 +35,5 @@ export function absolutePathToPage(
keepIndex: options.keepIndex, keepIndex: options.keepIndex,
} }
) )
return normalizeMetadataRoute(page)
} }
...@@ -28,8 +28,11 @@ export type ServerRuntime = 'nodejs' | 'experimental-edge' | 'edge' | undefined ...@@ -28,8 +28,11 @@ export type ServerRuntime = 'nodejs' | 'experimental-edge' | 'edge' | undefined
// @ts-ignore This path is generated at build time and conflicts otherwise // @ts-ignore This path is generated at build time and conflicts otherwise
export { NextConfig } from '../dist/server/config' export { NextConfig } from '../dist/server/config'
// @ts-ignore This path is generated at build time and conflicts otherwise export type {
export { Metadata } from '../dist/lib/metadata/types/metadata-interface' Metadata,
RobotsFile,
SitemapFile, // @ts-ignore This path is generated at build time and conflicts otherwise
} from '../dist/lib/metadata/types/metadata-interface'
// Extend the React types with missing properties // Extend the React types with missing properties
declare module 'react' { declare module 'react' {
......
export default function Layout({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
export const metadata = {
title: 'this is the layout title',
description: 'this is the layout description',
}
import React from 'react'
export default function Page() {
return <>hello index</>
}
export const metadata = {
title: 'index page',
}
import type { RobotsFile } from 'next'
export default function robots(): RobotsFile {
return {
rules: [
{
userAgent: 'Googlebot',
allow: ['/'],
},
{
userAgent: ['Applebot', 'Bingbot'],
disallow: ['/'],
crawlDelay: 2,
},
],
sitemap: 'https://example.com/sitemap.xml',
host: 'https://example.com',
}
}
import type { SitemapFile } from 'next'
export default function sitemap(): SitemapFile {
return [
{
url: 'https://example.com',
lastModified: '2021-01-01',
},
{
url: 'https://example.com/about',
lastModified: '2021-01-01',
},
]
}
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app dir - metadata dynamic routes',
{
files: __dirname,
skipDeployment: true,
},
({ next }) => {
describe('dynamic routes', () => {
it('should handle robots.[ext] dynamic routes', async () => {
const res = await next.fetch('/robots.txt')
const text = await res.text()
expect(res.headers.get('content-type')).toBe('text/plain')
expect(res.headers.get('cache-control')).toBe(
'public, max-age=0, must-revalidate'
)
expect(text).toMatchInlineSnapshot(`
"User-Agent: Googlebot
Allow: /
User-Agent: Applebot
User-Agent: Bingbot
Disallow: /
Crawl-delay: 2
Host: https://example.com
Sitemap: https://example.com/sitemap.xml
"
`)
})
it('should handle sitemap.[ext] dynamic routes', async () => {
const res = await next.fetch('/sitemap.xml')
const text = await res.text()
expect(res.headers.get('content-type')).toBe('application/xml')
expect(res.headers.get('cache-control')).toBe(
'public, max-age=0, must-revalidate'
)
expect(text).toMatchInlineSnapshot(`
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\">
<url>
<loc>https://example.com</loc>
<lastmod>2021-01-01</lastmod>
</url>
<url>
<loc>https://example.com/about</loc>
<lastmod>2021-01-01</lastmod>
</url>
</urlset>
"
`)
})
})
}
)
module.exports = {
experimental: { appDir: true },
}
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "**/*.test.ts"]
}
...@@ -20,5 +20,5 @@ ...@@ -20,5 +20,5 @@
] ]
}, },
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"], "include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"] "exclude": ["node_modules", "**/*.test.ts"]
} }
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