Unverified Commit ae0dbe5e authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix GSP redirect cache error (#26627)

This makes sure we don't attempt flushing cache info to disk for `getStaticProps` `redirect` items with `revalidate`

Fixes: https://github.com/vercel/next.js/issues/20816

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
parent e8a9bd19
...@@ -194,7 +194,11 @@ export class IncrementalCache { ...@@ -194,7 +194,11 @@ export class IncrementalCache {
// TODO: This option needs to cease to exist unless it stops mutating the // TODO: This option needs to cease to exist unless it stops mutating the
// `next build` output's manifest. // `next build` output's manifest.
if (this.incrementalOptions.flushToDisk && !data.isNotFound) { if (
this.incrementalOptions.flushToDisk &&
!data.isNotFound &&
!data.isRedirect
) {
try { try {
const seedPath = this.getSeedPath(pathname, 'html') const seedPath = this.getSeedPath(pathname, 'html')
await promises.mkdir(path.dirname(seedPath), { recursive: true }) await promises.mkdir(path.dirname(seedPath), { recursive: true })
......
import { useRouter } from 'next/router'
export default function Post(props) {
const router = useRouter()
if (typeof window === 'undefined') {
if (router.query.post?.startsWith('redir')) {
console.log(router)
throw new Error('render should not occur for redirect')
}
}
if (typeof window !== 'undefined' && !window.initialHref) {
window.initialHref = window.location.href
}
if (router.isFallback) return <p>Loading...</p>
return (
<>
<p id="gsp">getStaticProps</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
export const getStaticProps = ({ params }) => {
if (params.post.startsWith('redir')) {
let destination = '/404'
if (params.post.includes('dest-external')) {
destination = 'https://example.com'
} else if (params.post.includes('dest-')) {
destination = params.post.split('dest-').pop().replace(/_/g, '/')
}
let permanent = undefined
let statusCode = undefined
if (params.post.includes('statusCode-')) {
permanent = parseInt(
params.post.split('statusCode-').pop().split('-').shift(),
10
)
}
if (params.post.includes('permanent')) {
permanent = true
} else if (!statusCode) {
permanent = false
}
let revalidate
if (params.post.includes('revalidate-')) {
revalidate = 1
}
console.log('redirecting', {
destination,
permanent,
statusCode,
revalidate,
})
return {
redirect: {
destination,
permanent,
statusCode,
},
revalidate,
}
}
return {
props: {
params,
},
}
}
export const getStaticPaths = () => {
return {
paths: ['first', 'second'].map((post) => ({ params: { post } })),
fallback: 'blocking',
}
}
...@@ -49,6 +49,17 @@ export const getStaticProps = ({ params }) => { ...@@ -49,6 +49,17 @@ export const getStaticProps = ({ params }) => {
} else if (!statusCode) { } else if (!statusCode) {
permanent = false permanent = false
} }
let revalidate
if (params.post.includes('revalidate-')) {
revalidate = 1
}
console.log('redirecting', {
destination,
permanent,
statusCode,
revalidate,
})
return { return {
redirect: { redirect: {
...@@ -56,6 +67,7 @@ export const getStaticProps = ({ params }) => { ...@@ -56,6 +67,7 @@ export const getStaticProps = ({ params }) => {
permanent, permanent,
statusCode, statusCode,
}, },
revalidate,
} }
} }
......
...@@ -110,6 +110,90 @@ const runTests = (isDev) => { ...@@ -110,6 +110,90 @@ const runTests = (isDev) => {
expect(pathname).toBe('/gsp-blog/redirect-dest-_gsp-blog_first') expect(pathname).toBe('/gsp-blog/redirect-dest-_gsp-blog_first')
}) })
it('should apply redirect when fallback blocking GSP page is visited directly (internal dynamic)', async () => {
const browser = await webdriver(
appPort,
'/gsp-blog-blocking/redirect-dest-_gsp-blog_first',
true,
true
)
await browser.waitForElementByCss('#gsp')
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props).toEqual({
params: {
post: 'first',
},
})
const initialHref = await browser.eval(() => window.initialHref)
const { pathname } = url.parse(initialHref)
expect(pathname).toBe('/gsp-blog/first')
})
it('should apply redirect when fallback blocking GSP page is visited directly (internal dynamic) second visit', async () => {
const browser = await webdriver(
appPort,
'/gsp-blog-blocking/redirect-dest-_gsp-blog_first',
true,
true
)
await browser.waitForElementByCss('#gsp')
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props).toEqual({
params: {
post: 'first',
},
})
const initialHref = await browser.eval(() => window.initialHref)
const { pathname } = url.parse(initialHref)
expect(pathname).toBe('/gsp-blog/first')
})
it('should apply redirect when fallback blocking GSP page is visited directly (internal dynamic) with revalidate', async () => {
const browser = await webdriver(
appPort,
'/gsp-blog-blocking/redirect-revalidate-dest-_gsp-blog_first',
true,
true
)
await browser.waitForElementByCss('#gsp')
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props).toEqual({
params: {
post: 'first',
},
})
const initialHref = await browser.eval(() => window.initialHref)
const { pathname } = url.parse(initialHref)
expect(pathname).toBe('/gsp-blog/first')
})
it('should apply redirect when fallback blocking GSP page is visited directly (internal dynamic) with revalidate second visit', async () => {
const browser = await webdriver(
appPort,
'/gsp-blog-blocking/redirect-revalidate-dest-_gsp-blog_first',
true,
true
)
await browser.waitForElementByCss('#gsp')
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props).toEqual({
params: {
post: 'first',
},
})
const initialHref = await browser.eval(() => window.initialHref)
const { pathname } = url.parse(initialHref)
expect(pathname).toBe('/gsp-blog/first')
})
if (!isDev) { if (!isDev) {
it('should apply redirect when fallback GSP page is visited directly (internal dynamic) 2nd visit', async () => { it('should apply redirect when fallback GSP page is visited directly (internal dynamic) 2nd visit', async () => {
const browser = await webdriver( const browser = await webdriver(
...@@ -435,15 +519,28 @@ describe('GS(S)P Redirect Support', () => { ...@@ -435,15 +519,28 @@ describe('GS(S)P Redirect Support', () => {
}) })
describe('production mode', () => { describe('production mode', () => {
let output = ''
beforeAll(async () => { beforeAll(async () => {
await fs.remove(join(appDir, '.next')) await fs.remove(join(appDir, '.next'))
await nextBuild(appDir) await nextBuild(appDir)
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort, {
onStdout(msg) {
output += msg
},
onStderr(msg) {
output += msg
},
})
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
runTests() runTests()
it('should not have errors in output', async () => {
expect(output).not.toContain('Failed to update prerender files')
})
}) })
describe('serverless mode', () => { describe('serverless mode', () => {
......
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