Skip to content

Commit

Permalink
Merge branch 'response-helpers' of https://github.com/botv/next.js in…
Browse files Browse the repository at this point in the history
…to response-helpers
  • Loading branch information
botv committed Jun 30, 2020
2 parents dff7b14 + 88cdad2 commit 001dcff
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 45 deletions.
4 changes: 2 additions & 2 deletions packages/next/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { findPageFile } from '../server/lib/find-page-file'
import { GetStaticPaths } from 'next/types'
import { denormalizePagePath } from '../next-server/server/normalize-page-path'
import { BuildManifest } from '../next-server/server/get-page-files'
import { normalizeTrailingSlash } from '../next-server/lib/router/normalize-trailing-slash'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'

const fileGzipStats: { [k: string]: Promise<number> } = {}
const fsStatGzip = (file: string) => {
Expand Down Expand Up @@ -574,7 +574,7 @@ export async function buildStaticPaths(
// For a string-provided path, we must make sure it matches the dynamic
// route.
if (typeof entry === 'string') {
entry = normalizeTrailingSlash(entry)
entry = removePathTrailingSlash(entry)
const result = _routeMatcher(entry)
if (!result) {
throw new Error(
Expand Down
12 changes: 2 additions & 10 deletions packages/next/client/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../next-server/lib/utils'
import Router from './router'
import { addBasePath } from '../next-server/lib/router/router'
import { normalizeTrailingSlash } from '../next-server/lib/router/normalize-trailing-slash'
import { normalizeTrailingSlash } from './normalize-trailing-slash'

function isLocal(href: string): boolean {
const url = parse(href, false, true)
Expand Down Expand Up @@ -41,19 +41,11 @@ function memoizedFormatUrl(formatFunc: (href: Url, as?: Url) => FormatResult) {
}
}

function formatTrailingSlash(url: UrlObject): UrlObject {
return Object.assign({}, url, {
pathname:
url.pathname &&
normalizeTrailingSlash(url.pathname, !!process.env.__NEXT_TRAILING_SLASH),
})
}

function formatUrl(url: Url): string {
return (
url &&
formatWithValidation(
formatTrailingSlash(typeof url === 'object' ? url : parse(url))
normalizeTrailingSlash(typeof url === 'object' ? url : parse(url))
)
)
}
Expand Down
25 changes: 25 additions & 0 deletions packages/next/client/normalize-trailing-slash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { UrlObject } from 'url'

export function removePathTrailingSlash(path: string): string {
return path.endsWith('/') && path !== '/' ? path.slice(0, -1) : path
}

const normalizePathTrailingSlash = process.env.__NEXT_TRAILING_SLASH
? (path: string): string => {
if (/\.[^/]+\/?$/.test(path)) {
return removePathTrailingSlash(path)
} else if (path.endsWith('/')) {
return path
} else {
return path + '/'
}
}
: removePathTrailingSlash

export function normalizeTrailingSlash(url: UrlObject): UrlObject {
const normalizedPath =
url.pathname && normalizePathTrailingSlash(url.pathname)
return url.pathname === normalizedPath
? url
: Object.assign({}, url, { pathname: normalizedPath })
}
20 changes: 0 additions & 20 deletions packages/next/next-server/lib/router/normalize-trailing-slash.ts

This file was deleted.

17 changes: 6 additions & 11 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import {
import { isDynamicRoute } from './utils/is-dynamic'
import { getRouteMatcher } from './utils/route-matcher'
import { getRouteRegex } from './utils/route-regex'
import { normalizeTrailingSlash } from './normalize-trailing-slash'
import {
normalizeTrailingSlash,
removePathTrailingSlash,
} from '../../../client/normalize-trailing-slash'

const basePath = (process.env.__NEXT_ROUTER_BASEPATH as string) || ''

Expand All @@ -37,18 +40,10 @@ function prepareRoute(path: string) {

type Url = UrlObject | string

function formatTrailingSlash(url: UrlObject): UrlObject {
return Object.assign({}, url, {
pathname:
url.pathname &&
normalizeTrailingSlash(url.pathname, !!process.env.__NEXT_TRAILING_SLASH),
})
}

function formatUrl(url: Url): string {
return url
? formatWithValidation(
formatTrailingSlash(typeof url === 'object' ? url : parse(url))
normalizeTrailingSlash(typeof url === 'object' ? url : parse(url))
)
: url
}
Expand Down Expand Up @@ -427,7 +422,7 @@ export default class Router implements BaseRouter {
// point by either next/link or router.push/replace so strip the
// basePath from the pathname to match the pages dir 1-to-1
pathname = pathname
? normalizeTrailingSlash(delBasePath(pathname), false)
? removePathTrailingSlash(delBasePath(pathname))
: pathname

if (!pathname || protocol) {
Expand Down
4 changes: 2 additions & 2 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import { compile as compilePathToRegex } from 'next/dist/compiled/path-to-regexp
import { loadEnvConfig } from '../../lib/load-env-config'
import './node-polyfill-fetch'
import { PagesManifest } from '../../build/webpack/plugins/pages-manifest-plugin'
import { normalizeTrailingSlash } from '../lib/router/normalize-trailing-slash'
import { removePathTrailingSlash } from '../../client/normalize-trailing-slash'
import getRouteFromAssetPath from '../lib/router/utils/get-route-from-asset-path'

const getCustomRouteMatcher = pathMatch(true)
Expand Down Expand Up @@ -586,7 +586,7 @@ export default class Server {
}

// next.js core assumes page path without trailing slash
pathname = normalizeTrailingSlash(pathname, false)
pathname = removePathTrailingSlash(pathname)

if (params?.path?.[0] === 'api') {
const handled = await this.handleApiRequest(
Expand Down

0 comments on commit 001dcff

Please sign in to comment.