Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GSP redirect cache error #26627

Merged
merged 2 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/next/next-server/server/incremental-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ export class IncrementalCache {

// TODO: This option needs to cease to exist unless it stops mutating the
// `next build` output's manifest.
if (this.incrementalOptions.flushToDisk && !data.isNotFound) {
if (
this.incrementalOptions.flushToDisk &&
!data.isNotFound &&
!data.isRedirect
) {
try {
const seedPath = this.getSeedPath(pathname, 'html')
await promises.mkdir(path.dirname(seedPath), { recursive: true })
Expand Down
86 changes: 86 additions & 0 deletions test/integration/gssp-redirect/pages/gsp-blog-blocking/[post].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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',
}
}
12 changes: 12 additions & 0 deletions test/integration/gssp-redirect/pages/gsp-blog/[post].js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,25 @@ export const getStaticProps = ({ params }) => {
} 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,
}
}

Expand Down
99 changes: 98 additions & 1 deletion test/integration/gssp-redirect/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,90 @@ const runTests = (isDev) => {
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) {
it('should apply redirect when fallback GSP page is visited directly (internal dynamic) 2nd visit', async () => {
const browser = await webdriver(
Expand Down Expand Up @@ -435,15 +519,28 @@ describe('GS(S)P Redirect Support', () => {
})

describe('production mode', () => {
let output = ''

beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
app = await nextStart(appDir, appPort, {
onStdout(msg) {
output += msg
},
onStderr(msg) {
output += msg
},
})
})
afterAll(() => killApp(app))

runTests()

it('should not have errors in output', async () => {
expect(output).not.toContain('Failed to update prerender files')
})
})

describe('serverless mode', () => {
Expand Down