Skip to content

Commit

Permalink
Add test for fetch with auth in use cache (vercel#71768)
Browse files Browse the repository at this point in the history
This ensures we are properly caching fetch inside of use cache even if
it uses `Authorzation` and comes after cookies() access.
  • Loading branch information
ijjk authored Oct 25, 2024
1 parent b3ba330 commit 38c3e83
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/e2e/app-dir/use-cache/app/cache-fetch-auth-header/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { cookies } from 'next/headers'
import React from 'react'

async function getData() {
'use cache'

return fetch('https://next-data-api-endpoint.vercel.app/api/random', {
headers: {
Authorization: `Bearer ${process.env.MY_TOKEN}`,
},
}).then((res) => res.text())
}

export default async function Page() {
const myCookies = await cookies()
const id = myCookies.get('id')?.value

return (
<>
<p>index page</p>
<p id="random">{await getData()}</p>
<p id="my-id">{id || ''}</p>
</>
)
}
9 changes: 9 additions & 0 deletions test/e2e/app-dir/use-cache/use-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,13 @@ describe('use-cache', () => {

expect(await browser.elementByCss('#random').text()).toBe(initialValue)
})

it('should override fetch with cookies/auth in use cache properly', async () => {
const browser = await next.browser('/cache-fetch-auth-header')

const initialValue = await browser.elementByCss('#random').text()
await browser.refresh()

expect(await browser.elementByCss('#random').text()).toBe(initialValue)
})
})

0 comments on commit 38c3e83

Please sign in to comment.