Skip to content

Commit

Permalink
Apply suggestions from review
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jan 14, 2020
1 parent 8a2d304 commit 1ebb040
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/next/build/index.ts
Expand Up @@ -500,11 +500,11 @@ export default async function build(dir: string, conf = null): Promise<void> {
additionalSprPaths.set(page, result.prerenderRoutes)
ssgPageRoutes = result.prerenderRoutes
}
} else if (result.hasServerProps) {
serverPropsPages.add(page)
} else if (result.static && customAppGetInitialProps === false) {
staticPages.add(page)
isStatic = true
} else if (result.serverProps) {
serverPropsPages.add(page)
}
} catch (err) {
if (err.message !== 'INVALID_DEFAULT_EXPORT') throw err
Expand Down
12 changes: 6 additions & 6 deletions packages/next/build/utils.ts
Expand Up @@ -10,9 +10,9 @@ import {
getRedirectStatus,
} from '../lib/check-custom-routes'
import {
SPR_GET_INITIAL_PROPS_CONFLICT,
SSG_GET_INITIAL_PROPS_CONFLICT,
SERVER_PROPS_GET_INIT_PROPS_CONFLICT,
SERVER_PROPS_SPR_CONFLICT,
SERVER_PROPS_SSG_CONFLICT,
} from '../lib/constants'
import prettyBytes from '../lib/pretty-bytes'
import { recursiveReadDir } from '../lib/recursive-readdir'
Expand Down Expand Up @@ -486,7 +486,7 @@ export async function isPageStatic(
static?: boolean
prerender?: boolean
isHybridAmp?: boolean
serverProps?: boolean
hasServerProps?: boolean
prerenderRoutes?: string[] | undefined
}> {
try {
Expand All @@ -513,15 +513,15 @@ 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)
}

if (hasGetInitialProps && hasServerProps) {
throw new Error(SERVER_PROPS_GET_INIT_PROPS_CONFLICT)
}

if (hasStaticProps && hasServerProps) {
throw new Error(SERVER_PROPS_SPR_CONFLICT)
throw new Error(SERVER_PROPS_SSG_CONFLICT)
}

// A page cannot have static parameters if it is not a dynamic page.
Expand Down Expand Up @@ -596,8 +596,8 @@ export async function isPageStatic(
static: !hasStaticProps && !hasGetInitialProps && !hasServerProps,
isHybridAmp: config.amp === 'hybrid',
prerenderRoutes: prerenderPaths,
serverProps: hasServerProps,
prerender: hasStaticProps,
hasServerProps,
}
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return {}
Expand Down
2 changes: 2 additions & 0 deletions packages/next/build/webpack/loaders/next-serverless-loader.ts
Expand Up @@ -273,6 +273,8 @@ const nextServerlessLoader: loader.Loader = function() {
`
: `const nowParams = null;`
}
renderOpts.params = _params || params
let result = await renderToHTML(req, res, "${page}", Object.assign({}, unstable_getStaticProps ? {} : parsedUrl.query, nowParams ? nowParams : params, _params), renderOpts)
if (_nextData && !fromExport) {
Expand Down
4 changes: 2 additions & 2 deletions packages/next/lib/constants.ts
Expand Up @@ -24,8 +24,8 @@ 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`

export const SERVER_PROPS_GET_INIT_PROPS_CONFLICT = `You can not use getInitialProps with unstable_getServerProps. Please remove one or the other`

export const SERVER_PROPS_SPR_CONFLICT = `You can not use unstable_getStaticProps with unstable_getServerProps. To use SPR, please remove your unstable_getServerProps`
export const SERVER_PROPS_SSG_CONFLICT = `You can not use unstable_getStaticProps with unstable_getServerProps. To use SSG, please remove your unstable_getServerProps`
1 change: 1 addition & 0 deletions packages/next/next-server/server/load-components.ts
Expand Up @@ -25,6 +25,7 @@ export type LoadComponentsReturnType = {
}
unstable_getStaticPaths?: () => void
unstable_getServerProps?: (context: {
params: { [key: string]: string }
req: IncomingMessage
res: ServerResponse
query: ParsedUrlQuery
Expand Down
1 change: 1 addition & 0 deletions packages/next/next-server/server/next-server.ts
Expand Up @@ -1026,6 +1026,7 @@ export default class Server {
result,
{
...this.renderOpts,
params,
amphtml,
hasAmp,
}
Expand Down
12 changes: 8 additions & 4 deletions packages/next/next-server/server/render.tsx
Expand Up @@ -29,9 +29,9 @@ import { isInAmpMode } from '../lib/amp'
import { PageConfig } from 'next/types'
import { isDynamicRoute } from '../lib/router/utils/is-dynamic'
import {
SPR_GET_INITIAL_PROPS_CONFLICT,
SSG_GET_INITIAL_PROPS_CONFLICT,
SERVER_PROPS_GET_INIT_PROPS_CONFLICT,
SERVER_PROPS_SPR_CONFLICT,
SERVER_PROPS_SSG_CONFLICT,
} from '../../lib/constants'
import { AMP_RENDER_TARGET } from '../lib/constants'

Expand Down Expand Up @@ -160,11 +160,13 @@ type RenderOpts = {
}>
unstable_getStaticPaths?: () => void
unstable_getServerProps?: (context: {
params: { [key: string]: string }
req: IncomingMessage
res: ServerResponse
query: ParsedUrlQuery
}) => Promise<{ [key: string]: any }>
isDataReq: boolean
params: { [key: string]: string }
}

function renderDocument(
Expand Down Expand Up @@ -284,6 +286,7 @@ export async function renderToHTML(
unstable_getStaticPaths,
unstable_getServerProps,
isDataReq,
params,
} = renderOpts

const callMiddleware = async (method: string, args: any[], props = false) => {
Expand Down Expand Up @@ -339,15 +342,15 @@ 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 (hasPageGetInitialProps && unstable_getServerProps) {
throw new Error(SERVER_PROPS_GET_INIT_PROPS_CONFLICT + ` ${pathname}`)
}

if (unstable_getServerProps && isSpr) {
throw new Error(SERVER_PROPS_SPR_CONFLICT + ` ${pathname}`)
throw new Error(SERVER_PROPS_SSG_CONFLICT + ` ${pathname}`)
}

if (!!unstable_getStaticPaths && !isSpr) {
Expand Down Expand Up @@ -503,6 +506,7 @@ export async function renderToHTML(

if (unstable_getServerProps) {
props.pageProps = await unstable_getServerProps({
params,
query,
req,
res,
Expand Down
14 changes: 7 additions & 7 deletions test/integration/getserverprops/pages/blog/[post]/index.js
Expand Up @@ -3,30 +3,30 @@ import Link from 'next/link'
import { useRouter } from 'next/router'

// eslint-disable-next-line camelcase
export async function unstable_getServerProps({ query }) {
if (query.post === 'post-10') {
export async function unstable_getServerProps({ params }) {
if (params.post === 'post-10') {
await new Promise(resolve => {
setTimeout(() => resolve(), 1000)
})
}

if (query.post === 'post-100') {
if (params.post === 'post-100') {
throw new Error('such broken..')
}

return {
query,
post: query.post,
params,
post: params.post,
time: (await import('perf_hooks')).performance.now(),
}
}

export default ({ post, time, query }) => {
export default ({ post, time, params }) => {
return (
<>
<p>Post: {post}</p>
<span>time: {time}</span>
<div id="params">{JSON.stringify(query)}</div>
<div id="params">{JSON.stringify(params)}</div>
<div id="query">{JSON.stringify(useRouter().query)}</div>
<Link href="/">
<a id="home">to home</a>
Expand Down
4 changes: 2 additions & 2 deletions test/integration/getserverprops/pages/something.js
Expand Up @@ -3,10 +3,10 @@ import Link from 'next/link'
import { useRouter } from 'next/router'

// eslint-disable-next-line camelcase
export async function unstable_getServerProps({ query }) {
export async function unstable_getServerProps({ params }) {
return {
world: 'world',
params: query || {},
params: params || {},
time: new Date().getTime(),
random: Math.random(),
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/getserverprops/test/index.test.js
Expand Up @@ -164,7 +164,7 @@ const runTests = (dev = false) => {
const html = await renderViaHTTP(appPort, '/blog/post-1?hello=world')
const $ = cheerio.load(html)
const params = $('#params').text()
expect(JSON.parse(params)).toEqual({ hello: 'world', post: 'post-1' })
expect(JSON.parse(params)).toEqual({ post: 'post-1' })
const query = $('#query').text()
expect(JSON.parse(query)).toEqual({ hello: 'world', post: 'post-1' })
})
Expand Down

0 comments on commit 1ebb040

Please sign in to comment.