Skip to content

Commit

Permalink
Update to use existing util to de-dupe path check (vercel#10431)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ijjk authored and chibicode committed Feb 11, 2020
1 parent 0549c0e commit 63c9281
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 22 deletions.
3 changes: 2 additions & 1 deletion packages/next/build/entries.ts
Expand Up @@ -6,6 +6,7 @@ import { API_ROUTE, DOT_NEXT_ALIAS, PAGES_DIR_ALIAS } from '../lib/constants'
import { isTargetLikeServerless } from '../next-server/server/config'
import { warn } from './output/log'
import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader'
import { normalizePagePath } from '../next-server/server/normalize-page-path'

type PagesMapping = {
[page: string]: string
Expand Down Expand Up @@ -91,7 +92,7 @@ export function createEntrypoints(

Object.keys(pages).forEach(page => {
const absolutePagePath = pages[page]
const bundleFile = page === '/' ? '/index.js' : `${page}.js`
const bundleFile = `${normalizePagePath(page)}.js`
const isApiRoute = page.match(API_ROUTE)

const bundlePath = join('static', buildId, 'pages', bundleFile)
Expand Down
20 changes: 9 additions & 11 deletions packages/next/build/index.ts
Expand Up @@ -67,6 +67,7 @@ import {
} from './utils'
import getBaseWebpackConfig from './webpack-config'
import { writeBuildId } from './write-build-id'
import { normalizePagePath } from '../next-server/server/normalize-page-path'

const fsAccess = promisify(fs.access)
const fsUnlink = promisify(fs.unlink)
Expand Down Expand Up @@ -435,7 +436,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
const analysisBegin = process.hrtime()
await Promise.all(
pageKeys.map(async page => {
const actualPage = page === '/' ? '/index' : page
const actualPage = normalizePagePath(page)
const [selfSize, allSize] = await getPageSizeInKb(
actualPage,
distDir,
Expand Down Expand Up @@ -554,10 +555,11 @@ export default async function build(dir: string, conf = null): Promise<void> {
routesManifest.serverPropsRoutes = {}

for (const page of serverPropsPages) {
const pagePath = normalizePagePath(page)
const dataRoute = path.posix.join(
'/_next/data',
buildId,
`${page === '/' ? '/index' : page}.json`
`${pagePath}.json`
)

routesManifest.serverPropsRoutes[page] = {
Expand All @@ -571,7 +573,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
`^${path.posix.join(
'/_next/data',
escapeStringRegexp(buildId),
`${page === '/' ? '/index' : page}.json`
`${pagePath}.json`
)}$`
).source,
}
Expand Down Expand Up @@ -709,7 +711,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
for (const page of combinedPages) {
const isSsg = ssgPages.has(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 specific prerenders of these.
if (!(isSsg && isDynamic)) {
Expand All @@ -729,11 +731,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
initialRevalidateSeconds:
exportConfig.initialPageRevalidationMap[page],
srcRoute: null,
dataRoute: path.posix.join(
'/_next/data',
buildId,
`${page === '/' ? '/index' : page}.json`
),
dataRoute: path.posix.join('/_next/data', buildId, `${file}.json`),
}
} else {
// For a dynamic SSG page, we did not copy its html nor data exports.
Expand All @@ -750,7 +748,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
dataRoute: path.posix.join(
'/_next/data',
buildId,
`${route === '/' ? '/index' : route}.json`
`${normalizePagePath(route)}.json`
),
}
}
Expand Down Expand Up @@ -783,7 +781,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
const dataRoute = path.posix.join(
'/_next/data',
buildId,
`${tbdRoute === '/' ? '/index' : tbdRoute}.json`
`${normalizePagePath(tbdRoute)}.json`
)

finalDynamicRoutes[tbdRoute] = {
Expand Down
3 changes: 2 additions & 1 deletion packages/next/export/index.ts
Expand Up @@ -33,6 +33,7 @@ import loadConfig, {
} from '../next-server/server/config'
import { eventVersion } from '../telemetry/events'
import { Telemetry } from '../telemetry/storage'
import { normalizePagePath } from '../next-server/server/normalize-page-path'

const mkdirp = promisify(mkdirpModule)
const copyFile = promisify(copyFileOrig)
Expand Down Expand Up @@ -352,7 +353,7 @@ export default async function(
if (!options.buildExport && prerenderManifest) {
await Promise.all(
Object.keys(prerenderManifest.routes).map(async route => {
route = route === '/' ? '/index' : route
route = normalizePagePath(route)
const orig = join(distPagesDir, route)
const htmlDest = join(
outDir,
Expand Down
3 changes: 2 additions & 1 deletion packages/next/export/worker.js
Expand Up @@ -9,6 +9,7 @@ import { loadComponents } from '../next-server/server/load-components'
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
import { getRouteMatcher } from '../next-server/lib/router/utils/route-matcher'
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 writeFileP = promisify(writeFile)
Expand Down Expand Up @@ -39,7 +40,7 @@ export default async function({
try {
const { query: originalQuery = {} } = pathMap
const { page } = pathMap
const filePath = path === '/' ? '/index' : path
const filePath = normalizePagePath(path)
const ampPath = `${filePath}.amp`
let query = { ...originalQuery }
let params
Expand Down
3 changes: 2 additions & 1 deletion packages/next/next-server/server/next-server.ts
Expand Up @@ -52,6 +52,7 @@ import {
Header,
getRedirectStatus,
} from '../../lib/check-custom-routes'
import { normalizePagePath } from './normalize-page-path'

const getCustomRouteMatcher = pathMatch(true)

Expand Down Expand Up @@ -815,7 +816,7 @@ export default class Server {
return await loadComponents(
this.distDir,
this.buildId,
(pathname === '/' ? '/index' : pathname) + '.amp',
normalizePagePath(pathname) + '.amp',
serverless
)
} catch (err) {
Expand Down
4 changes: 3 additions & 1 deletion packages/next/next-server/server/normalize-page-path.ts
Expand Up @@ -11,7 +11,9 @@ export function normalizePagePath(page: string): string {
// Throw when using ../ etc in the pathname
const resolvedPage = posix.normalize(page)
if (page !== resolvedPage) {
throw new Error('Requested and resolved page mismatch')
throw new Error(
`Requested and resolved page mismatch: ${page} ${resolvedPage}`
)
}
return page
}
1 change: 0 additions & 1 deletion packages/next/next-server/server/require.ts
Expand Up @@ -30,7 +30,6 @@ export function getPagePath(

try {
page = normalizePagePath(page)
page = page === '/' ? '/index' : page
} catch (err) {
// tslint:disable-next-line
console.error(err)
Expand Down
4 changes: 2 additions & 2 deletions packages/next/next-server/server/spr-cache.ts
Expand Up @@ -146,12 +146,12 @@ export async function setSprCache(
) {
if (sprOptions.dev) return
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.
prerenderManifest.routes[pathname] = {
dataRoute: path.posix.join(
'/_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
initialRevalidateSeconds: revalidateSeconds,
Expand Down
10 changes: 7 additions & 3 deletions packages/next/server/on-demand-entry-handler.ts
Expand Up @@ -285,11 +285,15 @@ export default function onDemandEntryHandler(
throw pageNotFoundError(normalizedPagePath)
}

let pageUrl = `/${pagePath
let pageUrl = pagePath.replace(/\\/g, '/')

pageUrl = `${pageUrl[0] !== '/' ? '/' : ''}${pageUrl
.replace(new RegExp(`\\.+(?:${pageExtensions.join('|')})$`), '')
.replace(/\\/g, '/')}`.replace(/\/index$/, '')
.replace(/\/index$/, '')}`

pageUrl = pageUrl === '' ? '/' : pageUrl
const bundleFile = pageUrl === '/' ? '/index.js' : `${pageUrl}.js`

const bundleFile = `${normalizePagePath(pageUrl)}.js`
const name = join('static', buildId, 'pages', bundleFile)
const absolutePagePath = pagePath.startsWith('next/dist/pages')
? require.resolve(pagePath)
Expand Down

0 comments on commit 63c9281

Please sign in to comment.