提交 fff897d7 编写于 作者: J JJ Kasper 提交者: Joe Haddad

Apply some renames for SSG related code (#10099)

* Rename some spr related stuff to ssg

* Update packages/next/lib/constants.ts
Co-authored-by: NJoe Haddad <timer150@gmail.com>
上级 a2ea7a49
......@@ -9,7 +9,7 @@ import {
Rewrite,
getRedirectStatus,
} from '../lib/check-custom-routes'
import { SPR_GET_INITIAL_PROPS_CONFLICT } from '../lib/constants'
import { SSG_GET_INITIAL_PROPS_CONFLICT } from '../lib/constants'
import prettyBytes from '../lib/pretty-bytes'
import { recursiveReadDir } from '../lib/recursive-readdir'
import { getRouteMatcher, getRouteRegex } from '../next-server/lib/router/utils'
......@@ -507,7 +507,7 @@ export async function isPageStatic(
// A page cannot be prerendered _and_ define a data requirement. That's
// contradictory!
if (hasGetInitialProps && hasStaticProps) {
throw new Error(SPR_GET_INITIAL_PROPS_CONFLICT)
throw new Error(SSG_GET_INITIAL_PROPS_CONFLICT)
}
// A page cannot have static parameters if it is not a dynamic page.
......
......@@ -213,10 +213,10 @@ const nextServerlessLoader: loader.Loader = function() {
assetPrefix: "${assetPrefix}",
..._renderOpts
}
let sprData = false
let _nextData = false
if (req.url.match(/_next\\/data/)) {
sprData = true
_nextData = true
req.url = req.url
.replace(new RegExp('/_next/data/${escapedBuildId}/'), '/')
.replace(/\\.json$/, '')
......@@ -273,8 +273,8 @@ const nextServerlessLoader: loader.Loader = function() {
}
let result = await renderToHTML(req, res, "${page}", Object.assign({}, unstable_getStaticProps ? {} : parsedUrl.query, nowParams ? nowParams : params, _params), renderOpts)
if (sprData && !fromExport) {
const payload = JSON.stringify(renderOpts.sprData)
if (_nextData && !fromExport) {
const payload = JSON.stringify(renderOpts.pageData)
res.setHeader('Content-Type', 'application/json')
res.setHeader('Content-Length', Buffer.byteLength(payload))
res.setHeader(
......
......@@ -276,7 +276,7 @@ export default async function(
}
const progress = !options.silent && createProgress(filteredPaths.length)
const sprDataDir = options.buildExport
const pagesDataDir = options.buildExport
? outDir
: join(outDir, '_next/data', buildId)
......@@ -318,7 +318,7 @@ export default async function(
distDir,
buildId,
outDir,
sprDataDir,
pagesDataDir,
renderOpts,
serverRuntimeConfig,
subFolders,
......@@ -360,7 +360,7 @@ export default async function(
subFolders && route !== '/index' ? `${sep}index` : ''
}.html`
)
const jsonDest = join(sprDataDir, `${route}.json`)
const jsonDest = join(pagesDataDir, `${route}.json`)
await mkdirp(dirname(htmlDest))
await mkdirp(dirname(jsonDest))
......
......@@ -25,7 +25,7 @@ export default async function({
distDir,
buildId,
outDir,
sprDataDir,
pagesDataDir,
renderOpts,
buildExport,
serverRuntimeConfig,
......@@ -131,7 +131,7 @@ export default async function({
if (typeof mod === 'string') {
html = mod
} else {
// for non-dynamic SPR pages we should have already
// for non-dynamic SSG pages we should have already
// prerendered the file
if (renderedDuringBuild(mod.unstable_getStaticProps)) return results
......@@ -158,7 +158,7 @@ export default async function({
serverless
)
// for non-dynamic SPR pages we should have already
// for non-dynamic SSG pages we should have already
// prerendered the file
if (renderedDuringBuild(components.unstable_getStaticProps)) {
return results
......@@ -234,14 +234,14 @@ export default async function({
}
}
if (curRenderOpts.sprData) {
if (curRenderOpts.pageData) {
const dataFile = join(
sprDataDir,
pagesDataDir,
htmlFilename.replace(/\.html$/, '.json')
)
await mkdirp(dirname(dataFile))
await writeFileP(dataFile, JSON.stringify(curRenderOpts.sprData), 'utf8')
await writeFileP(dataFile, JSON.stringify(curRenderOpts.pageData), 'utf8')
}
results.fromBuildExportRevalidate = curRenderOpts.revalidate
......
......@@ -24,4 +24,4 @@ export const DOT_NEXT_ALIAS = 'private-dot-next'
export const PUBLIC_DIR_MIDDLEWARE_CONFLICT = `You can not have a '_next' folder inside of your public folder. This conflicts with the internal '/_next' route. https://err.sh/zeit/next.js/public-next-folder-conflict`
export const SPR_GET_INITIAL_PROPS_CONFLICT = `You can not use getInitialProps with unstable_getStaticProps. To use SPR, please remove your getInitialProps`
export const SSG_GET_INITIAL_PROPS_CONFLICT = `You can not use getInitialProps with unstable_getStaticProps. To use SSG, please remove your getInitialProps`
......@@ -380,7 +380,7 @@ export default class Server {
req,
res,
pathname,
{ _nextSprData: '1' },
{ _nextDataReq: '1' },
parsedUrl
)
return {
......@@ -828,10 +828,10 @@ export default class Server {
const isLikeServerless =
typeof result.Component === 'object' &&
typeof result.Component.renderReqToHTML === 'function'
const isSpr = !!result.unstable_getStaticProps
const isSSG = !!result.unstable_getStaticProps
// non-spr requests should render like normal
if (!isSpr) {
if (!isSSG) {
// handle serverless
if (isLikeServerless) {
const curUrl = parseUrl(req.url!, true)
......@@ -853,23 +853,23 @@ export default class Server {
}
// Toggle whether or not this is an SPR Data request
const isSprData = isSpr && query._nextSprData
delete query._nextSprData
const isDataReq = query._nextDataReq
delete query._nextDataReq
// Compute the SPR cache key
const sprCacheKey = parseUrl(req.url || '').pathname!
const ssgCacheKey = parseUrl(req.url || '').pathname!
// Complete the response with cached data if its present
const cachedData = await getSprCache(sprCacheKey)
const cachedData = await getSprCache(ssgCacheKey)
if (cachedData) {
const data = isSprData
const data = isDataReq
? JSON.stringify(cachedData.pageData)
: cachedData.html
this.__sendPayload(
res,
data,
isSprData ? 'application/json' : 'text/html; charset=utf-8',
isDataReq ? 'application/json' : 'text/html; charset=utf-8',
cachedData.curRevalidate
)
......@@ -883,7 +883,7 @@ export default class Server {
// Serverless requests need its URL transformed back into the original
// request path (to emulate lambda behavior in production)
if (isLikeServerless && isSprData) {
if (isLikeServerless && isDataReq) {
let { pathname } = parseUrl(req.url || '', true)
pathname = !pathname || pathname === '/' ? '/index' : pathname
req.url = `/_next/data/${this.buildId}${pathname}.json`
......@@ -891,10 +891,10 @@ export default class Server {
const doRender = withCoalescedInvoke(async function(): Promise<{
html: string | null
sprData: any
pageData: any
sprRevalidate: number | false
}> {
let sprData: any
let pageData: any
let html: string | null
let sprRevalidate: number | false
......@@ -904,7 +904,7 @@ export default class Server {
renderResult = await result.Component.renderReqToHTML(req, res, true)
html = renderResult.html
sprData = renderResult.renderOpts.sprData
pageData = renderResult.renderOpts.pageData
sprRevalidate = renderResult.renderOpts.revalidate
} else {
const renderOpts = {
......@@ -914,21 +914,21 @@ export default class Server {
renderResult = await renderToHTML(req, res, pathname, query, renderOpts)
html = renderResult
sprData = renderOpts.sprData
pageData = renderOpts.pageData
sprRevalidate = renderOpts.revalidate
}
return { html, sprData, sprRevalidate }
return { html, pageData, sprRevalidate }
})
return doRender(sprCacheKey, []).then(
async ({ isOrigin, value: { html, sprData, sprRevalidate } }) => {
return doRender(ssgCacheKey, []).then(
async ({ isOrigin, value: { html, pageData, sprRevalidate } }) => {
// Respond to the request if a payload wasn't sent above (from cache)
if (!isResSent(res)) {
this.__sendPayload(
res,
isSprData ? JSON.stringify(sprData) : html,
isSprData ? 'application/json' : 'text/html; charset=utf-8',
isDataReq ? JSON.stringify(pageData) : html,
isDataReq ? 'application/json' : 'text/html; charset=utf-8',
sprRevalidate
)
}
......@@ -936,8 +936,8 @@ export default class Server {
// Update the SPR cache if the head request
if (isOrigin) {
await setSprCache(
sprCacheKey,
{ html: html!, pageData: sprData },
ssgCacheKey,
{ html: html!, pageData },
sprRevalidate
)
}
......@@ -968,7 +968,7 @@ export default class Server {
res,
pathname,
result.unstable_getStaticProps
? { _nextSprData: query._nextSprData }
? { _nextDataReq: query._nextDataReq }
: query,
result,
{ ...this.renderOpts, amphtml, hasAmp }
......@@ -994,7 +994,7 @@ export default class Server {
// only add params for SPR enabled pages
{
...(result.unstable_getStaticProps
? { _nextSprData: query._nextSprData }
? { _nextDataReq: query._nextDataReq }
: query),
...params,
},
......
......@@ -28,7 +28,7 @@ import { isInAmpMode } from '../lib/amp'
// Uses a module path because of the compiled output directory location
import { PageConfig } from 'next/types'
import { isDynamicRoute } from '../lib/router/utils/is-dynamic'
import { SPR_GET_INITIAL_PROPS_CONFLICT } from '../../lib/constants'
import { SSG_GET_INITIAL_PROPS_CONFLICT } from '../../lib/constants'
import { AMP_RENDER_TARGET } from '../lib/constants'
export type ManifestItem = {
......@@ -324,7 +324,7 @@ export async function renderToHTML(
}
if (hasPageGetInitialProps && isSpr) {
throw new Error(SPR_GET_INITIAL_PROPS_CONFLICT + ` ${pathname}`)
throw new Error(SSG_GET_INITIAL_PROPS_CONFLICT + ` ${pathname}`)
}
if (!!unstable_getStaticPaths && !isSpr) {
......@@ -470,7 +470,7 @@ export async function renderToHTML(
props.pageProps = data.props
// pass up revalidate and props for export
;(renderOpts as any).revalidate = data.revalidate
;(renderOpts as any).sprData = props
;(renderOpts as any).pageData = props
}
} catch (err) {
if (!dev || !err) throw err
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册