Unverified Commit a4c507d5 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update to use existing util to de-dupe path check (#10431)

* Update to use existing util to de-dupe path check

* Update error message for requested/resolved mismatch

* Use correct dataRoute value for prerender manifest

* Fix pageUrl having double slash on Windows
parent 924f8ae3
...@@ -6,6 +6,7 @@ import { API_ROUTE, DOT_NEXT_ALIAS, PAGES_DIR_ALIAS } from '../lib/constants' ...@@ -6,6 +6,7 @@ import { API_ROUTE, DOT_NEXT_ALIAS, PAGES_DIR_ALIAS } from '../lib/constants'
import { isTargetLikeServerless } from '../next-server/server/config' import { isTargetLikeServerless } from '../next-server/server/config'
import { warn } from './output/log' import { warn } from './output/log'
import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader' import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader'
import { normalizePagePath } from '../next-server/server/normalize-page-path'
type PagesMapping = { type PagesMapping = {
[page: string]: string [page: string]: string
...@@ -91,7 +92,7 @@ export function createEntrypoints( ...@@ -91,7 +92,7 @@ export function createEntrypoints(
Object.keys(pages).forEach(page => { Object.keys(pages).forEach(page => {
const absolutePagePath = pages[page] const absolutePagePath = pages[page]
const bundleFile = page === '/' ? '/index.js' : `${page}.js` const bundleFile = `${normalizePagePath(page)}.js`
const isApiRoute = page.match(API_ROUTE) const isApiRoute = page.match(API_ROUTE)
const bundlePath = join('static', buildId, 'pages', bundleFile) const bundlePath = join('static', buildId, 'pages', bundleFile)
......
...@@ -67,6 +67,7 @@ import { ...@@ -67,6 +67,7 @@ import {
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
import { normalizePagePath } from '../next-server/server/normalize-page-path'
const fsAccess = promisify(fs.access) const fsAccess = promisify(fs.access)
const fsUnlink = promisify(fs.unlink) const fsUnlink = promisify(fs.unlink)
...@@ -435,7 +436,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -435,7 +436,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
const analysisBegin = process.hrtime() const analysisBegin = process.hrtime()
await Promise.all( await Promise.all(
pageKeys.map(async page => { pageKeys.map(async page => {
const actualPage = page === '/' ? '/index' : page const actualPage = normalizePagePath(page)
const [selfSize, allSize] = await getPageSizeInKb( const [selfSize, allSize] = await getPageSizeInKb(
actualPage, actualPage,
distDir, distDir,
...@@ -554,10 +555,11 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -554,10 +555,11 @@ export default async function build(dir: string, conf = null): Promise<void> {
routesManifest.serverPropsRoutes = {} routesManifest.serverPropsRoutes = {}
for (const page of serverPropsPages) { for (const page of serverPropsPages) {
const pagePath = normalizePagePath(page)
const dataRoute = path.posix.join( const dataRoute = path.posix.join(
'/_next/data', '/_next/data',
buildId, buildId,
`${page === '/' ? '/index' : page}.json` `${pagePath}.json`
) )
routesManifest.serverPropsRoutes[page] = { routesManifest.serverPropsRoutes[page] = {
...@@ -571,7 +573,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -571,7 +573,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
`^${path.posix.join( `^${path.posix.join(
'/_next/data', '/_next/data',
escapeStringRegexp(buildId), escapeStringRegexp(buildId),
`${page === '/' ? '/index' : page}.json` `${pagePath}.json`
)}$` )}$`
).source, ).source,
} }
...@@ -709,7 +711,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -709,7 +711,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
for (const page of combinedPages) { for (const page of combinedPages) {
const isSsg = ssgPages.has(page) const isSsg = ssgPages.has(page)
const isDynamic = isDynamicRoute(page) const isDynamic = isDynamicRoute(page)
let file = page === '/' ? '/index' : page const file = normalizePagePath(page)
// The dynamic version of SSG pages are not prerendered. Below, we handle // The dynamic version of SSG pages are not prerendered. Below, we handle
// the specific prerenders of these. // the specific prerenders of these.
if (!(isSsg && isDynamic)) { if (!(isSsg && isDynamic)) {
...@@ -729,11 +731,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -729,11 +731,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
initialRevalidateSeconds: initialRevalidateSeconds:
exportConfig.initialPageRevalidationMap[page], exportConfig.initialPageRevalidationMap[page],
srcRoute: null, srcRoute: null,
dataRoute: path.posix.join( dataRoute: path.posix.join('/_next/data', buildId, `${file}.json`),
'/_next/data',
buildId,
`${page === '/' ? '/index' : page}.json`
),
} }
} else { } else {
// For a dynamic SSG page, we did not copy its html nor data exports. // For a dynamic SSG page, we did not copy its html nor data exports.
...@@ -750,7 +748,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -750,7 +748,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
dataRoute: path.posix.join( dataRoute: path.posix.join(
'/_next/data', '/_next/data',
buildId, buildId,
`${route === '/' ? '/index' : route}.json` `${normalizePagePath(route)}.json`
), ),
} }
} }
...@@ -783,7 +781,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -783,7 +781,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
const dataRoute = path.posix.join( const dataRoute = path.posix.join(
'/_next/data', '/_next/data',
buildId, buildId,
`${tbdRoute === '/' ? '/index' : tbdRoute}.json` `${normalizePagePath(tbdRoute)}.json`
) )
finalDynamicRoutes[tbdRoute] = { finalDynamicRoutes[tbdRoute] = {
......
...@@ -33,6 +33,7 @@ import loadConfig, { ...@@ -33,6 +33,7 @@ import loadConfig, {
} from '../next-server/server/config' } from '../next-server/server/config'
import { eventVersion } from '../telemetry/events' import { eventVersion } from '../telemetry/events'
import { Telemetry } from '../telemetry/storage' import { Telemetry } from '../telemetry/storage'
import { normalizePagePath } from '../next-server/server/normalize-page-path'
const mkdirp = promisify(mkdirpModule) const mkdirp = promisify(mkdirpModule)
const copyFile = promisify(copyFileOrig) const copyFile = promisify(copyFileOrig)
...@@ -352,7 +353,7 @@ export default async function( ...@@ -352,7 +353,7 @@ export default async function(
if (!options.buildExport && prerenderManifest) { if (!options.buildExport && prerenderManifest) {
await Promise.all( await Promise.all(
Object.keys(prerenderManifest.routes).map(async route => { Object.keys(prerenderManifest.routes).map(async route => {
route = route === '/' ? '/index' : route route = normalizePagePath(route)
const orig = join(distPagesDir, route) const orig = join(distPagesDir, route)
const htmlDest = join( const htmlDest = join(
outDir, outDir,
......
...@@ -9,6 +9,7 @@ import { loadComponents } from '../next-server/server/load-components' ...@@ -9,6 +9,7 @@ import { loadComponents } from '../next-server/server/load-components'
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic' import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
import { getRouteMatcher } from '../next-server/lib/router/utils/route-matcher' import { getRouteMatcher } from '../next-server/lib/router/utils/route-matcher'
import { getRouteRegex } from '../next-server/lib/router/utils/route-regex' import { getRouteRegex } from '../next-server/lib/router/utils/route-regex'
import { normalizePagePath } from '../next-server/server/normalize-page-path'
const envConfig = require('../next-server/lib/runtime-config') const envConfig = require('../next-server/lib/runtime-config')
const writeFileP = promisify(writeFile) const writeFileP = promisify(writeFile)
...@@ -39,7 +40,7 @@ export default async function({ ...@@ -39,7 +40,7 @@ export default async function({
try { try {
const { query: originalQuery = {} } = pathMap const { query: originalQuery = {} } = pathMap
const { page } = pathMap const { page } = pathMap
const filePath = path === '/' ? '/index' : path const filePath = normalizePagePath(path)
const ampPath = `${filePath}.amp` const ampPath = `${filePath}.amp`
let query = { ...originalQuery } let query = { ...originalQuery }
let params let params
......
...@@ -52,6 +52,7 @@ import { ...@@ -52,6 +52,7 @@ import {
Header, Header,
getRedirectStatus, getRedirectStatus,
} from '../../lib/check-custom-routes' } from '../../lib/check-custom-routes'
import { normalizePagePath } from './normalize-page-path'
const getCustomRouteMatcher = pathMatch(true) const getCustomRouteMatcher = pathMatch(true)
...@@ -815,7 +816,7 @@ export default class Server { ...@@ -815,7 +816,7 @@ export default class Server {
return await loadComponents( return await loadComponents(
this.distDir, this.distDir,
this.buildId, this.buildId,
(pathname === '/' ? '/index' : pathname) + '.amp', normalizePagePath(pathname) + '.amp',
serverless serverless
) )
} catch (err) { } catch (err) {
......
...@@ -11,7 +11,9 @@ export function normalizePagePath(page: string): string { ...@@ -11,7 +11,9 @@ export function normalizePagePath(page: string): string {
// Throw when using ../ etc in the pathname // Throw when using ../ etc in the pathname
const resolvedPage = posix.normalize(page) const resolvedPage = posix.normalize(page)
if (page !== resolvedPage) { if (page !== resolvedPage) {
throw new Error('Requested and resolved page mismatch') throw new Error(
`Requested and resolved page mismatch: ${page} ${resolvedPage}`
)
} }
return page return page
} }
...@@ -30,7 +30,6 @@ export function getPagePath( ...@@ -30,7 +30,6 @@ export function getPagePath(
try { try {
page = normalizePagePath(page) page = normalizePagePath(page)
page = page === '/' ? '/index' : page
} catch (err) { } catch (err) {
// tslint:disable-next-line // tslint:disable-next-line
console.error(err) console.error(err)
......
...@@ -146,12 +146,12 @@ export async function setSprCache( ...@@ -146,12 +146,12 @@ export async function setSprCache(
) { ) {
if (sprOptions.dev) return if (sprOptions.dev) return
if (typeof revalidateSeconds !== 'undefined') { if (typeof revalidateSeconds !== 'undefined') {
// TODO: This is really bad. We shouldn't be mutating the manifest from the // TODO: Update this to not mutate the manifest from the
// build. // build.
prerenderManifest.routes[pathname] = { prerenderManifest.routes[pathname] = {
dataRoute: path.posix.join( dataRoute: path.posix.join(
'/_next/data', '/_next/data',
`${pathname === '/' ? '/index' : pathname}.json` `${normalizePagePath(pathname)}.json`
), ),
srcRoute: null, // FIXME: provide actual source route, however, when dynamically appending it doesn't really matter srcRoute: null, // FIXME: provide actual source route, however, when dynamically appending it doesn't really matter
initialRevalidateSeconds: revalidateSeconds, initialRevalidateSeconds: revalidateSeconds,
......
...@@ -285,11 +285,15 @@ export default function onDemandEntryHandler( ...@@ -285,11 +285,15 @@ export default function onDemandEntryHandler(
throw pageNotFoundError(normalizedPagePath) throw pageNotFoundError(normalizedPagePath)
} }
let pageUrl = `/${pagePath let pageUrl = pagePath.replace(/\\/g, '/')
pageUrl = `${pageUrl[0] !== '/' ? '/' : ''}${pageUrl
.replace(new RegExp(`\\.+(?:${pageExtensions.join('|')})$`), '') .replace(new RegExp(`\\.+(?:${pageExtensions.join('|')})$`), '')
.replace(/\\/g, '/')}`.replace(/\/index$/, '') .replace(/\/index$/, '')}`
pageUrl = pageUrl === '' ? '/' : pageUrl pageUrl = pageUrl === '' ? '/' : pageUrl
const bundleFile = pageUrl === '/' ? '/index.js' : `${pageUrl}.js`
const bundleFile = `${normalizePagePath(pageUrl)}.js`
const name = join('static', buildId, 'pages', bundleFile) const name = join('static', buildId, 'pages', bundleFile)
const absolutePagePath = pagePath.startsWith('next/dist/pages') const absolutePagePath = pagePath.startsWith('next/dist/pages')
? require.resolve(pagePath) ? require.resolve(pagePath)
......
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