Skip to content

Commit

Permalink
Send Credentials for getServerSideProps Requests (#10826)
Browse files Browse the repository at this point in the history
* Send Credentials for `getServerSideProps` Requests

* Fix tests
  • Loading branch information
Timer committed Mar 4, 2020
1 parent 70cb5bd commit 50c95d1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
16 changes: 15 additions & 1 deletion packages/next/next-server/lib/router/router.ts
Expand Up @@ -83,7 +83,21 @@ function fetchNextData(
// @ts-ignore __NEXT_DATA__
pathname: `/_next/data/${__NEXT_DATA__.buildId}${pathname}.json`,
query,
})
}),
{
// Cookies are required to be present for Next.js' SSG "Preview Mode".
// Cookies may also be required for `getServerSideProps`.
//
// > `fetch` won’t send cookies, unless you set the credentials init
// > option.
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
//
// > For maximum browser compatibility when it comes to sending &
// > receiving cookies, always supply the `credentials: 'same-origin'`
// > option instead of relying on the default.
// https://github.com/github/fetch#caveats
credentials: 'same-origin',
}
).then(res => {
if (!res.ok) {
if (--attempts > 0 && res.status >= 500) {
Expand Down
15 changes: 15 additions & 0 deletions test/integration/getserversideprops-preview/pages/to-index.js
@@ -0,0 +1,15 @@
import Link from 'next/link'

export function getServerSideProps() {
return { props: {} }
}

export default function() {
return (
<main>
<Link href="/">
<a id="to-index">To Index</a>
</Link>
</main>
)
}
14 changes: 13 additions & 1 deletion test/integration/getserversideprops-preview/test/index.test.js
Expand Up @@ -165,7 +165,7 @@ function runTests(startServer = nextStart) {
)
})

it('should fetch preview data', async () => {
it('should fetch preview data on SSR', async () => {
await browser.get(`http://localhost:${appPort}/`)
await browser.waitForElementByCss('#props-pre')
// expect(await browser.elementById('props-pre').text()).toBe('Has No Props')
Expand All @@ -175,6 +175,18 @@ function runTests(startServer = nextStart) {
)
})

it('should fetch preview data on CST', async () => {
await browser.get(`http://localhost:${appPort}/to-index`)
await browser.waitForElementByCss('#to-index')
await browser.eval('window.itdidnotrefresh = "hello"')
await browser.elementById('to-index').click()
await browser.waitForElementByCss('#props-pre')
expect(await browser.eval('window.itdidnotrefresh')).toBe('hello')
expect(await browser.elementById('props-pre').text()).toBe(
'true and {"client":"mode"}'
)
})

it('should fetch prerendered data', async () => {
await browser.get(`http://localhost:${appPort}/api/reset`)

Expand Down
15 changes: 15 additions & 0 deletions test/integration/prerender-preview/pages/to-index.js
@@ -0,0 +1,15 @@
import Link from 'next/link'

export function getStaticProps() {
return { props: {} }
}

export default function() {
return (
<main>
<Link href="/">
<a id="to-index">To Index</a>
</Link>
</main>
)
}
14 changes: 13 additions & 1 deletion test/integration/prerender-preview/test/index.test.js
Expand Up @@ -165,7 +165,7 @@ function runTests(startServer = nextStart) {
)
})

it('should fetch preview data', async () => {
it('should fetch preview data on SSR', async () => {
await browser.get(`http://localhost:${appPort}/`)
await browser.waitForElementByCss('#props-pre')
// expect(await browser.elementById('props-pre').text()).toBe('Has No Props')
Expand All @@ -175,6 +175,18 @@ function runTests(startServer = nextStart) {
)
})

it('should fetch preview data on CST', async () => {
await browser.get(`http://localhost:${appPort}/to-index`)
await browser.waitForElementByCss('#to-index')
await browser.eval('window.itdidnotrefresh = "hello"')
await browser.elementById('to-index').click()
await browser.waitForElementByCss('#props-pre')
expect(await browser.eval('window.itdidnotrefresh')).toBe('hello')
expect(await browser.elementById('props-pre').text()).toBe(
'true and {"client":"mode"}'
)
})

it('should fetch prerendered data', async () => {
await browser.get(`http://localhost:${appPort}/api/reset`)

Expand Down

0 comments on commit 50c95d1

Please sign in to comment.