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

Extract sendPayload and prepareServerlessUrl #10732

Merged
merged 2 commits into from Feb 28, 2020
Merged
Changes from all commits
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
133 changes: 72 additions & 61 deletions packages/next/next-server/server/next-server.ts
Expand Up @@ -662,7 +662,7 @@ export default class Server {

if (!this.renderOpts.dev && this._isLikeServerless) {
if (typeof pageModule.default === 'function') {
this.prepareServerlessUrl(req, query)
prepareServerlessUrl(req, query)
await pageModule.default(req, res)
return true
}
Expand Down Expand Up @@ -823,50 +823,6 @@ export default class Server {
return null
}

private __sendPayload(
res: ServerResponse,
payload: any,
type: string,
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 (options?.private) {
res.setHeader(
'Cache-Control',
`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=${options.revalidate}, stale-while-revalidate`
)
} else if (options?.revalidate === false) {
res.setHeader(
'Cache-Control',
`s-maxage=31536000, stale-while-revalidate`
)
}
}
res.end(payload)
}

private prepareServerlessUrl(req: IncomingMessage, query: ParsedUrlQuery) {
const curUrl = parseUrl(req.url!, true)
req.url = formatUrl({
...curUrl,
search: undefined,
query: {
...curUrl.query,
...query,
},
})
}

private async renderToHTMLWithComponents(
req: IncomingMessage,
res: ServerResponse,
Expand Down Expand Up @@ -918,18 +874,20 @@ export default class Server {
true
)

this.__sendPayload(
sendPayload(
res,
JSON.stringify(renderResult?.renderOpts?.pageData),
'application/json',
{
revalidate: -1,
private: false, // Leave to user-land caching
}
!this.renderOpts.dev
? {
revalidate: -1,
private: false, // Leave to user-land caching
}
: undefined
)
return null
}
this.prepareServerlessUrl(req, query)
prepareServerlessUrl(req, query)
return (components.Component as any).renderReqToHTML(req, res)
}

Expand All @@ -939,10 +897,17 @@ export default class Server {
...opts,
isDataReq,
})
this.__sendPayload(res, JSON.stringify(props), 'application/json', {
revalidate: -1,
private: false, // Leave to user-land caching
})
sendPayload(
res,
JSON.stringify(props),
'application/json',
!this.renderOpts.dev
? {
revalidate: -1,
private: false, // Leave to user-land caching
}
: undefined
)
return null
}

Expand Down Expand Up @@ -972,11 +937,11 @@ export default class Server {
? JSON.stringify(cachedData.pageData)
: cachedData.html

this.__sendPayload(
sendPayload(
res,
data,
isDataReq ? 'application/json' : 'text/html; charset=utf-8',
cachedData.curRevalidate !== undefined
cachedData.curRevalidate !== undefined && !this.renderOpts.dev
? { revalidate: cachedData.curRevalidate, private: isPreviewMode }
: undefined
)
Expand Down Expand Up @@ -1108,7 +1073,7 @@ export default class Server {
else {
query.__nextFallback = 'true'
if (isLikeServerless) {
this.prepareServerlessUrl(req, query)
prepareServerlessUrl(req, query)
html = await (components.Component as any).renderReqToHTML(req, res)
} else {
html = (await renderToHTML(req, res, pathname, query, {
Expand All @@ -1118,19 +1083,21 @@ export default class Server {
}
}

this.__sendPayload(res, html, 'text/html; charset=utf-8')
sendPayload(res, html, 'text/html; charset=utf-8')
}

const {
isOrigin,
value: { html, pageData, sprRevalidate },
} = await doRender(ssgCacheKey, [])
if (!isResSent(res)) {
this.__sendPayload(
sendPayload(
res,
isDataReq ? JSON.stringify(pageData) : html,
isDataReq ? 'application/json' : 'text/html; charset=utf-8',
{ revalidate: sprRevalidate, private: isPreviewMode }
!this.renderOpts.dev
? { revalidate: sprRevalidate, private: isPreviewMode }
: undefined
)
}

Expand Down Expand Up @@ -1352,3 +1319,47 @@ export default class Server {
return isTargetLikeServerless(this.nextConfig.target)
}
}

function sendPayload(
res: ServerResponse,
payload: any,
type: string,
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 (options != null) {
if (options?.private) {
res.setHeader(
'Cache-Control',
`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=${options.revalidate}, stale-while-revalidate`
)
} else if (options?.revalidate === false) {
res.setHeader(
'Cache-Control',
`s-maxage=31536000, stale-while-revalidate`
)
}
}
res.end(payload)
}

function prepareServerlessUrl(req: IncomingMessage, query: ParsedUrlQuery) {
const curUrl = parseUrl(req.url!, true)
req.url = formatUrl({
...curUrl,
search: undefined,
query: {
...curUrl.query,
...query,
},
})
}