diff --git a/packages/next/client/page-loader.js b/packages/next/client/page-loader.js index d2c72bb34dd4e3..4551667f53be80 100644 --- a/packages/next/client/page-loader.js +++ b/packages/next/client/page-loader.js @@ -3,6 +3,7 @@ import mitt from '../next-server/lib/mitt' 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 { delBasePath } from './../next-server/lib/router/router' function hasRel(rel, link) { try { @@ -96,10 +97,12 @@ export default class PageLoader { * @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes */ getDataHref(href, asPath) { - const getHrefForSlug = (/** @type string */ path) => - `${this.assetPrefix}/_next/data/${this.buildId}${ + const getHrefForSlug = (/** @type string */ path) => { + path = delBasePath(path) + return `${this.assetPrefix}/_next/data/${this.buildId}${ path === '/' ? '/index' : path }.json` + } const { pathname: hrefPathname, query } = parse(href, true) const { pathname: asPathname } = parse(asPath) diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index ef6abfee8bb8a7..e9b09e44658e4c 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -22,7 +22,7 @@ export function addBasePath(path: string): string { return path.indexOf(basePath) !== 0 ? basePath + path : path } -function delBasePath(path: string): string { +export function delBasePath(path: string): string { return path.indexOf(basePath) === 0 ? path.substr(basePath.length) || '/' : path @@ -91,8 +91,10 @@ function fetchNextData( function getResponse(): Promise { return fetch( formatWithValidation({ - // @ts-ignore __NEXT_DATA__ - pathname: `/_next/data/${__NEXT_DATA__.buildId}${pathname}.json`, + pathname: addBasePath( + // @ts-ignore __NEXT_DATA__ + `/_next/data/${__NEXT_DATA__.buildId}${delBasePath(pathname)}.json` + ), query, }), { diff --git a/test/integration/basepath/pages/gsp.js b/test/integration/basepath/pages/gsp.js new file mode 100644 index 00000000000000..0270db93260bf7 --- /dev/null +++ b/test/integration/basepath/pages/gsp.js @@ -0,0 +1,15 @@ +export const getStaticProps = () => { + return { + props: { + hello: 'world', + random: Math.random(), + }, + } +} + +export default props => ( + <> +

getStaticProps

+

{JSON.stringify(props)}

+ +) diff --git a/test/integration/basepath/pages/gssp.js b/test/integration/basepath/pages/gssp.js new file mode 100644 index 00000000000000..be8c1c1f3fa6a3 --- /dev/null +++ b/test/integration/basepath/pages/gssp.js @@ -0,0 +1,15 @@ +export const getServerSideProps = () => { + return { + props: { + hello: 'world', + random: Math.random(), + }, + } +} + +export default props => ( + <> +

getServerSideProps

+

{JSON.stringify(props)}

+ +) diff --git a/test/integration/basepath/pages/hello.js b/test/integration/basepath/pages/hello.js index c5064613a454d0..97382993820a37 100644 --- a/test/integration/basepath/pages/hello.js +++ b/test/integration/basepath/pages/hello.js @@ -8,6 +8,18 @@ export default () => (

Hello World

+
+ + +

getStaticProps

+
+ +
+ + +

getServerSideProps

+
+
{useRouter().basePath}
) diff --git a/test/integration/basepath/test/index.test.js b/test/integration/basepath/test/index.test.js index f94d6838d96c45..76656783b6468b 100644 --- a/test/integration/basepath/test/index.test.js +++ b/test/integration/basepath/test/index.test.js @@ -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 () => { const browser = await webdriver(context.appPort, '/docs/hello') const href = await browser.elementByCss('a').getAttribute('href')