Skip to content

Commit

Permalink
Fix prefetching for static app paths (#41398)
Browse files Browse the repository at this point in the history
Ensures we delete the prefetch header when the path is static as it
can't be honored and the full tree must be sent instead.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

x-ref: [slack
thread](https://vercel.slack.com/archives/C035J346QQL/p1665610012952039?thread_ts=1665582783.184029&cid=C035J346QQL)
  • Loading branch information
ijjk committed Oct 13, 2022
1 parent 5cd31e4 commit a1d830e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/next/server/base-server.ts
Expand Up @@ -1064,6 +1064,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
) {
delete req.headers['__rsc__']
delete req.headers['__next_router_state_tree__']
delete req.headers['__next_router_prefetch__']
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/app-dir/app-prefetch/app/dashboard/[id]/page.js
Expand Up @@ -8,6 +8,10 @@ async function getData() {
}
}

export function generateStaticParams() {
return [{ id: 'static' }]
}

export default function IdPage({ params }) {
const data = use(getData())
console.log(data)
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/app-dir/app-prefetch/app/dashboard/page.js
@@ -1,5 +1,9 @@
import { experimental_use as use } from 'react'

export const config = {
revalidate: 0,
}

async function getData() {
await new Promise((resolve) => setTimeout(resolve, 3000))
return {
Expand Down
4 changes: 0 additions & 4 deletions test/e2e/app-dir/app-prefetch/app/layout.js
@@ -1,7 +1,3 @@
export const config = {
revalidate: 0,
}

export default function Root({ children }) {
return (
<html>
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/app-dir/prefetching.test.ts
Expand Up @@ -55,4 +55,13 @@ describe('app dir prefetching', () => {
'Welcome to the dashboard'
)
})

it('should not have prefetch error for static path', async () => {
const browser = await webdriver(next.url, '/')
await browser.eval('window.nd.router.prefetch("/dashboard/123")')
await waitFor(3000)
await browser.eval('window.nd.router.push("/dashboard/123")')
expect(next.cliOutput).not.toContain('ReferenceError')
expect(next.cliOutput).not.toContain('is not defined')
})
})

0 comments on commit a1d830e

Please sign in to comment.