Skip to content

Commit

Permalink
Split page path utils
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed Apr 30, 2022
1 parent b92879d commit f6020f0
Show file tree
Hide file tree
Showing 31 changed files with 166 additions and 155 deletions.
4 changes: 2 additions & 2 deletions packages/next/build/entries.ts
Expand Up @@ -15,11 +15,11 @@ import { EDGE_RUNTIME_WEBPACK } from '../shared/lib/constants'
import { MIDDLEWARE_ROUTE } from '../lib/constants'
import { __ApiPreviewProps } from '../server/api-utils'
import { isTargetLikeServerless } from '../server/utils'
import { normalizePagePath } from '../server/page-path-utils'
import { normalizePathSep } from '../server/page-path-utils'
import { warn } from './output/log'
import { parse } from '../build/swc'
import { isFlightPage, withoutRSCExtensions } from './utils'
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'

type ObjectValue<T> = T extends { [key: string]: infer V } ? V : never

Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/index.ts
Expand Up @@ -63,7 +63,7 @@ import { __ApiPreviewProps } from '../server/api-utils'
import loadConfig from '../server/config'
import { isTargetLikeServerless } from '../server/utils'
import { BuildManifest } from '../server/get-page-files'
import { normalizePagePath } from '../server/page-path-utils'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { getPagePath } from '../server/require'
import * as ciEnvironment from '../telemetry/ci-info'
import {
Expand Down
6 changes: 2 additions & 4 deletions packages/next/build/utils.ts
Expand Up @@ -26,10 +26,6 @@ import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
import escapePathDelimiters from '../shared/lib/router/utils/escape-path-delimiters'
import { findPageFile } from '../server/lib/find-page-file'
import { GetStaticPaths, PageConfig } from 'next/types'
import {
denormalizePagePath,
normalizePagePath,
} from '../server/page-path-utils'
import { BuildManifest } from '../server/get-page-files'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
import { UnwrapPromise } from '../lib/coalesced-function'
Expand All @@ -42,6 +38,8 @@ import isError from '../lib/is-error'
import { recursiveDelete } from '../lib/recursive-delete'
import { Sema } from 'next/dist/compiled/async-sema'
import { MiddlewareManifest } from './webpack/plugins/middleware-plugin'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'

const { builtinModules } = require('module')
const RESERVED_PAGE = /^\/(_app|_error|_document|api(\/|$))/
Expand Down
Expand Up @@ -6,7 +6,7 @@ import { getUtils, vercelHeader, ServerlessHandlerCtx } from './utils'

import { renderToHTML } from '../../../../server/render'
import { tryGetPreviewData } from '../../../../server/api-utils/node'
import { denormalizePagePath } from '../../../../server/page-path-utils'
import { denormalizePagePath } from '../../../../shared/lib/page-path/denormalize-page-path'
import { setLazyProp, getCookieParser } from '../../../../server/api-utils'
import { getRedirectStatus } from '../../../../lib/load-custom-routes'
import getRouteNoAssetPath from '../../../../shared/lib/router/utils/get-route-from-asset-path'
Expand Down
Expand Up @@ -23,7 +23,7 @@ import { __ApiPreviewProps } from '../../../../server/api-utils'
import { acceptLanguage } from '../../../../server/accept-header'
import { detectLocaleCookie } from '../../../../shared/lib/i18n/detect-locale-cookie'
import { detectDomainLocale } from '../../../../shared/lib/i18n/detect-domain-locale'
import { denormalizePagePath } from '../../../../server/page-path-utils'
import { denormalizePagePath } from '../../../../shared/lib/page-path/denormalize-page-path'
import cookie from 'next/dist/compiled/cookie'
import { TEMPORARY_REDIRECT_STATUS } from '../../../../shared/lib/constants'
import { addRequestMeta } from '../../../../server/request-meta'
Expand Down
6 changes: 2 additions & 4 deletions packages/next/export/index.ts
Expand Up @@ -34,10 +34,8 @@ import { NextConfigComplete } from '../server/config-shared'
import { eventCliSession } from '../telemetry/events'
import { hasNextSupport } from '../telemetry/ci-info'
import { Telemetry } from '../telemetry/storage'
import {
normalizePagePath,
denormalizePagePath,
} from '../server/page-path-utils'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import { loadEnvConfig } from '@next/env'
import { PrerenderManifest } from '../build'
import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/export/worker.ts
Expand Up @@ -13,7 +13,7 @@ import { loadComponents } from '../server/load-components'
import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
import { getRouteMatcher } from '../shared/lib/router/utils/route-matcher'
import { getRouteRegex } from '../shared/lib/router/utils/route-regex'
import { normalizePagePath } from '../server/page-path-utils'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { SERVER_PROPS_EXPORT_ERROR } from '../lib/constants'
import '../server/node-polyfill-fetch'
import { requireFontManifest } from '../server/require'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/base-server.ts
Expand Up @@ -50,7 +50,7 @@ import { isBlockedPage, isBot } from './utils'
import RenderResult from './render-result'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
import getRouteFromAssetPath from '../shared/lib/router/utils/get-route-from-asset-path'
import { denormalizePagePath } from './page-path-utils'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import * as Log from '../build/output/log'
import { detectDomainLocale } from '../shared/lib/i18n/detect-domain-locale'
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/dev/hot-reloader.ts
Expand Up @@ -26,7 +26,8 @@ import {
entries,
onDemandEntryHandler,
} from './on-demand-entry-handler'
import { denormalizePagePath, normalizePathSep } from '../page-path-utils'
import { denormalizePagePath } from '../../shared/lib/page-path/denormalize-page-path'
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep'
import getRouteFromEntrypoint from '../get-route-from-entrypoint'
import { fileExists } from '../../lib/file-exists'
import { difference } from '../../build/utils'
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/dev/next-dev-server.ts
Expand Up @@ -40,7 +40,8 @@ import {
isDynamicRoute,
} from '../../shared/lib/router/utils'
import Server, { WrappedBuildError } from '../next-server'
import { absolutePathToPage, normalizePagePath } from '../page-path-utils'
import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path'
import { absolutePathToPage } from '../../shared/lib/page-path/absolute-path-to-page'
import Router from '../router'
import { getPathMatch } from '../../shared/lib/router/utils/path-match'
import { hasBasePath, replaceBasePath } from '../router-utils'
Expand Down
6 changes: 4 additions & 2 deletions packages/next/server/dev/on-demand-entry-handler.ts
Expand Up @@ -5,8 +5,10 @@ import { EventEmitter } from 'events'
import { findPageFile } from '../lib/find-page-file'
import { getPageRuntime, runDependingOnPageType } from '../../build/entries'
import { join, posix } from 'path'
import { normalizePagePath, normalizePathSep } from '../page-path-utils'
import { ensureLeadingSlash, removePagePathTail } from '../page-path-utils'
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep'
import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path'
import { ensureLeadingSlash } from '../../shared/lib/page-path/ensure-leading-slash'
import { removePagePathTail } from '../../shared/lib/page-path/remove-page-path-tail'
import { pageNotFoundError } from '../require'
import { reportTrigger } from '../../build/output'
import getRouteFromEntrypoint from '../get-route-from-entrypoint'
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/get-page-files.ts
@@ -1,4 +1,5 @@
import { normalizePagePath, denormalizePagePath } from './page-path-utils'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'

export type BuildManifest = {
devFiles: readonly string[]
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/incremental-cache.ts
Expand Up @@ -3,7 +3,7 @@ import type { CacheFs } from '../shared/lib/utils'
import LRUCache from 'next/dist/compiled/lru-cache'
import path from '../shared/lib/isomorphic/path'
import { PrerenderManifest } from '../build'
import { normalizePagePath } from './page-path-utils'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { IncrementalCacheValue, IncrementalCacheEntry } from './response-cache'

function toRoute(pathname: string): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/lib/find-page-file.ts
@@ -1,5 +1,5 @@
import { fileExists } from '../../lib/file-exists'
import { getPagePaths } from '../page-path-utils'
import { getPagePaths } from '../../shared/lib/page-path/get-page-paths'
import { nonNullable } from '../../lib/non-nullable'
import { join, sep, normalize } from 'path'
import { promises } from 'fs'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/next-server.ts
Expand Up @@ -55,7 +55,7 @@ import BaseServer, {
stringifyQuery,
} from './base-server'
import { getMiddlewareInfo, getPagePath, requireFontManifest } from './require'
import { normalizePagePath } from './page-path-utils'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { loadComponents } from './load-components'
import isError, { getProperError } from '../lib/is-error'
import { FontManifest } from './font-utils'
Expand Down
123 changes: 0 additions & 123 deletions packages/next/server/page-path-utils.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/next/server/render.tsx
Expand Up @@ -55,7 +55,8 @@ import {
loadGetInitialProps,
} from '../shared/lib/utils'
import { HtmlContext } from '../shared/lib/html-context'
import { normalizePagePath, denormalizePagePath } from './page-path-utils'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import { getRequestMeta, NextParsedUrlQuery } from './request-meta'
import {
allowedStatusCodes,
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/require.ts
Expand Up @@ -7,8 +7,9 @@ import {
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
} from '../shared/lib/constants'
import { normalizePagePath, denormalizePagePath } from './page-path-utils'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { MiddlewareManifest } from '../build/webpack/plugins/middleware-plugin'
import type { WasmBinding } from '../build/webpack/loaders/get-module-build-info'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/shared/lib/i18n/get-locale-metadata.ts
@@ -1,5 +1,5 @@
import { acceptLanguage } from '../../../server/accept-header'
import { denormalizePagePath } from '../../../server/page-path-utils'
import { denormalizePagePath } from '../page-path/denormalize-page-path'
import { detectDomainLocale } from './detect-domain-locale'
import { formatUrl } from '../router/utils/format-url'
import { normalizeLocalePath } from './normalize-locale-path'
Expand Down
25 changes: 25 additions & 0 deletions packages/next/shared/lib/page-path/absolute-path-to-page.ts
@@ -0,0 +1,25 @@
import { ensureLeadingSlash } from './ensure-leading-slash'
import { normalizePathSep } from './normalize-path-sep'
import { relative } from '../isomorphic/path'
import { removePagePathTail } from './remove-page-path-tail'

/**
* Given the absolute path to the pages folder, an absolute file path for a
* page and the page extensions, this function will return the page path
* relative to the pages folder. It doesn't consider index tail. Example:
* - `/Users/rick/my-project/pages/foo/bar/baz.js` -> `/foo/bar/baz`
*
* @param pagesDir Absolute path to the pages folder.
* @param filepath Absolute path to the page.
* @param extensions Extensions allowed for the page.
*/
export function absolutePathToPage(
pagesDir: string,
pagePath: string,
extensions: string[]
) {
return removePagePathTail(
normalizePathSep(ensureLeadingSlash(relative(pagesDir, pagePath))),
extensions
)
}
19 changes: 19 additions & 0 deletions packages/next/shared/lib/page-path/denormalize-page-path.ts
@@ -0,0 +1,19 @@
import { isDynamicRoute } from '../router/utils'
import { normalizePathSep } from './normalize-path-sep'

/**
* Performs the opposite transformation of `normalizePagePath`. Note that
* this function is not idempotent either in cases where there are multiple
* leading `/index` for the page. Examples:
* - `/index` -> `/`
* - `/index/foo` -> `/foo`
* - `/index/index` -> `/index`
*/
export function denormalizePagePath(page: string) {
let _page = normalizePathSep(page)
return _page.startsWith('/index/') && !isDynamicRoute(_page)
? _page.slice(6)
: _page !== '/index'
? _page
: '/'
}
7 changes: 7 additions & 0 deletions packages/next/shared/lib/page-path/ensure-leading-slash.ts
@@ -0,0 +1,7 @@
/**
* For a given page path, this function ensures that there is a leading slash.
* If there is not a leading slash, one is added, otherwise it is noop.
*/
export function ensureLeadingSlash(path: string) {
return path.startsWith('/') ? path : `/${path}`
}
22 changes: 22 additions & 0 deletions packages/next/shared/lib/page-path/get-page-paths.ts
@@ -0,0 +1,22 @@
import { denormalizePagePath } from './denormalize-page-path'
import { flatten } from '../flatten'
import { join } from '../isomorphic/path'

/**
* Calculate all possible pagePaths for a given normalized pagePath along with
* allowed extensions. This can be used to check which one of the files exists
* and to debug inspected locations.
*
* @param normalizedPagePath Normalized page path (it will denormalize).
* @param extensions Allowed extensions.
*/
export function getPagePaths(normalizedPagePath: string, extensions: string[]) {
const page = denormalizePagePath(normalizedPagePath)
return flatten(
extensions.map((extension) => {
return !normalizedPagePath.endsWith('/index')
? [`${page}.${extension}`, join(page, `index.${extension}`)]
: [join(page, `index.${extension}`)]
})
)
}

0 comments on commit f6020f0

Please sign in to comment.