Unverified Commit 8222d571 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Correctly fetch data for root route (#15149)

This PR corrects the data fetching behavior for optional root routes.

---

Fixes #14923
parent a1319098
...@@ -328,10 +328,17 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -328,10 +328,17 @@ const nextServerlessLoader: loader.Loader = function () {
${handleBasePath} ${handleBasePath}
if (parsedUrl.pathname.match(/_next\\/data/)) { if (parsedUrl.pathname.match(/_next\\/data/)) {
_nextData = true const {
parsedUrl.pathname = parsedUrl.pathname default: getRouteFromAssetPath,
.replace(new RegExp('/_next/data/${escapedBuildId}/'), '/') } = require('next/dist/next-server/lib/router/utils/get-route-from-asset-path');
.replace(/\\.json$/, '') _nextData = true;
parsedUrl.pathname = getRouteFromAssetPath(
parsedUrl.pathname.replace(
new RegExp('/_next/data/${escapedBuildId}/'),
'/'
),
'.json'
);
} }
const renderOpts = Object.assign( const renderOpts = Object.assign(
......
...@@ -155,12 +155,13 @@ export default class PageLoader { ...@@ -155,12 +155,13 @@ export default class PageLoader {
return ( return (
(optional || param in dynamicMatches) && (optional || param in dynamicMatches) &&
// Interpolate group into data URL if present // Interpolate group into data URL if present
(interpolatedRoute = interpolatedRoute.replace( (interpolatedRoute =
replaced, interpolatedRoute.replace(
repeat replaced,
? value.map(escapePathDelimiters).join('/') repeat
: escapePathDelimiters(value) ? value.map(escapePathDelimiters).join('/')
)) : escapePathDelimiters(value)
) || '/')
) )
}) })
) { ) {
......
import { useRouter } from 'next/router'
export const getStaticPaths = () => ({
paths: [],
fallback: true,
})
export const getStaticProps = ({ params }) => ({ props: { params } })
export default function Page({ params }) {
const r = useRouter()
if (r.isFallback) {
return <div>loading</div>
}
return (
<div id="success">
{params.optionalName ? params.optionalName.join(',') : 'yay'}
</div>
)
}
/* eslint-env jest */
import fs from 'fs-extra'
import {
check,
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import { join } from 'path'
jest.setTimeout(1000 * 60 * 2)
let app
let appPort
const appDir = join(__dirname, '../')
function runTests() {
it('should render optional catch-all top-level route with no segments', async () => {
const browser = await webdriver(appPort, '/')
try {
await browser.waitForElementByCss('#success')
await check(() => browser.elementByCss('#success').text(), /yay/)
} finally {
await browser.close()
}
})
it('should render optional catch-all top-level route with one segment', async () => {
const browser = await webdriver(appPort, '/one')
try {
await browser.waitForElementByCss('#success')
await check(() => browser.elementByCss('#success').text(), /one/)
} finally {
await browser.close()
}
})
it('should render optional catch-all top-level route with two segments', async () => {
const browser = await webdriver(appPort, '/one/two')
try {
await browser.waitForElementByCss('#success')
await check(() => browser.elementByCss('#success').text(), /one,two/)
} finally {
await browser.close()
}
})
}
const nextConfig = join(appDir, 'next.config.js')
describe('Dynamic Optional Routing Root Fallback', () => {
describe('dev mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
const curConfig = await fs.readFile(nextConfig, 'utf8')
if (curConfig.includes('target')) {
await fs.writeFile(nextConfig, `module.exports = {}`)
}
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('serverless mode', () => {
let origNextConfig
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
runTests()
})
})
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