Unverified Commit 8dd3d2a8 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update handling for basePath to only automatically add (#13817)

As discussed, this streamlines the handling for `basePath` to not automatically strip and add the `basePath` when provided to `next/link` or `router.push/replace` and only automatically adds the `basePath` and when it is manually provided it will cause a 404 which ensures `href` still matches to the pages directory 1-to-1.

This also adds additional test cases that we discussed to ensure this behavior is working as intended

---

Fixes #13902
parent 154c4958
...@@ -162,8 +162,8 @@ class Link extends Component<LinkProps> { ...@@ -162,8 +162,8 @@ class Link extends Component<LinkProps> {
// as per https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html // as per https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
formatUrls = memoizedFormatUrl((href, asHref) => { formatUrls = memoizedFormatUrl((href, asHref) => {
return { return {
href: addBasePath(formatUrl(href)), href: formatUrl(href),
as: asHref ? addBasePath(formatUrl(asHref)) : asHref, as: asHref ? formatUrl(asHref) : asHref,
} }
}) })
...@@ -237,7 +237,10 @@ class Link extends Component<LinkProps> { ...@@ -237,7 +237,10 @@ class Link extends Component<LinkProps> {
render() { render() {
let { children } = this.props let { children } = this.props
const { href, as } = this.formatUrls(this.props.href, this.props.as) let { href, as } = this.formatUrls(this.props.href, this.props.as)
as = as ? addBasePath(as) : as
href = addBasePath(href)
// Deprecated. Warning shown by propType check. If the children provided is a string (<Link>example</Link>) we wrap it in an <a> tag // Deprecated. Warning shown by propType check. If the children provided is a string (<Link>example</Link>) we wrap it in an <a> tag
if (typeof children === 'string') { if (typeof children === 'string') {
children = <a>{children}</a> children = <a>{children}</a>
......
...@@ -3,7 +3,6 @@ import mitt from '../next-server/lib/mitt' ...@@ -3,7 +3,6 @@ import mitt from '../next-server/lib/mitt'
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 { delBasePath } from './../next-server/lib/router/router'
function hasRel(rel, link) { function hasRel(rel, link) {
try { try {
...@@ -105,7 +104,6 @@ export default class PageLoader { ...@@ -105,7 +104,6 @@ export default class PageLoader {
*/ */
getDataHref(href, asPath) { getDataHref(href, asPath) {
const getHrefForSlug = (/** @type string */ path) => { const getHrefForSlug = (/** @type string */ path) => {
path = delBasePath(path)
const dataRoute = getAssetPath(path) const dataRoute = getAssetPath(path)
return `${this.assetPrefix}/_next/data/${this.buildId}${dataRoute}.json` return `${this.assetPrefix}/_next/data/${this.buildId}${dataRoute}.json`
} }
......
...@@ -19,13 +19,11 @@ import { getRouteRegex } from './utils/route-regex' ...@@ -19,13 +19,11 @@ import { getRouteRegex } from './utils/route-regex'
const basePath = (process.env.__NEXT_ROUTER_BASEPATH as string) || '' const basePath = (process.env.__NEXT_ROUTER_BASEPATH as string) || ''
export function addBasePath(path: string): string { export function addBasePath(path: string): string {
return path.indexOf(basePath) !== 0 ? basePath + path : path return `${basePath}${path}`
} }
export function delBasePath(path: string): string { export function delBasePath(path: string): string {
return path.indexOf(basePath) === 0 return path.substr(basePath.length)
? path.substr(basePath.length) || '/'
: path
} }
function toRoute(path: string): string { function toRoute(path: string): string {
...@@ -37,6 +35,21 @@ const prepareRoute = (path: string) => ...@@ -37,6 +35,21 @@ const prepareRoute = (path: string) =>
type Url = UrlObject | string type Url = UrlObject | string
function prepareUrlAs(url: Url, as: Url) {
// If url and as provided as an object representation,
// we'll format them into the string version here.
url = typeof url === 'object' ? formatWithValidation(url) : url
as = typeof as === 'object' ? formatWithValidation(as) : as
url = addBasePath(url)
as = as ? addBasePath(as) : as
return {
url,
as,
}
}
type ComponentRes = { page: ComponentType; mod: any } type ComponentRes = { page: ComponentType; mod: any }
export type BaseRouter = { export type BaseRouter = {
...@@ -235,7 +248,7 @@ export default class Router implements BaseRouter { ...@@ -235,7 +248,7 @@ export default class Router implements BaseRouter {
// we have to register the initial route upon initialization // we have to register the initial route upon initialization
this.changeState( this.changeState(
'replaceState', 'replaceState',
formatWithValidation({ pathname, query }), formatWithValidation({ pathname: addBasePath(pathname), query }),
as as
) )
} }
...@@ -269,7 +282,7 @@ export default class Router implements BaseRouter { ...@@ -269,7 +282,7 @@ export default class Router implements BaseRouter {
const { pathname, query } = this const { pathname, query } = this
this.changeState( this.changeState(
'replaceState', 'replaceState',
formatWithValidation({ pathname, query }), formatWithValidation({ pathname: addBasePath(pathname), query }),
getURL() getURL()
) )
return return
...@@ -300,7 +313,7 @@ export default class Router implements BaseRouter { ...@@ -300,7 +313,7 @@ export default class Router implements BaseRouter {
) )
} }
} }
this.replace(url, as, options) this.change('replaceState', url, as, options)
} }
update(route: string, mod: any) { update(route: string, mod: any) {
...@@ -346,6 +359,7 @@ export default class Router implements BaseRouter { ...@@ -346,6 +359,7 @@ export default class Router implements BaseRouter {
* @param options object you can define `shallow` and other options * @param options object you can define `shallow` and other options
*/ */
push(url: Url, as: Url = url, options = {}) { push(url: Url, as: Url = url, options = {}) {
;({ url, as } = prepareUrlAs(url, as))
return this.change('pushState', url, as, options) return this.change('pushState', url, as, options)
} }
...@@ -356,13 +370,14 @@ export default class Router implements BaseRouter { ...@@ -356,13 +370,14 @@ export default class Router implements BaseRouter {
* @param options object you can define `shallow` and other options * @param options object you can define `shallow` and other options
*/ */
replace(url: Url, as: Url = url, options = {}) { replace(url: Url, as: Url = url, options = {}) {
;({ url, as } = prepareUrlAs(url, as))
return this.change('replaceState', url, as, options) return this.change('replaceState', url, as, options)
} }
change( change(
method: HistoryMethod, method: HistoryMethod,
_url: Url, url: string,
_as: Url, as: string,
options: any options: any
): Promise<boolean> { ): Promise<boolean> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
...@@ -374,18 +389,6 @@ export default class Router implements BaseRouter { ...@@ -374,18 +389,6 @@ export default class Router implements BaseRouter {
performance.mark('routeChange') performance.mark('routeChange')
} }
// If url and as provided as an object representation,
// we'll format them into the string version here.
let url = typeof _url === 'object' ? formatWithValidation(_url) : _url
let as = typeof _as === 'object' ? formatWithValidation(_as) : _as
// parse url parts without basePath since pathname should map 1-1 with
// pages dir
const { pathname, query, protocol } = parse(delBasePath(url), true)
url = addBasePath(url)
as = addBasePath(as)
// Add the ending slash to the paths. So, we can serve the // Add the ending slash to the paths. So, we can serve the
// "<page>/index.html" directly for the SSR page. // "<page>/index.html" directly for the SSR page.
if (process.env.__NEXT_EXPORT_TRAILING_SLASH) { if (process.env.__NEXT_EXPORT_TRAILING_SLASH) {
...@@ -414,6 +417,13 @@ export default class Router implements BaseRouter { ...@@ -414,6 +417,13 @@ export default class Router implements BaseRouter {
return resolve(true) return resolve(true)
} }
let { pathname, query, protocol } = parse(url, true)
// url and as should always be prefixed with basePath by this
// 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 ? delBasePath(pathname) : pathname
if (!pathname || protocol) { if (!pathname || protocol) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
throw new Error( throw new Error(
...@@ -488,7 +498,7 @@ export default class Router implements BaseRouter { ...@@ -488,7 +498,7 @@ export default class Router implements BaseRouter {
!(routeInfo.Component as any).getInitialProps !(routeInfo.Component as any).getInitialProps
} }
this.set(route, pathname, query, as, routeInfo).then(() => { this.set(route, pathname!, query, as, routeInfo).then(() => {
if (error) { if (error) {
Router.events.emit('routeChangeError', error, as) Router.events.emit('routeChangeError', error, as)
throw error throw error
...@@ -760,9 +770,9 @@ export default class Router implements BaseRouter { ...@@ -760,9 +770,9 @@ export default class Router implements BaseRouter {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
return return
} }
const route = delBasePath(toRoute(pathname)) const route = toRoute(pathname)
Promise.all([ Promise.all([
this.pageLoader.prefetchData(url, delBasePath(asPath)), this.pageLoader.prefetchData(url, asPath),
this.pageLoader[options.priority ? 'loadPage' : 'prefetch'](route), this.pageLoader[options.priority ? 'loadPage' : 'prefetch'](route),
]).then(() => resolve(), reject) ]).then(() => resolve(), reject)
}) })
...@@ -773,7 +783,6 @@ export default class Router implements BaseRouter { ...@@ -773,7 +783,6 @@ export default class Router implements BaseRouter {
const cancel = (this.clc = () => { const cancel = (this.clc = () => {
cancelled = true cancelled = true
}) })
route = delBasePath(route)
const componentResult = await this.pageLoader.loadPage(route) const componentResult = await this.pageLoader.loadPage(route)
......
export default () => <p>hello from another</p>
import Link from 'next/link'
export default () => (
<>
<Link href="/docs/other-page">
<a id="other-page-link">
<h1>Hello World</h1>
</a>
</Link>
</>
)
...@@ -38,8 +38,118 @@ const runTests = (context, dev = false) => { ...@@ -38,8 +38,118 @@ const runTests = (context, dev = false) => {
) )
expect(routesManifest.basePath).toBe('/docs') expect(routesManifest.basePath).toBe('/docs')
}) })
it('should prefetch pages correctly when manually called', async () => {
const browser = await webdriver(context.appPort, '/docs/other-page')
await browser.eval('window.next.router.prefetch("/gssp")')
await check(
async () => {
const links = await browser.elementsByCss('link[rel=prefetch]')
for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('gssp')) {
return true
}
}
return false
},
{
test(result) {
return result === true
},
}
)
})
it('should prefetch pages correctly in viewport with <Link>', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
await browser.eval('window.next.router.prefetch("/gssp")')
await check(
async () => {
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = new Set()
for (const link of links) {
const href = await link.getAttribute('href')
if (href.match(/(gsp|gssp|other-page)\.js$/)) {
found.add(href)
}
if (href.match(/gsp\.json$/)) {
found.add(href)
}
}
return found
},
{
test(result) {
return result.size === 4
},
}
)
})
} }
it('should work with nested folder with same name as basePath', async () => {
const html = await renderViaHTTP(context.appPort, '/docs/docs/another')
expect(html).toContain('hello from another')
const browser = await webdriver(context.appPort, '/docs/hello')
await browser.eval('window.next.router.push("/docs/another")')
await check(() => browser.elementByCss('p').text(), /hello from another/)
})
it('should 404 when manually adding basePath with <Link>', async () => {
const browser = await webdriver(
context.appPort,
'/docs/invalid-manual-basepath'
)
await browser.eval('window.beforeNav = "hi"')
await browser.elementByCss('#other-page-link').click()
await check(() => browser.eval('window.beforeNav'), {
test(content) {
return content !== 'hi'
},
})
const html = await browser.eval('document.documentElement.innerHTML')
expect(html).toContain('This page could not be found')
})
it('should 404 when manually adding basePath with router.push', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
await browser.eval('window.beforeNav = "hi"')
await browser.eval('window.next.router.push("/docs/other-page")')
await check(() => browser.eval('window.beforeNav'), {
test(content) {
return content !== 'hi'
},
})
const html = await browser.eval('document.documentElement.innerHTML')
expect(html).toContain('This page could not be found')
})
it('should 404 when manually adding basePath with router.replace', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
await browser.eval('window.beforeNav = "hi"')
await browser.eval('window.next.router.replace("/docs/other-page")')
await check(() => browser.eval('window.beforeNav'), {
test(content) {
return content !== 'hi'
},
})
const html = await browser.eval('document.documentElement.innerHTML')
expect(html).toContain('This page could not be found')
})
it('should show the hello page under the /docs prefix', async () => { it('should show the hello page under the /docs prefix', async () => {
const browser = await webdriver(context.appPort, '/docs/hello') const browser = await webdriver(context.appPort, '/docs/hello')
try { try {
......
...@@ -247,7 +247,7 @@ describe('Prefetching Links in viewport', () => { ...@@ -247,7 +247,7 @@ describe('Prefetching Links in viewport', () => {
expect(calledPrefetch).toBe(true) expect(calledPrefetch).toBe(true)
}) })
it('should correctly omit pre-generaged dynamic pages from SSG manifest', async () => { it('should correctly omit pre-generated dynamic pages from SSG manifest', async () => {
const content = await readFile( const content = await readFile(
join(appDir, '.next', 'static', 'test-build', '_ssgManifest.js'), join(appDir, '.next', 'static', 'test-build', '_ssgManifest.js'),
'utf8' 'utf8'
......
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