Skip to content

Commit

Permalink
Extract sendPayload and prepareServerlessUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
devknoll committed Feb 28, 2020
1 parent 7848615 commit cb3e4a7
Showing 1 changed file with 72 additions and 61 deletions.
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,
},
})
}

0 comments on commit cb3e4a7

Please sign in to comment.