Skip to content

Commit

Permalink
Merge branch 'canary' into hrmny/fix-ts-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Mar 28, 2023
2 parents 0b8623c + 9d9bd8b commit 9062bd4
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/advanced-features/compiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ module.exports = {
src: './',
artifactDirectory: './__generated__',
language: 'typescript',
eagerEsModules: false;
eagerEsModules: false,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export function createPagesMapping({
let pageKey = getPageFromPath(pagePath, pageExtensions)
if (isAppRoute) {
pageKey = pageKey.replace(/%5F/g, '_')
pageKey = pageKey.replace(/^\/not-found$/g, '/_not-found')
}

if (pageKey in result) {
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ export default async function build(

const conflictingPublicFiles: string[] = []
const hasPages404 = mappedPages['/404']?.startsWith(PAGES_DIR_ALIAS)
const hasApp404 = !!mappedAppPages?.['/not-found']
const hasApp404 = !!mappedAppPages?.['/_not-found']
const hasCustomErrorPage =
mappedPages['/_error'].startsWith(PAGES_DIR_ALIAS)

Expand Down Expand Up @@ -2493,7 +2493,7 @@ export default async function build(

routes.forEach((route) => {
if (isDynamicRoute(page) && route === page) return
if (route === '/not-found') return
if (route === '/_not-found') return

let revalidate = exportConfig.initialPageRevalidationMap[route]

Expand Down Expand Up @@ -2703,7 +2703,7 @@ export default async function build(
distDir,
'server',
'app',
'not-found.html'
'_not-found.html'
)
const updatedRelativeDest = path
.join('pages', '404.html')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('resolveRouteData', () => {
},
{
userAgent: 'Googlebot',
allow: '/bot',
allow: ['/bot', '/bot2'],
},
],
}
Expand All @@ -54,7 +54,17 @@ describe('resolveRouteData', () => {
rules: { allow: '/' },
}

resolveRobots(data1)
expect(resolveRobots(data1)).toMatchInlineSnapshot(`
"User-Agent: *
Allow: /
User-Agent: Googlebot
Allow: /bot
Allow: /bot2
"
`)

resolveRobots(data2)
expect(resolveRobots(data3)).toMatchInlineSnapshot(`
"User-Agent: *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ import type {
Sitemap,
} from '../../../../lib/metadata/types/metadata-interface'
import type { Manifest } from '../../../../lib/metadata/types/manifest-types'
import { resolveAsArrayOrUndefined } from '../../../../lib/metadata/generate/utils'
import { resolveArray } from '../../../../lib/metadata/generate/utils'

// convert robots data to txt string
export function resolveRobots(data: Robots): string {
let content = ''
const rules = Array.isArray(data.rules) ? data.rules : [data.rules]
for (const rule of rules) {
const userAgent = resolveAsArrayOrUndefined(rule.userAgent) || ['*']
const userAgent = resolveArray(rule.userAgent || ['*'])
for (const agent of userAgent) {
content += `User-Agent: ${agent}\n`
}
if (rule.allow) {
content += `Allow: ${rule.allow}\n`
const allow = resolveArray(rule.allow)
for (const item of allow) {
content += `Allow: ${item}\n`
}
}
if (rule.disallow) {
content += `Disallow: ${rule.disallow}\n`
const disallow = resolveArray(rule.disallow)
for (const item of disallow) {
content += `Disallow: ${item}\n`
}
}
if (rule.crawlDelay) {
content += `Crawl-delay: ${rule.crawlDelay}\n`
Expand All @@ -28,8 +34,8 @@ export function resolveRobots(data: Robots): string {
if (data.host) {
content += `Host: ${data.host}\n`
}
const sitemap = resolveAsArrayOrUndefined(data.sitemap)
if (sitemap) {
if (data.sitemap) {
const sitemap = resolveArray(data.sitemap)
// TODO-METADATA: support injecting sitemap url into robots.txt
sitemap.forEach((item) => {
content += `Sitemap: ${item}\n`
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ export default async function exportPage({
try {
curRenderOpts.params ||= {}

const isNotFoundPage = page === '/not-found'
const isNotFoundPage = page === '/_not-found'
const result = await renderToHTMLOrFlight(
req as any,
res as any,
Expand Down
9 changes: 8 additions & 1 deletion packages/next/src/lib/metadata/generate/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function resolveArray<T>(value: T): T[] {
if (Array.isArray(value)) {
return value
}
return [value]
}

function resolveAsArrayOrUndefined<T extends unknown | readonly unknown[]>(
value: T | T[] | undefined | null
): undefined | T[] {
Expand All @@ -10,4 +17,4 @@ function resolveAsArrayOrUndefined<T extends unknown | readonly unknown[]>(
return [value]
}

export { resolveAsArrayOrUndefined }
export { resolveAsArrayOrUndefined, resolveArray }
2 changes: 1 addition & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2247,7 +2247,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (this.hasAppDir) {
// Use the not-found entry in app directory
result = await this.findPageComponents({
pathname: '/not-found',
pathname: this.renderOpts.dev ? '/not-found' : '/_not-found',
query,
params: {},
isAppPath: true,
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/app-dir/not-found/app/not-found/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <h1>I'm still a valid page</h1>
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/not-found/not-found.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ createNextDescribe(
expect(html).toContain('This Is The Not Found Page')
})

it('should allow to have a valid /not-found route', async () => {
const html = await next.render('/not-found')
expect(html).toContain("I'm still a valid page")
})

if (!isNextDev) {
it('should create the 404 mapping and copy the file to pages', async () => {
const html = await next.readFile('.next/server/pages/404.html')
Expand Down

0 comments on commit 9062bd4

Please sign in to comment.