Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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