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

Preview Mode Should Not Cache #10636

Merged
merged 6 commits into from Feb 22, 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
Expand Up @@ -320,6 +320,9 @@ const nextServerlessLoader: loader.Loader = function() {

const isFallback = parsedUrl.query.__nextFallback

const previewData = tryGetPreviewData(req, res, options.previewProps)
Timer marked this conversation as resolved.
Show resolved Hide resolved
const isPreviewMode = previewData !== false

let result = await renderToHTML(req, res, "${page}", Object.assign({}, unstable_getStaticProps ? {} : parsedUrl.query, nowParams ? nowParams : params, _params, isFallback ? { __nextFallback: 'true' } : {}), renderOpts)

if (_nextData && !fromExport) {
Expand All @@ -329,7 +332,9 @@ const nextServerlessLoader: loader.Loader = function() {

res.setHeader(
'Cache-Control',
unstable_getServerProps
isPreviewMode
? \`private, no-cache, no-store, max-age=0, must-revalidate\`
: unstable_getServerProps
? \`no-cache, no-store, must-revalidate\`
: \`s-maxage=\${renderOpts.revalidate}, stale-while-revalidate\`
)
Expand Down
31 changes: 22 additions & 9 deletions packages/next/next-server/server/next-server.ts
Expand Up @@ -821,20 +821,25 @@ export default class Server {
res: ServerResponse,
payload: any,
type: string,
revalidate?: number | false
options?: { revalidate: number | false; private: boolean }
) {
// TODO: ETag? Cache-Control headers? Next-specific headers?
res.setHeader('Content-Type', type)
res.setHeader('Content-Length', Buffer.byteLength(payload))
if (!this.renderOpts.dev) {
if (revalidate) {
if (options?.private) {
res.setHeader(
'Cache-Control',
revalidate < 0
`private, no-cache, no-store, max-age=0, must-revalidate`
)
} else if (options?.revalidate) {
res.setHeader(
'Cache-Control',
options.revalidate < 0
? `no-cache, no-store, must-revalidate`
: `s-maxage=${revalidate}, stale-while-revalidate`
: `s-maxage=${options.revalidate}, stale-while-revalidate`
)
} else if (revalidate === false) {
} else if (options?.revalidate === false) {
res.setHeader(
'Cache-Control',
`s-maxage=31536000, stale-while-revalidate`
Expand Down Expand Up @@ -910,7 +915,10 @@ export default class Server {
res,
JSON.stringify(renderResult?.renderOpts?.pageData),
'application/json',
-1
{
revalidate: -1,
private: false, // Leave to user-land caching
}
)
return null
}
Expand All @@ -924,7 +932,10 @@ export default class Server {
...opts,
isDataReq,
})
this.__sendPayload(res, JSON.stringify(props), 'application/json', -1)
this.__sendPayload(res, JSON.stringify(props), 'application/json', {
revalidate: -1,
private: false, // Leave to user-land caching
})
return null
}

Expand Down Expand Up @@ -957,7 +968,9 @@ export default class Server {
res,
data,
isDataReq ? 'application/json' : 'text/html; charset=utf-8',
cachedData.curRevalidate
cachedData.curRevalidate !== undefined
? { revalidate: cachedData.curRevalidate, private: isPreviewMode }
: undefined
)

// Stop the request chain here if the data we sent was up-to-date
Expand Down Expand Up @@ -1064,7 +1077,7 @@ export default class Server {
res,
isDataReq ? JSON.stringify(pageData) : html,
isDataReq ? 'application/json' : 'text/html; charset=utf-8',
sprRevalidate
{ revalidate: sprRevalidate, private: isPreviewMode }
)
}

Expand Down