Fix query values being passed in dev mode for SSG (#9734)

* Fix query values being passed in dev mode for SSG

* Update test names
This commit is contained in:
JJ Kasper 2019-12-13 12:14:09 -06:00 committed by Joe Haddad
parent 55e966bb81
commit 677a9509f3
5 changed files with 34 additions and 13 deletions

View file

@ -774,9 +774,8 @@ export default class Server {
// Toggle whether or not this is an SPR Data request
const isSprData = isSpr && query._nextSprData
if (isSprData) {
delete query._nextSprData
}
// Compute the SPR cache key
const sprCacheKey = parseUrl(req.url || '').pathname!
@ -890,7 +889,9 @@ export default class Server {
req,
res,
pathname,
query,
result.unstable_getStaticProps
? { _nextSprData: query._nextSprData }
: query,
result,
{ ...this.renderOpts, amphtml, hasAmp, dataOnly }
)

View file

@ -414,13 +414,8 @@ export default class DevServer extends Server {
continue
}
// eslint-disable-next-line no-loop-func
return this.hotReloader!.ensurePage(dynamicRoute.page).then(() => {
pathname = dynamicRoute.page
query = Object.assign({}, query, params)
})
return this.hotReloader!.ensurePage(dynamicRoute.page)
}
throw err
})
} catch (err) {

View file

@ -27,6 +27,7 @@ export async function unstable_getStaticProps({ params }) {
return {
props: {
params,
post: params.post,
time: (await import('perf_hooks')).performance.now(),
},
@ -34,11 +35,12 @@ export async function unstable_getStaticProps({ params }) {
}
}
export default ({ post, time }) => {
export default ({ post, time, params }) => {
return (
<>
<p>Post: {post}</p>
<span>time: {time}</span>
<div id="params">{JSON.stringify(params)}</div>
<div id="query">{JSON.stringify(useRouter().query)}</div>
<Link href="/">
<a id="home">to home</a>

View file

@ -1,22 +1,26 @@
import React from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'
// eslint-disable-next-line camelcase
export async function unstable_getStaticProps() {
export async function unstable_getStaticProps({ params }) {
return {
props: {
world: 'world',
params: params || {},
time: new Date().getTime(),
},
revalidate: false,
}
}
export default ({ world, time }) => {
export default ({ world, time, params }) => {
return (
<>
<p>hello: {world}</p>
<span>time: {time}</span>
<div id="params">{JSON.stringify(params)}</div>
<div id="query">{JSON.stringify(useRouter().query)}</div>
<Link href="/">
<a id="home">to home</a>
</Link>

View file

@ -3,6 +3,7 @@
import fs from 'fs-extra'
import { join } from 'path'
import webdriver from 'next-webdriver'
import cheerio from 'cheerio'
import {
renderViaHTTP,
fetchViaHTTP,
@ -190,6 +191,24 @@ const runTests = (dev = false) => {
expect(html).toMatch(/Post:.*?post-1/)
})
it('should not supply query values to params or useRouter non-dynamic page SSR', async () => {
const html = await renderViaHTTP(appPort, '/something?hello=world')
const $ = cheerio.load(html)
const query = $('#query').text()
expect(JSON.parse(query)).toEqual({})
const params = $('#params').text()
expect(JSON.parse(params)).toEqual({})
})
it('should not supply query values to params or useRouter dynamic page SSR', async () => {
const html = await renderViaHTTP(appPort, '/blog/post-1?hello=world')
const $ = cheerio.load(html)
const params = $('#params').text()
expect(JSON.parse(params)).toEqual({ post: 'post-1' })
const query = $('#query').text()
expect(JSON.parse(query)).toEqual({ post: 'post-1' })
})
it('should return data correctly', async () => {
const data = JSON.parse(
await renderViaHTTP(