Skip to content

Commit

Permalink
Fix static to dynamic error in dev (#46597)
Browse files Browse the repository at this point in the history
This corrects the static to dynamic error incorrectly showing in
development since we don't have enough information to accurately know if
this error should be thrown or not unless a build has been done.

Fixes: #46436
x-ref: [slack
thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1677637140906959)

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
  • Loading branch information
ijjk authored Mar 1, 2023
1 parent cccf4f2 commit abf8a42
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
isNotFound = (renderOpts as any).isNotFound
isRedirect = (renderOpts as any).isRedirect

if (isAppPath && isSSG && isrRevalidate === 0) {
// we don't throw static to dynamic errors in dev as isSSG
// is a best guess in dev since we don't have the prerender pass
// to know whether the path is actually static or not
if (isAppPath && isSSG && isrRevalidate === 0 && !this.renderOpts.dev) {
const staticBailoutInfo: {
stack?: string
description?: string
Expand Down
21 changes: 21 additions & 0 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ createNextDescribe(
'force-static/page.js',
'force-static/second.html',
'force-static/second.rsc',
'gen-params-dynamic/[slug]/page.js',
'hooks/use-pathname/[slug]/page.js',
'hooks/use-pathname/slug.html',
'hooks/use-pathname/slug.rsc',
Expand Down Expand Up @@ -772,6 +773,26 @@ createNextDescribe(
}
})

it('should not error with generateStaticParams and dynamic data', async () => {
const res = await next.fetch('/gen-params-dynamic/one')
const html = await res.text()
expect(res.status).toBe(200)
expect(html).toContain('gen-params-dynamic/[slug]')
expect(html).toContain('one')

const data = cheerio.load(html)('#data').text()

for (let i = 0; i < 5; i++) {
const res2 = await next.fetch('/gen-params-dynamic/one')
expect(res2.status).toBe(200)
expect(
cheerio
.load(await res2.text())('#data')
.text()
).not.toBe(data)
}
})

it('should honor dynamic = "force-static" correctly', async () => {
const res = await next.fetch('/force-static/first')
expect(res.status).toBe(200)
Expand Down
35 changes: 35 additions & 0 deletions test/e2e/app-dir/app-static/app/gen-params-dynamic/[slug]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export async function generateStaticParams() {
return [{ slug: 'one' }]
}

const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
return await fetch(url, init)
} catch (err) {
if (i === 4) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}

export default async function page({ params }) {
const { slug } = params
const data = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
body: JSON.stringify({ hello: 'world' }),
}
).then((res) => res.text())

return (
<>
<p id="page">/gen-params-dynamic/[slug]</p>
<p id="slug">{slug}</p>
<p id="data">{data}</p>
</>
)
}

0 comments on commit abf8a42

Please sign in to comment.