Skip to content

Commit

Permalink
Correct initial fallback route param values (vercel#16485)
Browse files Browse the repository at this point in the history
This fixes invalid initial route params in development mode and serverless production mode. It also adds tests to ensure these values are correct in development, production, and serverless mode.

x-ref: vercel#16084
Fixes: vercel#16481
Fixes: vercel#16482
  • Loading branch information
ijjk authored Aug 23, 2020
1 parent 32ee65e commit 4e0b401
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,7 @@ const nextServerlessLoader: loader.Loader = function () {
pageIsDynamicRoute
? `
const params = (
fromExport &&
!getStaticProps &&
!getServerSideProps
fromExport
) ? {}
: normalizeDynamicRouteParams(
trustQuery
Expand Down
2 changes: 1 addition & 1 deletion packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ export async function renderToHTML(
)
}

if (isAutoExport) {
if (isAutoExport || isFallback) {
// remove query values except ones that will be set during export
query = {
...(query.amp
Expand Down
34 changes: 34 additions & 0 deletions test/integration/fallback-route-params/pages/[slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useRouter } from 'next/router'

export const getStaticProps = () => {
return {
props: {
world: 'world',
},
}
}

export const getStaticPaths = () => {
return {
paths: [],
fallback: true,
}
}

export default function Page({ world }) {
const router = useRouter()

if (typeof window !== 'undefined' && !window.setInitialSlug) {
window.setInitialSlug = true
window.initialSlug = router.query.slug
}

if (router.isFallback) return 'Loading...'

return (
<>
<p>hello {world}</p>
<p id="query">{JSON.stringify(router.query)}</p>
</>
)
}
89 changes: 89 additions & 0 deletions test/integration/fallback-route-params/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-env jest */

import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import {
killApp,
findPort,
nextBuild,
nextStart,
renderViaHTTP,
launchApp,
File,
} from 'next-test-utils'

jest.setTimeout(1000 * 60 * 2)

const appDir = join(__dirname, '../')
const nextConfig = new File(join(appDir, 'next.config.js'))
let appPort
let app

const runTests = () => {
it('should have correct fallback query (skeleton)', async () => {
const html = await renderViaHTTP(appPort, '/first')
const $ = cheerio.load(html)
const { query } = JSON.parse($('#__NEXT_DATA__').text())
expect(query).toEqual({})
})

it('should have correct fallback query (hydration)', async () => {
const browser = await webdriver(appPort, '/second')
const initialSlug = await browser.eval(() => window.initialSlug)
expect(initialSlug).toBe(null)

await browser.waitForElementByCss('#query')

const hydratedQuery = JSON.parse(
await browser.elementByCss('#query').text()
)
expect(hydratedQuery).toEqual({ slug: 'second' })
})
}

describe('Fallback Dynamic Route Params', () => {
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'))
await nextBuild(appDir, [])
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})

describe('serverless mode', () => {
beforeAll(async () => {
nextConfig.write(`
module.exports = {
target: 'experimental-serverless-trace'
}
`)
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir, [], { stdout: true })
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
nextConfig.delete()
})

runTests()
})
})

0 comments on commit 4e0b401

Please sign in to comment.