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

Send Credentials for getServerSideProps Requests #10826

Merged
merged 2 commits into from Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 getServerProps() {
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`)
ijjk marked this conversation as resolved.
Show resolved Hide resolved
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`)
ijjk marked this conversation as resolved.
Show resolved Hide resolved
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