mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Move sendHTML and rewrite in ts (#5839)
* Move send-html function and rewrite in typescript * Move getPageFiles and convert to ts * Move getPageFiles and convert to ts (#5841) * Move getPageFiles and convert to ts # Conflicts: # packages/next-server/server/render.js * Fix unit tests
This commit is contained in:
parent
8873242b0b
commit
0e6d190706
|
@ -5,9 +5,9 @@ import { parse as parseQs } from 'querystring'
|
|||
import fs from 'fs'
|
||||
import {
|
||||
renderToHTML,
|
||||
renderErrorToHTML,
|
||||
sendHTML
|
||||
renderErrorToHTML
|
||||
} from './render'
|
||||
import {sendHTML} from './send-html'
|
||||
import {serveStatic} from './serve-static'
|
||||
import Router, {route} from './router'
|
||||
import { isInternalUrl, isBlockedPage } from './utils'
|
||||
|
@ -189,7 +189,7 @@ export default class Server {
|
|||
if (this.nextConfig.poweredByHeader) {
|
||||
res.setHeader('X-Powered-By', 'Next.js ' + process.env.NEXT_VERSION)
|
||||
}
|
||||
return sendHTML(req, res, html, req.method, this.renderOpts)
|
||||
return sendHTML(req, res, html, this.renderOpts)
|
||||
}
|
||||
|
||||
async renderToHTML (req, res, pathname, query) {
|
||||
|
@ -210,7 +210,7 @@ export default class Server {
|
|||
async renderError (err, req, res, pathname, query) {
|
||||
res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
|
||||
const html = await this.renderErrorToHTML(err, req, res, pathname, query)
|
||||
return sendHTML(req, res, html, req.method, this.renderOpts)
|
||||
return sendHTML(req, res, html, this.renderOpts)
|
||||
}
|
||||
|
||||
async renderErrorToHTML (err, req, res, pathname, query) {
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
import { join } from 'path'
|
||||
import React from 'react'
|
||||
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
||||
import generateETag from 'etag'
|
||||
import fresh from 'fresh'
|
||||
import {requirePage} from './require'
|
||||
import Router from '../lib/router/router'
|
||||
import { loadGetInitialProps, isResSent } from '../lib/utils'
|
||||
|
@ -142,32 +140,6 @@ async function doRender (req, res, pathname, query, {
|
|||
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
|
||||
}
|
||||
|
||||
export function sendHTML (req, res, html, method, { dev, generateEtags }) {
|
||||
if (isResSent(res)) return
|
||||
const etag = generateEtags && generateETag(html)
|
||||
|
||||
if (fresh(req.headers, { etag })) {
|
||||
res.statusCode = 304
|
||||
res.end()
|
||||
return
|
||||
}
|
||||
|
||||
if (dev) {
|
||||
// In dev, we should not cache pages for any reason.
|
||||
res.setHeader('Cache-Control', 'no-store, must-revalidate')
|
||||
}
|
||||
|
||||
if (etag) {
|
||||
res.setHeader('ETag', etag)
|
||||
}
|
||||
|
||||
if (!res.getHeader('Content-Type')) {
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
}
|
||||
res.setHeader('Content-Length', Buffer.byteLength(html))
|
||||
res.end(method === 'HEAD' ? null : html)
|
||||
}
|
||||
|
||||
function errorToJSON (err) {
|
||||
const { name, message, stack } = err
|
||||
const json = { name, message, stack }
|
||||
|
|
30
packages/next-server/server/send-html.ts
Normal file
30
packages/next-server/server/send-html.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import {IncomingMessage, ServerResponse} from 'http'
|
||||
import generateETag from 'etag'
|
||||
import fresh from 'fresh'
|
||||
import { isResSent } from '../lib/utils'
|
||||
|
||||
export function sendHTML (req: IncomingMessage, res: ServerResponse, html: string, { dev, generateEtags }: {dev: boolean, generateEtags: boolean}) {
|
||||
if (isResSent(res)) return
|
||||
const etag = generateEtags ? generateETag(html) : undefined
|
||||
|
||||
if (fresh(req.headers, { etag })) {
|
||||
res.statusCode = 304
|
||||
res.end()
|
||||
return
|
||||
}
|
||||
|
||||
if (dev) {
|
||||
// In dev, we should not cache pages for any reason.
|
||||
res.setHeader('Cache-Control', 'no-store, must-revalidate')
|
||||
}
|
||||
|
||||
if (etag) {
|
||||
res.setHeader('ETag', etag)
|
||||
}
|
||||
|
||||
if (!res.getHeader('Content-Type')) {
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
}
|
||||
res.setHeader('Content-Length', Buffer.byteLength(html))
|
||||
res.end(req.method === 'HEAD' ? null : html)
|
||||
}
|
|
@ -101,6 +101,8 @@
|
|||
"@types/babel__generator": "7.0.1",
|
||||
"@types/babel__template": "7.0.1",
|
||||
"@types/babel__traverse": "7.0.3",
|
||||
"@types/etag": "1.8.0",
|
||||
"@types/fresh": "0.5.0",
|
||||
"@types/nanoid": "1.2.0",
|
||||
"@types/node-fetch": "2.1.4",
|
||||
"@types/webpack": "4.4.20",
|
||||
|
|
Loading…
Reference in a new issue