Unverified Commit 9fef0d55 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Normalize ssgCacheKey for /index with minimalMode de-duping (#35536)

* Normalize ssgCacheKey for /index

* apply test changes to i18n suite as well
parent 72478c5c
...@@ -1323,6 +1323,10 @@ export default abstract class Server { ...@@ -1323,6 +1323,10 @@ export default abstract class Server {
return seg return seg
}) })
.join('/') .join('/')
// ensure /index and / is normalized to one key
ssgCacheKey =
ssgCacheKey === '/index' && pathname === '/' ? '/' : ssgCacheKey
} }
const doRender: () => Promise<ResponseCacheEntry | null> = async () => { const doRender: () => Promise<ResponseCacheEntry | null> = async () => {
......
...@@ -162,6 +162,8 @@ describe('should set-up next', () => { ...@@ -162,6 +162,8 @@ describe('should set-up next', () => {
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, { const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ', redirect: 'manual ',
}) })
await next.patchFile('standalone/data.txt', 'show')
expect(res2.status).toBe(404) expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe( expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate' 's-maxage=1, stale-while-revalidate'
...@@ -169,18 +171,18 @@ describe('should set-up next', () => { ...@@ -169,18 +171,18 @@ describe('should set-up next', () => {
}) })
it('should render SSR page correctly', async () => { it('should render SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/gssp')
const $ = cheerio.load(html) const $ = cheerio.load(html)
const data = JSON.parse($('#props').text()) const data = JSON.parse($('#props').text())
expect($('#index').text()).toBe('index page') expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world') expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/') const html2 = await renderViaHTTP(appPort, '/gssp')
const $2 = cheerio.load(html2) const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text()) const data2 = JSON.parse($2('#props').text())
expect($2('#index').text()).toBe('index page') expect($2('#gssp').text()).toBe('getServerSideProps page')
expect(isNaN(data2.random)).toBe(false) expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random) expect(data2.random).not.toBe(data.random)
}) })
...@@ -244,24 +246,24 @@ describe('should set-up next', () => { ...@@ -244,24 +246,24 @@ describe('should set-up next', () => {
it('should render SSR page correctly with x-matched-path', async () => { it('should render SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(appPort, '/some-other-path', undefined, { const html = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: { headers: {
'x-matched-path': '/', 'x-matched-path': '/gssp',
}, },
}) })
const $ = cheerio.load(html) const $ = cheerio.load(html)
const data = JSON.parse($('#props').text()) const data = JSON.parse($('#props').text())
expect($('#index').text()).toBe('index page') expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world') expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/some-other-path', undefined, { const html2 = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: { headers: {
'x-matched-path': '/', 'x-matched-path': '/gssp',
}, },
}) })
const $2 = cheerio.load(html2) const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text()) const data2 = JSON.parse($2('#props').text())
expect($2('#index').text()).toBe('index page') expect($2('#gssp').text()).toBe('getServerSideProps page')
expect(isNaN(data2.random)).toBe(false) expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random) expect(data2.random).not.toBe(data.random)
}) })
...@@ -508,7 +510,7 @@ describe('should set-up next', () => { ...@@ -508,7 +510,7 @@ describe('should set-up next', () => {
}, },
{ {
headers: { headers: {
'x-matched-path': '/', 'x-matched-path': '/gssp',
}, },
} }
) )
......
...@@ -176,6 +176,29 @@ describe('should set-up next', () => { ...@@ -176,6 +176,29 @@ describe('should set-up next', () => {
expect(res2.status).toBe(200) expect(res2.status).toBe(200)
const { pageProps: props2 } = await res2.json() const { pageProps: props2 } = await res2.json()
expect(props2.gspCalls).toBe(props.gspCalls) expect(props2.gspCalls).toBe(props.gspCalls)
const res3 = await fetchViaHTTP(appPort, '/index', undefined, {
redirect: 'manual',
headers: {
'x-matched-path': '/index',
},
})
expect(res3.status).toBe(200)
const $2 = cheerio.load(await res3.text())
const props3 = JSON.parse($2('#props').text())
expect(props3.gspCalls).toBeDefined()
const res4 = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/index.json`,
undefined,
{
redirect: 'manual',
}
)
expect(res4.status).toBe(200)
const { pageProps: props4 } = await res4.json()
expect(props4.gspCalls).toBe(props3.gspCalls)
}) })
it('should set correct SWR headers with notFound gsp', async () => { it('should set correct SWR headers with notFound gsp', async () => {
...@@ -218,6 +241,8 @@ describe('should set-up next', () => { ...@@ -218,6 +241,8 @@ describe('should set-up next', () => {
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, { const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ', redirect: 'manual ',
}) })
await next.patchFile('standalone/data.txt', 'show')
expect(res2.status).toBe(404) expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe( expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate' 's-maxage=1, stale-while-revalidate'
...@@ -225,18 +250,18 @@ describe('should set-up next', () => { ...@@ -225,18 +250,18 @@ describe('should set-up next', () => {
}) })
it('should render SSR page correctly', async () => { it('should render SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/gssp')
const $ = cheerio.load(html) const $ = cheerio.load(html)
const data = JSON.parse($('#props').text()) const data = JSON.parse($('#props').text())
expect($('#index').text()).toBe('index page') expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world') expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/') const html2 = await renderViaHTTP(appPort, '/gssp')
const $2 = cheerio.load(html2) const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text()) const data2 = JSON.parse($2('#props').text())
expect($2('#index').text()).toBe('index page') expect($2('#gssp').text()).toBe('getServerSideProps page')
expect(isNaN(data2.random)).toBe(false) expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random) expect(data2.random).not.toBe(data.random)
}) })
...@@ -300,24 +325,24 @@ describe('should set-up next', () => { ...@@ -300,24 +325,24 @@ describe('should set-up next', () => {
it('should render SSR page correctly with x-matched-path', async () => { it('should render SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(appPort, '/some-other-path', undefined, { const html = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: { headers: {
'x-matched-path': '/', 'x-matched-path': '/gssp',
}, },
}) })
const $ = cheerio.load(html) const $ = cheerio.load(html)
const data = JSON.parse($('#props').text()) const data = JSON.parse($('#props').text())
expect($('#index').text()).toBe('index page') expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world') expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/some-other-path', undefined, { const html2 = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: { headers: {
'x-matched-path': '/', 'x-matched-path': '/gssp',
}, },
}) })
const $2 = cheerio.load(html2) const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text()) const data2 = JSON.parse($2('#props').text())
expect($2('#index').text()).toBe('index page') expect($2('#gssp').text()).toBe('getServerSideProps page')
expect(isNaN(data2.random)).toBe(false) expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random) expect(data2.random).not.toBe(data.random)
}) })
...@@ -564,7 +589,7 @@ describe('should set-up next', () => { ...@@ -564,7 +589,7 @@ describe('should set-up next', () => {
}, },
{ {
headers: { headers: {
'x-matched-path': '/', 'x-matched-path': '/gssp',
}, },
} }
) )
......
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import { useRouter } from 'next/router'
export async function getServerSideProps({ res }) { export async function getServerSideProps({ res }) {
res.setHeader('cache-control', 's-maxage=1, stale-while-revalidate') res.setHeader('cache-control', 's-maxage=1, stale-while-revalidate')
...@@ -19,6 +20,7 @@ export async function getServerSideProps({ res }) { ...@@ -19,6 +20,7 @@ export async function getServerSideProps({ res }) {
props: { props: {
hello: 'world', hello: 'world',
data, data,
random: Math.random(),
// make sure fetch if polyfilled // make sure fetch if polyfilled
example: await fetch('https://example.com').then((res) => res.text()), example: await fetch('https://example.com').then((res) => res.text()),
}, },
...@@ -26,9 +28,12 @@ export async function getServerSideProps({ res }) { ...@@ -26,9 +28,12 @@ export async function getServerSideProps({ res }) {
} }
export default function Page(props) { export default function Page(props) {
const router = useRouter()
return ( return (
<> <>
<p id="gsp">getStaticProps page</p> <p id="gssp">getServerSideProps page</p>
<p id="router">{JSON.stringify(router)}</p>
<p id="props">{JSON.stringify(props)}</p> <p id="props">{JSON.stringify(props)}</p>
</> </>
) )
......
...@@ -8,12 +8,18 @@ if (localConfig.hello !== 'world') { ...@@ -8,12 +8,18 @@ if (localConfig.hello !== 'world') {
throw new Error('oof import order is wrong, _app comes first') throw new Error('oof import order is wrong, _app comes first')
} }
export const getServerSideProps = ({ req }) => { let gspCalls = 0
export const getStaticProps = () => {
gspCalls += 1
return { return {
props: { props: {
hello: 'world', hello: 'world',
random: Math.random(), random: Math.random(),
gspCalls,
}, },
revalidate: 1,
} }
} }
......
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