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

Apply some renames for SSG related code #10099

Merged
merged 2 commits into from Jan 15, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions packages/next/build/utils.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -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.
Expand Down
Expand Up @@ -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$/, '')
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions packages/next/export/index.ts
Expand Up @@ -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)

Expand Down Expand Up @@ -318,7 +318,7 @@ export default async function(
distDir,
buildId,
outDir,
sprDataDir,
pagesDataDir,
renderOpts,
serverRuntimeConfig,
subFolders,
Expand Down Expand Up @@ -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))
Expand Down
12 changes: 6 additions & 6 deletions packages/next/export/worker.js
Expand Up @@ -25,7 +25,7 @@ export default async function({
distDir,
buildId,
outDir,
sprDataDir,
pagesDataDir,
renderOpts,
buildExport,
serverRuntimeConfig,
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/next/lib/constants.ts
Expand Up @@ -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`
46 changes: 23 additions & 23 deletions packages/next/next-server/server/next-server.ts
Expand Up @@ -380,7 +380,7 @@ export default class Server {
req,
res,
pathname,
{ _nextSprData: '1' },
{ _nextDataReq: '1' },
parsedUrl
)
return {
Expand Down Expand Up @@ -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)
Expand All @@ -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
)

Expand All @@ -883,18 +883,18 @@ 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`
}

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

Expand All @@ -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 = {
Expand All @@ -914,30 +914,30 @@ 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
)
}

// Update the SPR cache if the head request
if (isOrigin) {
await setSprCache(
sprCacheKey,
{ html: html!, pageData: sprData },
ssgCacheKey,
{ html: html!, pageData },
sprRevalidate
)
}
Expand Down Expand Up @@ -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 }
Expand All @@ -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,
},
Expand Down
6 changes: 3 additions & 3 deletions packages/next/next-server/server/render.tsx
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down