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

View file

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

View file

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

View file

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

View file

@ -3,6 +3,7 @@
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import cheerio from 'cheerio'
import { import {
renderViaHTTP, renderViaHTTP,
fetchViaHTTP, fetchViaHTTP,
@ -190,6 +191,24 @@ const runTests = (dev = false) => {
expect(html).toMatch(/Post:.*?post-1/) 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 () => { it('should return data correctly', async () => {
const data = JSON.parse( const data = JSON.parse(
await renderViaHTTP( await renderViaHTTP(