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