Unverified Commit 9b8a9dea authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix basePath not being applied for GS(S)P data routes (#12200)

parent 5d8968f6
...@@ -3,6 +3,7 @@ import mitt from '../next-server/lib/mitt' ...@@ -3,6 +3,7 @@ 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 {
...@@ -96,10 +97,12 @@ export default class PageLoader { ...@@ -96,10 +97,12 @@ export default class PageLoader {
* @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes * @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes
*/ */
getDataHref(href, asPath) { getDataHref(href, asPath) {
const getHrefForSlug = (/** @type string */ path) => const getHrefForSlug = (/** @type string */ path) => {
`${this.assetPrefix}/_next/data/${this.buildId}${ path = delBasePath(path)
return `${this.assetPrefix}/_next/data/${this.buildId}${
path === '/' ? '/index' : path path === '/' ? '/index' : path
}.json` }.json`
}
const { pathname: hrefPathname, query } = parse(href, true) const { pathname: hrefPathname, query } = parse(href, true)
const { pathname: asPathname } = parse(asPath) const { pathname: asPathname } = parse(asPath)
......
...@@ -22,7 +22,7 @@ export function addBasePath(path: string): string { ...@@ -22,7 +22,7 @@ export function addBasePath(path: string): string {
return path.indexOf(basePath) !== 0 ? basePath + path : path return path.indexOf(basePath) !== 0 ? basePath + path : path
} }
function delBasePath(path: string): string { export function delBasePath(path: string): string {
return path.indexOf(basePath) === 0 return path.indexOf(basePath) === 0
? path.substr(basePath.length) || '/' ? path.substr(basePath.length) || '/'
: path : path
...@@ -91,8 +91,10 @@ function fetchNextData( ...@@ -91,8 +91,10 @@ function fetchNextData(
function getResponse(): Promise<any> { function getResponse(): Promise<any> {
return fetch( return fetch(
formatWithValidation({ formatWithValidation({
// @ts-ignore __NEXT_DATA__ pathname: addBasePath(
pathname: `/_next/data/${__NEXT_DATA__.buildId}${pathname}.json`, // @ts-ignore __NEXT_DATA__
`/_next/data/${__NEXT_DATA__.buildId}${delBasePath(pathname)}.json`
),
query, query,
}), }),
{ {
......
export const getStaticProps = () => {
return {
props: {
hello: 'world',
random: Math.random(),
},
}
}
export default props => (
<>
<h3 id="gsp">getStaticProps</h3>
<p id="props">{JSON.stringify(props)}</p>
</>
)
export const getServerSideProps = () => {
return {
props: {
hello: 'world',
random: Math.random(),
},
}
}
export default props => (
<>
<h3 id="gssp">getServerSideProps</h3>
<p id="props">{JSON.stringify(props)}</p>
</>
)
...@@ -8,6 +8,18 @@ export default () => ( ...@@ -8,6 +8,18 @@ export default () => (
<h1>Hello World</h1> <h1>Hello World</h1>
</a> </a>
</Link> </Link>
<br />
<Link href="/gsp">
<a id="gsp-link">
<h1>getStaticProps</h1>
</a>
</Link>
<br />
<Link href="/gssp">
<a id="gssp-link">
<h1>getServerSideProps</h1>
</a>
</Link>
<div id="base-path">{useRouter().basePath}</div> <div id="base-path">{useRouter().basePath}</div>
</> </>
) )
...@@ -50,6 +50,28 @@ const runTests = (context, dev = false) => { ...@@ -50,6 +50,28 @@ const runTests = (context, dev = false) => {
} }
}) })
it('should fetch data for getStaticProps without reloading', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
await browser.eval('window.beforeNavigate = true')
await browser.elementByCss('#gsp-link').click()
await browser.waitForElementByCss('#gsp')
expect(await browser.eval('window.beforeNavigate')).toBe(true)
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.hello).toBe('world')
})
it('should fetch data for getServerSideProps without reloading', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
await browser.eval('window.beforeNavigate = true')
await browser.elementByCss('#gsp-link').click()
await browser.waitForElementByCss('#gsp')
expect(await browser.eval('window.beforeNavigate')).toBe(true)
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.hello).toBe('world')
})
it('should have correct href for a link', async () => { it('should have correct href for a link', async () => {
const browser = await webdriver(context.appPort, '/docs/hello') const browser = await webdriver(context.appPort, '/docs/hello')
const href = await browser.elementByCss('a').getAttribute('href') const href = await browser.elementByCss('a').getAttribute('href')
......
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