From 9db387e9d685f20c134de6bc7f489e0fa29be905 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 27 Feb 2020 10:42:34 -0600 Subject: [PATCH 1/7] Rename getServerProps to getServerSideProps --- docs/concepts/_data-fetching.md | 4 ++-- errors/404-get-initial-props.md | 2 +- examples/with-apollo/lib/apollo.js | 2 +- .../build/babel/plugins/next-ssg-transform.ts | 2 +- packages/next/build/utils.ts | 4 ++-- .../webpack/loaders/next-serverless-loader.ts | 12 +++++------ packages/next/lib/constants.ts | 6 +++--- .../next-server/server/load-components.ts | 20 +++++++++---------- .../next/next-server/server/next-server.ts | 2 +- packages/next/next-server/server/render.tsx | 20 +++++++++---------- packages/next/pages/_app.tsx | 2 +- test/integration/404-page/test/index.test.js | 4 ++-- .../pages/another/index.js | 2 +- .../pages/blog/[post]/[comment].js | 2 +- .../pages/blog/[post]/index.js | 2 +- .../pages/blog/index.js | 2 +- .../pages/catchall/[...path].js | 2 +- .../pages/default-revalidate.js | 2 +- .../pages/index.js | 2 +- .../pages/invalid-keys.js | 2 +- .../pages/normal.js | 0 .../pages/something.js | 2 +- .../pages/user/[user]/profile.js | 2 +- .../test/index.test.js | 14 ++++++------- .../world.txt | 0 .../pages/index.js | 2 +- .../pages/index.js.alt | 2 +- .../test/index.test.js | 6 +++--- 28 files changed, 62 insertions(+), 62 deletions(-) rename test/integration/{getserverprops => getserversideprops}/pages/another/index.js (93%) rename test/integration/{getserverprops => getserversideprops}/pages/blog/[post]/[comment].js (87%) rename test/integration/{getserverprops => getserversideprops}/pages/blog/[post]/index.js (92%) rename test/integration/{getserverprops => getserversideprops}/pages/blog/index.js (89%) rename test/integration/{getserverprops => getserversideprops}/pages/catchall/[...path].js (92%) rename test/integration/{getserverprops => getserversideprops}/pages/default-revalidate.js (88%) rename test/integration/{getserverprops => getserversideprops}/pages/index.js (95%) rename test/integration/{getserverprops => getserversideprops}/pages/invalid-keys.js (91%) rename test/integration/{getserverprops => getserversideprops}/pages/normal.js (100%) rename test/integration/{getserverprops => getserversideprops}/pages/something.js (92%) rename test/integration/{getserverprops => getserversideprops}/pages/user/[user]/profile.js (86%) rename test/integration/{getserverprops => getserversideprops}/test/index.test.js (96%) rename test/integration/{getserverprops => getserversideprops}/world.txt (100%) diff --git a/docs/concepts/_data-fetching.md b/docs/concepts/_data-fetching.md index 0e850853018414c..b5496b255f37de9 100644 --- a/docs/concepts/_data-fetching.md +++ b/docs/concepts/_data-fetching.md @@ -46,7 +46,7 @@ export default HomePage [Read more about how Server-Side Rendering works](). -To opt-in to Server-Side Rendering, making every request render HTML on-demand you use the `getServerProps` method. +To opt-in to Server-Side Rendering, making every request render HTML on-demand you use the `getServerSideProps` method. It allows you to fetch data before rendering starts when a request comes in. @@ -56,7 +56,7 @@ Taking the same example as Static Generation, but opted into rendering the page import fetch from 'isomorphic-unfetch' // Called when a request comes in. -export async function getServerProps() { +export async function getServerSideProps() { const res = await fetch('https://api.github.com/repos/zeit/next.js') const json = await res.json() diff --git a/errors/404-get-initial-props.md b/errors/404-get-initial-props.md index dc0e1ecb3510c7f..af7fde5a3b48b2e 100644 --- a/errors/404-get-initial-props.md +++ b/errors/404-get-initial-props.md @@ -2,7 +2,7 @@ #### Why This Error Occurred -In your `404.js` page you added `getInitialProps` or `getServerProps` which is not allowed as this prevents the page from being static and `404.js` is meant to provide more flexibility for a static 404 page. Having a static 404 page is the most ideal as this page can be served very often and if not static puts extra strain on your server and more invocations for serverless functions which increase the cost of hosting your site unnecessarily. +In your `404.js` page you added `getInitialProps` or `getServerSideProps` which is not allowed as this prevents the page from being static and `404.js` is meant to provide more flexibility for a static 404 page. Having a static 404 page is the most ideal as this page can be served very often and if not static puts extra strain on your server and more invocations for serverless functions which increase the cost of hosting your site unnecessarily. #### Possible Ways to Fix It diff --git a/examples/with-apollo/lib/apollo.js b/examples/with-apollo/lib/apollo.js index 17a40a6d309c702..bff375c2cf927c7 100644 --- a/examples/with-apollo/lib/apollo.js +++ b/examples/with-apollo/lib/apollo.js @@ -11,7 +11,7 @@ let globalApolloClient = null /** * Installs the Apollo Client on NextPageContext * or NextAppContext. Useful if you want to use apolloClient - * inside getStaticProps, getStaticPaths or getServerProps + * inside getStaticProps, getStaticPaths or getServerSideProps * @param {NextPageContext | NextAppContext} ctx */ export const initOnContext = ctx => { diff --git a/packages/next/build/babel/plugins/next-ssg-transform.ts b/packages/next/build/babel/plugins/next-ssg-transform.ts index 2a7cea08e6b7490..97a27c7f8ff1453 100644 --- a/packages/next/build/babel/plugins/next-ssg-transform.ts +++ b/packages/next/build/babel/plugins/next-ssg-transform.ts @@ -10,7 +10,7 @@ const pageComponentVar = '__NEXT_COMP' export const EXPORT_NAME_GET_STATIC_PROPS = 'unstable_getStaticProps' export const EXPORT_NAME_GET_STATIC_PATHS = 'unstable_getStaticPaths' -export const EXPORT_NAME_GET_SERVER_PROPS = 'unstable_getServerProps' +export const EXPORT_NAME_GET_SERVER_PROPS = 'unstable_getServerSideProps' const ssgExports = new Set([ EXPORT_NAME_GET_STATIC_PROPS, diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index c7ed129ecfad87c..8269949a55dd9b4 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -202,7 +202,7 @@ export async function printTreeView( serverless ? '(Lambda)' : '(Server)', `server-side renders at runtime (uses ${chalk.cyan( 'getInitialProps' - )} or ${chalk.cyan('getServerProps')})`, + )} or ${chalk.cyan('getServerSideProps')})`, ], [ '○', @@ -636,7 +636,7 @@ export async function isPageStatic( const hasGetInitialProps = !!(Comp as any).getInitialProps const hasStaticProps = !!mod.unstable_getStaticProps const hasStaticPaths = !!mod.unstable_getStaticPaths - const hasServerProps = !!mod.unstable_getServerProps + const hasServerProps = !!mod.unstable_getServerSideProps const hasLegacyStaticParams = !!mod.unstable_getStaticParams if (hasLegacyStaticParams) { diff --git a/packages/next/build/webpack/loaders/next-serverless-loader.ts b/packages/next/build/webpack/loaders/next-serverless-loader.ts index 47ab3582dcf4875..0b6c770fc1a31c7 100644 --- a/packages/next/build/webpack/loaders/next-serverless-loader.ts +++ b/packages/next/build/webpack/loaders/next-serverless-loader.ts @@ -220,7 +220,7 @@ const nextServerlessLoader: loader.Loader = function() { export const unstable_getStaticProps = ComponentInfo['unstable_getStaticProp' + 's'] export const unstable_getStaticParams = ComponentInfo['unstable_getStaticParam' + 's'] export const unstable_getStaticPaths = ComponentInfo['unstable_getStaticPath' + 's'] - export const unstable_getServerProps = ComponentInfo['unstable_getServerProp' + 's'] + export const unstable_getServerSideProps = ComponentInfo['unstable_getServerSideProp' + 's'] ${dynamicRouteMatcher} ${handleRewrites} @@ -242,7 +242,7 @@ const nextServerlessLoader: loader.Loader = function() { Document, buildManifest, unstable_getStaticProps, - unstable_getServerProps, + unstable_getServerSideProps, unstable_getStaticPaths, reactLoadableManifest, canonicalBase: "${canonicalBase}", @@ -275,7 +275,7 @@ const nextServerlessLoader: loader.Loader = function() { ${page === '/_error' ? `res.statusCode = 404` : ''} ${ pageIsDynamicRoute - ? `const params = fromExport && !unstable_getStaticProps && !unstable_getServerProps ? {} : dynamicRouteMatcher(parsedUrl.pathname) || {};` + ? `const params = fromExport && !unstable_getStaticProps && !unstable_getServerSideProps ? {} : dynamicRouteMatcher(parsedUrl.pathname) || {};` : `const params = {};` } ${ @@ -331,7 +331,7 @@ const nextServerlessLoader: loader.Loader = function() { 'Cache-Control', isPreviewMode ? \`private, no-cache, no-store, max-age=0, must-revalidate\` - : unstable_getServerProps + : unstable_getServerSideProps ? \`no-cache, no-store, must-revalidate\` : \`s-maxage=\${renderOpts.revalidate}, stale-while-revalidate\` ) @@ -352,7 +352,7 @@ const nextServerlessLoader: loader.Loader = function() { const result = await renderToHTML(req, res, "/_error", parsedUrl.query, Object.assign({}, options, { unstable_getStaticProps: undefined, unstable_getStaticPaths: undefined, - unstable_getServerProps: undefined, + unstable_getServerSideProps: undefined, Component: Error })) return result @@ -362,7 +362,7 @@ const nextServerlessLoader: loader.Loader = function() { const result = await renderToHTML(req, res, "/_error", parsedUrl.query, Object.assign({}, options, { unstable_getStaticProps: undefined, unstable_getStaticPaths: undefined, - unstable_getServerProps: undefined, + unstable_getServerSideProps: undefined, Component: Error, err })) diff --git a/packages/next/lib/constants.ts b/packages/next/lib/constants.ts index 4391cbc755c04b2..1070627fad7a23a 100644 --- a/packages/next/lib/constants.ts +++ b/packages/next/lib/constants.ts @@ -26,8 +26,8 @@ export const PUBLIC_DIR_MIDDLEWARE_CONFLICT = `You can not have a '_next' folder 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_GET_INIT_PROPS_CONFLICT = `You can not use getInitialProps with unstable_getServerSideProps. Please remove one or the other` -export const SERVER_PROPS_SSG_CONFLICT = `You can not use unstable_getStaticProps with unstable_getServerProps. To use SSG, please remove your unstable_getServerProps` +export const SERVER_PROPS_SSG_CONFLICT = `You can not use unstable_getStaticProps with unstable_getServerSideProps. To use SSG, please remove your unstable_getServerSideProps` -export const PAGES_404_GET_INITIAL_PROPS_ERROR = `\`pages/404\` can not have getInitialProps/getServerProps, https://err.sh/zeit/next.js/404-get-initial-props` +export const PAGES_404_GET_INITIAL_PROPS_ERROR = `\`pages/404\` can not have getInitialProps/getServerSideProps, https://err.sh/zeit/next.js/404-get-initial-props` diff --git a/packages/next/next-server/server/load-components.ts b/packages/next/next-server/server/load-components.ts index e4052770e8aebac..856ea05a6efd8b2 100644 --- a/packages/next/next-server/server/load-components.ts +++ b/packages/next/next-server/server/load-components.ts @@ -21,13 +21,13 @@ export function interopDefault(mod: any) { function addComponentPropsId( Component: any, getStaticProps: any, - getServerProps: any + getServerSideProps: any ) { // Mark the component with the SSG or SSP id here since we don't run // the SSG babel transform for server mode if (getStaticProps) { Component[STATIC_PROPS_ID] = true - } else if (getServerProps) { + } else if (getServerSideProps) { Component[SERVER_PROPS_ID] = true } } @@ -55,7 +55,7 @@ export type Unstable_getStaticPaths = () => Promise<{ fallback: boolean }> -type Unstable_getServerProps = (context: { +type Unstable_getServerSideProps = (context: { params: ParsedUrlQuery | undefined req: IncomingMessage res: ServerResponse @@ -72,7 +72,7 @@ export type LoadComponentsReturnType = { App: AppType unstable_getStaticProps?: Unstable_getStaticProps unstable_getStaticPaths?: Unstable_getStaticPaths - unstable_getServerProps?: Unstable_getServerProps + unstable_getServerSideProps?: Unstable_getServerSideProps } export async function loadComponents( @@ -86,13 +86,13 @@ export async function loadComponents( const { unstable_getStaticProps, unstable_getStaticPaths, - unstable_getServerProps, + unstable_getServerSideProps, } = Component addComponentPropsId( Component, unstable_getStaticProps, - unstable_getServerProps + unstable_getServerSideProps ) return { @@ -100,7 +100,7 @@ export async function loadComponents( pageConfig: Component.config || {}, unstable_getStaticProps, unstable_getStaticPaths, - unstable_getServerProps, + unstable_getServerSideProps, } as LoadComponentsReturnType } const documentPath = join( @@ -142,7 +142,7 @@ export async function loadComponents( ]) const { - unstable_getServerProps, + unstable_getServerSideProps, unstable_getStaticProps, unstable_getStaticPaths, } = ComponentMod @@ -150,7 +150,7 @@ export async function loadComponents( addComponentPropsId( Component, unstable_getStaticProps, - unstable_getServerProps + unstable_getServerSideProps ) return { @@ -161,7 +161,7 @@ export async function loadComponents( DocumentMiddleware, reactLoadableManifest, pageConfig: ComponentMod.config || {}, - unstable_getServerProps, + unstable_getServerSideProps, unstable_getStaticProps, unstable_getStaticPaths, } diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index c02696bcf707941..4e1ec838ac3eefa 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -889,7 +889,7 @@ export default class Server { typeof components.Component === 'object' && typeof (components.Component as any).renderReqToHTML === 'function' const isSSG = !!components.unstable_getStaticProps - const isServerProps = !!components.unstable_getServerProps + const isServerProps = !!components.unstable_getServerSideProps const hasStaticPaths = !!components.unstable_getStaticPaths // Toggle whether or not this is a Data request diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index 8e848519c1c8119..287dd361ed7dc21 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -275,7 +275,7 @@ export async function renderToHTML( ErrorDebug, unstable_getStaticProps, unstable_getStaticPaths, - unstable_getServerProps, + unstable_getServerSideProps, isDataReq, params, previewProps, @@ -323,7 +323,7 @@ export async function renderToHTML( !hasPageGetInitialProps && defaultAppGetInitialProps && !isSpr && - !unstable_getServerProps + !unstable_getServerSideProps if ( process.env.NODE_ENV !== 'production' && @@ -349,11 +349,11 @@ export async function renderToHTML( throw new Error(SSG_GET_INITIAL_PROPS_CONFLICT + ` ${pathname}`) } - if (hasPageGetInitialProps && unstable_getServerProps) { + if (hasPageGetInitialProps && unstable_getServerSideProps) { throw new Error(SERVER_PROPS_GET_INIT_PROPS_CONFLICT + ` ${pathname}`) } - if (unstable_getServerProps && isSpr) { + if (unstable_getServerSideProps && isSpr) { throw new Error(SERVER_PROPS_SSG_CONFLICT + ` ${pathname}`) } @@ -529,8 +529,8 @@ export async function renderToHTML( console.error(err) } - if (unstable_getServerProps && !isFallback) { - const data = await unstable_getServerProps({ + if (unstable_getServerSideProps && !isFallback) { + const data = await unstable_getServerSideProps({ params, query, req, @@ -540,7 +540,7 @@ export async function renderToHTML( const invalidKeys = Object.keys(data).filter(key => key !== 'props') if (invalidKeys.length) { - throw new Error(invalidKeysMsg('getServerProps', invalidKeys)) + throw new Error(invalidKeysMsg('getServerSideProps', invalidKeys)) } props.pageProps = data.props @@ -549,7 +549,7 @@ export async function renderToHTML( if ( !isSpr && // we only show this warning for legacy pages - !unstable_getServerProps && + !unstable_getServerSideProps && process.env.NODE_ENV !== 'production' && Object.keys(props?.pageProps || {}).includes('url') ) { @@ -560,10 +560,10 @@ export async function renderToHTML( } // We only need to do this if we want to support calling - // _app's getInitialProps for getServerProps if not this can be removed + // _app's getInitialProps for getServerSideProps if not this can be removed if (isDataReq) return props - // We don't call getStaticProps or getServerProps while generating + // We don't call getStaticProps or getServerSideProps while generating // the fallback so make sure to set pageProps to an empty object if (isFallback) { props.pageProps = {} diff --git a/packages/next/pages/_app.tsx b/packages/next/pages/_app.tsx index 0132f5ab4825ddc..32727d7f2ea94a5 100644 --- a/packages/next/pages/_app.tsx +++ b/packages/next/pages/_app.tsx @@ -48,7 +48,7 @@ export default class App

extends React.Component< {...pageProps} { // we don't add the legacy URL prop if it's using non-legacy - // methods like getStaticProps and getServerProps + // methods like getStaticProps and getServerSideProps ...(!((Component as any).__N_SSG || (Component as any).__N_SSP) ? { url: createUrl(router) } : {}) diff --git a/test/integration/404-page/test/index.test.js b/test/integration/404-page/test/index.test.js index 0656fe67c1c3bb4..9029af04c2f982d 100644 --- a/test/integration/404-page/test/index.test.js +++ b/test/integration/404-page/test/index.test.js @@ -149,7 +149,7 @@ describe('404 Page Support', () => { await fs.move(`${pages404}.bak`, pages404) expect(stderr).toContain( - `\`pages/404\` can not have getInitialProps/getServerProps, https://err.sh/zeit/next.js/404-get-initial-props` + `\`pages/404\` can not have getInitialProps/getServerSideProps, https://err.sh/zeit/next.js/404-get-initial-props` ) expect(code).toBe(1) }) @@ -180,7 +180,7 @@ describe('404 Page Support', () => { await fs.remove(pages404) await fs.move(`${pages404}.bak`, pages404) - const error = `\`pages/404\` can not have getInitialProps/getServerProps, https://err.sh/zeit/next.js/404-get-initial-props` + const error = `\`pages/404\` can not have getInitialProps/getServerSideProps, https://err.sh/zeit/next.js/404-get-initial-props` expect(stderr).toContain(error) }) diff --git a/test/integration/getserverprops/pages/another/index.js b/test/integration/getserversideprops/pages/another/index.js similarity index 93% rename from test/integration/getserverprops/pages/another/index.js rename to test/integration/getserversideprops/pages/another/index.js index a5c3afb6343c63f..de46cda8bd03413 100644 --- a/test/integration/getserverprops/pages/another/index.js +++ b/test/integration/getserversideprops/pages/another/index.js @@ -3,7 +3,7 @@ import fs from 'fs' import findUp from 'find-up' // eslint-disable-next-line camelcase -export async function unstable_getServerProps() { +export async function unstable_getServerSideProps() { const text = fs .readFileSync( findUp.sync('world.txt', { diff --git a/test/integration/getserverprops/pages/blog/[post]/[comment].js b/test/integration/getserversideprops/pages/blog/[post]/[comment].js similarity index 87% rename from test/integration/getserverprops/pages/blog/[post]/[comment].js rename to test/integration/getserversideprops/pages/blog/[post]/[comment].js index 292c98024942dc3..a5a42247ecb12a9 100644 --- a/test/integration/getserverprops/pages/blog/[post]/[comment].js +++ b/test/integration/getserversideprops/pages/blog/[post]/[comment].js @@ -2,7 +2,7 @@ import React from 'react' import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getServerProps({ query }) { +export async function unstable_getServerSideProps({ query }) { return { props: { post: query.post, diff --git a/test/integration/getserverprops/pages/blog/[post]/index.js b/test/integration/getserversideprops/pages/blog/[post]/index.js similarity index 92% rename from test/integration/getserverprops/pages/blog/[post]/index.js rename to test/integration/getserversideprops/pages/blog/[post]/index.js index aa4e0c5cc2bd12a..ba9c5978ea8891c 100644 --- a/test/integration/getserverprops/pages/blog/[post]/index.js +++ b/test/integration/getserversideprops/pages/blog/[post]/index.js @@ -3,7 +3,7 @@ import Link from 'next/link' import { useRouter } from 'next/router' // eslint-disable-next-line camelcase -export async function unstable_getServerProps({ params }) { +export async function unstable_getServerSideProps({ params }) { if (params.post === 'post-10') { await new Promise(resolve => { setTimeout(() => resolve(), 1000) diff --git a/test/integration/getserverprops/pages/blog/index.js b/test/integration/getserversideprops/pages/blog/index.js similarity index 89% rename from test/integration/getserverprops/pages/blog/index.js rename to test/integration/getserversideprops/pages/blog/index.js index 056a1860a3e3dfa..be51f782e42d6ca 100644 --- a/test/integration/getserverprops/pages/blog/index.js +++ b/test/integration/getserversideprops/pages/blog/index.js @@ -2,7 +2,7 @@ import React from 'react' import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getServerProps() { +export async function unstable_getServerSideProps() { return { props: { slugs: ['post-1', 'post-2'], diff --git a/test/integration/getserverprops/pages/catchall/[...path].js b/test/integration/getserversideprops/pages/catchall/[...path].js similarity index 92% rename from test/integration/getserverprops/pages/catchall/[...path].js rename to test/integration/getserversideprops/pages/catchall/[...path].js index 5e1bd3543f63fec..45efb3e6c85f68f 100644 --- a/test/integration/getserverprops/pages/catchall/[...path].js +++ b/test/integration/getserversideprops/pages/catchall/[...path].js @@ -3,7 +3,7 @@ import Link from 'next/link' import { useRouter } from 'next/router' // eslint-disable-next-line camelcase -export async function unstable_getServerProps({ params }) { +export async function unstable_getServerSideProps({ params }) { return { props: { world: 'world', diff --git a/test/integration/getserverprops/pages/default-revalidate.js b/test/integration/getserversideprops/pages/default-revalidate.js similarity index 88% rename from test/integration/getserverprops/pages/default-revalidate.js rename to test/integration/getserversideprops/pages/default-revalidate.js index 15bb81ba617e876..582ef524ec1a408 100644 --- a/test/integration/getserverprops/pages/default-revalidate.js +++ b/test/integration/getserversideprops/pages/default-revalidate.js @@ -1,7 +1,7 @@ import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getServerProps() { +export async function unstable_getServerSideProps() { return { props: { world: 'world', diff --git a/test/integration/getserverprops/pages/index.js b/test/integration/getserversideprops/pages/index.js similarity index 95% rename from test/integration/getserverprops/pages/index.js rename to test/integration/getserversideprops/pages/index.js index f906758f846b1d8..fc84e9848d8d6f6 100644 --- a/test/integration/getserverprops/pages/index.js +++ b/test/integration/getserversideprops/pages/index.js @@ -1,7 +1,7 @@ import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getServerProps() { +export async function unstable_getServerSideProps() { return { props: { world: 'world', diff --git a/test/integration/getserverprops/pages/invalid-keys.js b/test/integration/getserversideprops/pages/invalid-keys.js similarity index 91% rename from test/integration/getserverprops/pages/invalid-keys.js rename to test/integration/getserversideprops/pages/invalid-keys.js index 6eeba67fb6a7730..4184eb8bcfabca8 100644 --- a/test/integration/getserverprops/pages/invalid-keys.js +++ b/test/integration/getserversideprops/pages/invalid-keys.js @@ -3,7 +3,7 @@ import Link from 'next/link' import { useRouter } from 'next/router' // eslint-disable-next-line camelcase -export async function unstable_getServerProps({ params, query }) { +export async function unstable_getServerSideProps({ params, query }) { return { world: 'world', query: query || {}, diff --git a/test/integration/getserverprops/pages/normal.js b/test/integration/getserversideprops/pages/normal.js similarity index 100% rename from test/integration/getserverprops/pages/normal.js rename to test/integration/getserversideprops/pages/normal.js diff --git a/test/integration/getserverprops/pages/something.js b/test/integration/getserversideprops/pages/something.js similarity index 92% rename from test/integration/getserverprops/pages/something.js rename to test/integration/getserversideprops/pages/something.js index f3d5ef1ef4de2f0..361bbb44b5097ce 100644 --- a/test/integration/getserverprops/pages/something.js +++ b/test/integration/getserversideprops/pages/something.js @@ -3,7 +3,7 @@ import Link from 'next/link' import { useRouter } from 'next/router' // eslint-disable-next-line camelcase -export async function unstable_getServerProps({ params, query }) { +export async function unstable_getServerSideProps({ params, query }) { return { props: { world: 'world', diff --git a/test/integration/getserverprops/pages/user/[user]/profile.js b/test/integration/getserversideprops/pages/user/[user]/profile.js similarity index 86% rename from test/integration/getserverprops/pages/user/[user]/profile.js rename to test/integration/getserversideprops/pages/user/[user]/profile.js index a4fb1e13403bf04..c14992d56d4dc22 100644 --- a/test/integration/getserverprops/pages/user/[user]/profile.js +++ b/test/integration/getserversideprops/pages/user/[user]/profile.js @@ -2,7 +2,7 @@ import React from 'react' import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getServerProps({ query }) { +export async function unstable_getServerSideProps({ query }) { return { props: { user: query.user, diff --git a/test/integration/getserverprops/test/index.test.js b/test/integration/getserversideprops/test/index.test.js similarity index 96% rename from test/integration/getserverprops/test/index.test.js rename to test/integration/getserversideprops/test/index.test.js index 84707e9eccf540e..963215c85850225 100644 --- a/test/integration/getserverprops/test/index.test.js +++ b/test/integration/getserversideprops/test/index.test.js @@ -186,7 +186,7 @@ const runTests = (dev = false) => { expect(html).toMatch(/hello.*?world/) }) - it('should SSR getServerProps page correctly', async () => { + it('should SSR getServerSideProps page correctly', async () => { const html = await renderViaHTTP(appPort, '/blog/post-1') expect(html).toMatch(/Post:.*?post-1/) }) @@ -298,7 +298,7 @@ const runTests = (dev = false) => { expect(await browser.eval('window.beforeClick')).not.toBe('true') }) - it('should always call getServerProps without caching', async () => { + it('should always call getServerSideProps without caching', async () => { const initialRes = await fetchViaHTTP(appPort, '/something') const initialHtml = await initialRes.text() expect(initialHtml).toMatch(/hello.*?world/) @@ -314,7 +314,7 @@ const runTests = (dev = false) => { expect(newHtml !== newerHtml).toBe(true) }) - it('should not re-call getServerProps when updating query', async () => { + it('should not re-call getServerSideProps when updating query', async () => { const browser = await webdriver(appPort, '/something?hello=world') await waitFor(2000) @@ -337,7 +337,7 @@ const runTests = (dev = false) => { await fs.writeFile( urlPropPage, ` - export async function unstable_getServerProps() { + export async function unstable_getServerSideProps() { return { props: { url: 'something' @@ -357,10 +357,10 @@ const runTests = (dev = false) => { expect(html).toMatch(/url:.*?something/) }) - it('should show error for extra keys returned from getServerProps', async () => { + it('should show error for extra keys returned from getServerSideProps', async () => { const html = await renderViaHTTP(appPort, '/invalid-keys') expect(html).toContain( - `Additional keys were returned from \`getServerProps\`. Properties intended for your component must be nested under the \`props\` key, e.g.:` + `Additional keys were returned from \`getServerSideProps\`. Properties intended for your component must be nested under the \`props\` key, e.g.:` ) expect(html).toContain( `Keys that need to be moved: world, query, params, time, random` @@ -396,7 +396,7 @@ const runTests = (dev = false) => { } } -describe('unstable_getServerProps', () => { +describe('unstable_getServerSideProps', () => { describe('dev mode', () => { beforeAll(async () => { stderr = '' diff --git a/test/integration/getserverprops/world.txt b/test/integration/getserversideprops/world.txt similarity index 100% rename from test/integration/getserverprops/world.txt rename to test/integration/getserversideprops/world.txt diff --git a/test/integration/mixed-ssg-serverprops-error/pages/index.js b/test/integration/mixed-ssg-serverprops-error/pages/index.js index dedf3d00be14bed..7bf4d452e293240 100644 --- a/test/integration/mixed-ssg-serverprops-error/pages/index.js +++ b/test/integration/mixed-ssg-serverprops-error/pages/index.js @@ -4,7 +4,7 @@ export const unstable_getStaticProps = async () => { } } -export const unstable_getServerProps = async () => { +export const unstable_getServerSideProps = async () => { return { props: { world: 'world' }, } diff --git a/test/integration/mixed-ssg-serverprops-error/pages/index.js.alt b/test/integration/mixed-ssg-serverprops-error/pages/index.js.alt index 4756984cc3a9369..9d6334f42ef07ad 100644 --- a/test/integration/mixed-ssg-serverprops-error/pages/index.js.alt +++ b/test/integration/mixed-ssg-serverprops-error/pages/index.js.alt @@ -4,7 +4,7 @@ export const unstable_getStaticPaths = async () => { } } -export const unstable_getServerProps = async () => { +export const unstable_getServerSideProps = async () => { return { props: { world: 'world' } } diff --git a/test/integration/mixed-ssg-serverprops-error/test/index.test.js b/test/integration/mixed-ssg-serverprops-error/test/index.test.js index 794cf77cae22f8b..27fdee631a1a118 100644 --- a/test/integration/mixed-ssg-serverprops-error/test/index.test.js +++ b/test/integration/mixed-ssg-serverprops-error/test/index.test.js @@ -11,13 +11,13 @@ const indexPage = join(appDir, 'pages/index.js') const indexPageAlt = `${indexPage}.alt` const indexPageBak = `${indexPage}.bak` -describe('Mixed getStaticProps and getServerProps error', () => { - it('should error when exporting both getStaticProps and getServerProps', async () => { +describe('Mixed getStaticProps and getServerSideProps error', () => { + it('should error when exporting both getStaticProps and getServerSideProps', async () => { const { stderr } = await nextBuild(appDir, [], { stderr: true }) expect(stderr).toContain(SERVER_PROPS_SSG_CONFLICT) }) - it('should error when exporting both getStaticPaths and getServerProps', async () => { + it('should error when exporting both getStaticPaths and getServerSideProps', async () => { await fs.move(indexPage, indexPageBak) await fs.move(indexPageAlt, indexPage) From a22532744bfa1d00ad901ace75c643b2477393c5 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 27 Feb 2020 10:59:51 -0600 Subject: [PATCH 2/7] Remove unstable_ prefix from new methods --- errors/invalid-getstaticpaths-value.md | 10 +-- errors/invalid-getstaticprops-value.md | 8 +- .../z-experimental-next-news/pages/ask.js | 2 +- .../z-experimental-next-news/pages/index.js | 2 +- .../pages/item/[id].js | 2 +- .../z-experimental-next-news/pages/jobs.js | 2 +- .../z-experimental-next-news/pages/newest.js | 2 +- .../pages/news/[id].js | 2 +- .../z-experimental-next-news/pages/show.js | 2 +- .../build/babel/plugins/next-ssg-transform.ts | 6 +- packages/next/build/index.ts | 2 +- packages/next/build/utils.ts | 32 ++++---- .../webpack/loaders/next-serverless-loader.ts | 30 ++++---- packages/next/export/worker.js | 15 ++-- packages/next/lib/constants.ts | 6 +- .../next-server/server/load-components.ts | 73 +++++-------------- .../next/next-server/server/next-server.ts | 8 +- packages/next/next-server/server/render.tsx | 30 ++++---- packages/next/server/static-paths-worker.ts | 6 +- packages/next/types/index.d.ts | 23 ++++++ .../pages/[slug].js | 2 +- .../test/index.test.js | 2 +- .../pages/p1/p2/all-ssg/[...rest].js | 4 +- .../p1/p2/nested-all-ssg/[...rest]/index.js | 4 +- .../pages/p1/p2/predefined-ssg/[...rest].js | 4 +- .../getserversideprops/pages/another/index.js | 2 +- .../pages/blog/[post]/[comment].js | 2 +- .../pages/blog/[post]/index.js | 2 +- .../getserversideprops/pages/blog/index.js | 2 +- .../pages/catchall/[...path].js | 2 +- .../pages/default-revalidate.js | 2 +- .../getserversideprops/pages/index.js | 2 +- .../getserversideprops/pages/invalid-keys.js | 2 +- .../getserversideprops/pages/something.js | 2 +- .../pages/user/[user]/profile.js | 2 +- .../getserversideprops/test/index.test.js | 4 +- .../pages/index.js | 4 +- .../pages/index.js.alt | 4 +- .../pages/[...slug].js | 4 +- .../test/index.test.js | 2 +- .../pages/[foo]/[post].js | 4 +- .../prerender-legacy/pages/blog/[post].js | 2 +- .../prerender-legacy/test/index.test.js | 4 +- .../prerender-preview/pages/index.js | 2 +- .../prerender/pages/another/index.js | 2 +- .../prerender/pages/blog/[post]/[comment].js | 4 +- .../prerender/pages/blog/[post]/index.js | 4 +- .../integration/prerender/pages/blog/index.js | 2 +- .../pages/catchall-explicit/[...slug].js | 4 +- .../prerender/pages/catchall/[...slug].js | 4 +- .../prerender/pages/default-revalidate.js | 2 +- test/integration/prerender/pages/index.js | 2 +- test/integration/prerender/pages/something.js | 2 +- .../prerender/pages/user/[user]/profile.js | 4 +- test/integration/prerender/test/index.test.js | 14 ++-- .../babel-plugin-next-ssg-transform.test.js | 62 ++++++++-------- test/unit/next-babel-loader.test.js | 10 +-- 57 files changed, 216 insertions(+), 229 deletions(-) diff --git a/errors/invalid-getstaticpaths-value.md b/errors/invalid-getstaticpaths-value.md index 47dfe60a7bbaf43..f1e8543d9503e6b 100644 --- a/errors/invalid-getstaticpaths-value.md +++ b/errors/invalid-getstaticpaths-value.md @@ -1,15 +1,15 @@ -# Invalid unstable_getStaticPaths Return Value +# Invalid getStaticPaths Return Value #### Why This Error Occurred -In one of the page's `unstable_getStaticPaths` the return value had the incorrect shape. +In one of the page's `getStaticPaths` the return value had the incorrect shape. #### Possible Ways to Fix It -Make sure to return the following shape from `unstable_getStaticPaths`: +Make sure to return the following shape from `getStaticPaths`: ```js -export async function unstable_getStaticPaths() { +export async function getStaticPaths() { return { paths: Array, fallback: boolean @@ -23,7 +23,7 @@ There are two required properties: - You may return a [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) or an [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) that explicitly defines all URL `params`. ```js // pages/blog/[slug].js - export async function unstable_getStaticPaths() { + export async function getStaticPaths() { return { paths: [ // String variant: diff --git a/errors/invalid-getstaticprops-value.md b/errors/invalid-getstaticprops-value.md index 5368ba600194ab4..ace1cdceb52bc95 100644 --- a/errors/invalid-getstaticprops-value.md +++ b/errors/invalid-getstaticprops-value.md @@ -1,15 +1,15 @@ -# Invalid unstable_getStaticProps Return Value +# Invalid getStaticProps Return Value #### Why This Error Occurred -In one of the page's `unstable_getStaticProps` the return value had the incorrect shape. +In one of the page's `getStaticProps` the return value had the incorrect shape. #### Possible Ways to Fix It -Make sure to return the following shape from `unstable_getStaticProps`: +Make sure to return the following shape from `getStaticProps`: ```js -export async function unstable_getStaticProps() { +export async function getStaticProps() { return { props: { [key: string]: any } } diff --git a/examples/z-experimental-next-news/pages/ask.js b/examples/z-experimental-next-news/pages/ask.js index 4757d7d97623a7d..93e84e5f4bb7ecf 100644 --- a/examples/z-experimental-next-news/pages/ask.js +++ b/examples/z-experimental-next-news/pages/ask.js @@ -4,7 +4,7 @@ import Stories from '../components/stories' import getStories from '../lib/get-stories' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps() { +export async function getStaticProps() { const page = 1 const stories = await getStories('askstories', { page }) return { props: { page, stories } } diff --git a/examples/z-experimental-next-news/pages/index.js b/examples/z-experimental-next-news/pages/index.js index 326cfc2e8fdf3c4..78d5ff36a8f942b 100644 --- a/examples/z-experimental-next-news/pages/index.js +++ b/examples/z-experimental-next-news/pages/index.js @@ -4,7 +4,7 @@ import Stories from '../components/stories' import getStories from '../lib/get-stories' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps() { +export async function getStaticProps() { const page = 1 const stories = await getStories('topstories', { page }) return { props: { page, stories } } diff --git a/examples/z-experimental-next-news/pages/item/[id].js b/examples/z-experimental-next-news/pages/item/[id].js index c44e60a6dfeffc1..12957562d0b6a34 100644 --- a/examples/z-experimental-next-news/pages/item/[id].js +++ b/examples/z-experimental-next-news/pages/item/[id].js @@ -4,7 +4,7 @@ import Item from '../../components/item' import getItem from '../../lib/get-item' import getComments from '../../lib/get-comments' -export async function unstable_getStaticProps({ params }) { +export async function getStaticProps({ params }) { const story = await getItem(params.id) return { props: { story } } } diff --git a/examples/z-experimental-next-news/pages/jobs.js b/examples/z-experimental-next-news/pages/jobs.js index 5fd4bf202e33d4c..2d7e6607d9fcd40 100644 --- a/examples/z-experimental-next-news/pages/jobs.js +++ b/examples/z-experimental-next-news/pages/jobs.js @@ -4,7 +4,7 @@ import Stories from '../components/stories' import getStories from '../lib/get-stories' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps() { +export async function getStaticProps() { const page = 1 const stories = await getStories('jobstories', { page }) return { props: { page, stories } } diff --git a/examples/z-experimental-next-news/pages/newest.js b/examples/z-experimental-next-news/pages/newest.js index 60c39074d15a4a3..ed276101b8e99b0 100644 --- a/examples/z-experimental-next-news/pages/newest.js +++ b/examples/z-experimental-next-news/pages/newest.js @@ -4,7 +4,7 @@ import Stories from '../components/stories' import getStories from '../lib/get-stories' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps() { +export async function getStaticProps() { const page = 1 const stories = await getStories('newstories', { page }) return { props: { page, stories } } diff --git a/examples/z-experimental-next-news/pages/news/[id].js b/examples/z-experimental-next-news/pages/news/[id].js index a5251da89db3793..4a77671d64eb610 100644 --- a/examples/z-experimental-next-news/pages/news/[id].js +++ b/examples/z-experimental-next-news/pages/news/[id].js @@ -4,7 +4,7 @@ import Stories from '../../components/stories' import getStories from '../../lib/get-stories' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps({ params }) { +export async function getStaticProps({ params }) { const page = Number(params.id) const stories = await getStories('topstories', { page }) return { props: { page, stories } } diff --git a/examples/z-experimental-next-news/pages/show.js b/examples/z-experimental-next-news/pages/show.js index ba99ecb278b1fd7..61be9121f09c68a 100644 --- a/examples/z-experimental-next-news/pages/show.js +++ b/examples/z-experimental-next-news/pages/show.js @@ -4,7 +4,7 @@ import Stories from '../components/stories' import getStories from '../lib/get-stories' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps() { +export async function getStaticProps() { const page = 1 const stories = await getStories('showstories', { page }) return { props: { page, stories } } diff --git a/packages/next/build/babel/plugins/next-ssg-transform.ts b/packages/next/build/babel/plugins/next-ssg-transform.ts index 97a27c7f8ff1453..3e38ea2226a8963 100644 --- a/packages/next/build/babel/plugins/next-ssg-transform.ts +++ b/packages/next/build/babel/plugins/next-ssg-transform.ts @@ -8,9 +8,9 @@ import { const pageComponentVar = '__NEXT_COMP' -export const EXPORT_NAME_GET_STATIC_PROPS = 'unstable_getStaticProps' -export const EXPORT_NAME_GET_STATIC_PATHS = 'unstable_getStaticPaths' -export const EXPORT_NAME_GET_SERVER_PROPS = 'unstable_getServerSideProps' +export const EXPORT_NAME_GET_STATIC_PROPS = 'getStaticProps' +export const EXPORT_NAME_GET_STATIC_PATHS = 'getStaticPaths' +export const EXPORT_NAME_GET_SERVER_PROPS = 'getServerSideProps' const ssgExports = new Set([ EXPORT_NAME_GET_STATIC_PROPS, diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 1ce20330f19fd11..6533ac91cfa1a1a 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -779,7 +779,7 @@ export default async function build(dir: string, conf = null): Promise { // For a dynamic SSG page, we did not copy its data exports and only // copy the fallback HTML file (if present). // We must also copy specific versions of this page as defined by - // `unstable_getStaticPaths` (additionalSsgPaths). + // `getStaticPaths` (additionalSsgPaths). const extraRoutes = additionalSsgPaths.get(page) || [] for (const route of extraRoutes) { await moveExportedPage(route, route, true, 'html') diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index 8269949a55dd9b4..3c9786b3a094448 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -15,7 +15,7 @@ import { recursiveReadDir } from '../lib/recursive-readdir' import { getRouteMatcher, getRouteRegex } from '../next-server/lib/router/utils' import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic' import { findPageFile } from '../server/lib/find-page-file' -import { Unstable_getStaticPaths } from '../next-server/server/load-components' +import { GetStaticPaths } from '../types/index' const fileGzipStats: { [k: string]: Promise } = {} const fsStatGzip = (file: string) => { @@ -500,7 +500,7 @@ export async function getPageSizeInKb( export async function buildStaticPaths( page: string, - unstable_getStaticPaths: Unstable_getStaticPaths + getStaticPaths: GetStaticPaths ): Promise<{ paths: string[]; fallback: boolean }> { const prerenderPaths = new Set() const _routeRegex = getRouteRegex(page) @@ -509,7 +509,7 @@ export async function buildStaticPaths( // Get the default list of allowed params. const _validParamKeys = Object.keys(_routeMatcher(page)) - const staticPathsResult = await unstable_getStaticPaths() + const staticPathsResult = await getStaticPaths() const expectedReturnVal = `Expected: { paths: [], fallback: boolean }\n` + @@ -521,7 +521,7 @@ export async function buildStaticPaths( Array.isArray(staticPathsResult) ) { throw new Error( - `Invalid value returned from unstable_getStaticPaths in ${page}. Received ${typeof staticPathsResult} ${expectedReturnVal}` + `Invalid value returned from getStaticPaths in ${page}. Received ${typeof staticPathsResult} ${expectedReturnVal}` ) } @@ -531,7 +531,7 @@ export async function buildStaticPaths( if (invalidStaticPathKeys.length > 0) { throw new Error( - `Extra keys returned from unstable_getStaticPaths in ${page} (${invalidStaticPathKeys.join( + `Extra keys returned from getStaticPaths in ${page} (${invalidStaticPathKeys.join( ', ' )}) ${expectedReturnVal}` ) @@ -539,7 +539,7 @@ export async function buildStaticPaths( if (typeof staticPathsResult.fallback !== 'boolean') { throw new Error( - `The \`fallback\` key must be returned from unstable_getStaticPaths in ${page}.\n` + + `The \`fallback\` key must be returned from getStaticPaths in ${page}.\n` + expectedReturnVal ) } @@ -548,7 +548,7 @@ export async function buildStaticPaths( if (!Array.isArray(toPrerender)) { throw new Error( - `Invalid \`paths\` value returned from unstable_getStaticProps in ${page}.\n` + + `Invalid \`paths\` value returned from getStaticProps in ${page}.\n` + `\`paths\` must be an array of strings or objects of shape { params: [key: string]: string }` ) } @@ -572,7 +572,7 @@ export async function buildStaticPaths( const invalidKeys = Object.keys(entry).filter(key => key !== 'params') if (invalidKeys.length) { throw new Error( - `Additional keys were returned from \`unstable_getStaticPaths\` in page "${page}". ` + + `Additional keys were returned from \`getStaticPaths\` in page "${page}". ` + `URL Parameters intended for this dynamic route must be nested under the \`params\` key, i.e.:` + `\n\n\treturn { params: { ${_validParamKeys .map(k => `${k}: ...`) @@ -593,7 +593,7 @@ export async function buildStaticPaths( throw new Error( `A required parameter (${validParamKey}) was not provided as ${ repeat ? 'an array' : 'a string' - } in unstable_getStaticPaths for ${page}` + } in getStaticPaths for ${page}` ) } @@ -634,14 +634,14 @@ export async function isPageStatic( } const hasGetInitialProps = !!(Comp as any).getInitialProps - const hasStaticProps = !!mod.unstable_getStaticProps - const hasStaticPaths = !!mod.unstable_getStaticPaths - const hasServerProps = !!mod.unstable_getServerSideProps + const hasStaticProps = !!mod.getStaticProps + const hasStaticPaths = !!mod.getStaticPaths + const hasServerProps = !!mod.getServerSideProps const hasLegacyStaticParams = !!mod.unstable_getStaticParams if (hasLegacyStaticParams) { throw new Error( - `unstable_getStaticParams was replaced with unstable_getStaticPaths. Please update your code.` + `unstable_getStaticParams was replaced with getStaticPaths. Please update your code.` ) } @@ -663,14 +663,14 @@ export async function isPageStatic( // A page cannot have static parameters if it is not a dynamic page. if (hasStaticProps && hasStaticPaths && !pageIsDynamic) { throw new Error( - `unstable_getStaticPaths can only be used with dynamic pages, not '${page}'.` + + `getStaticPaths can only be used with dynamic pages, not '${page}'.` + `\nLearn more: https://nextjs.org/docs#dynamic-routing` ) } if (hasStaticProps && pageIsDynamic && !hasStaticPaths) { throw new Error( - `unstable_getStaticPaths is required for dynamic SSG pages and is missing for '${page}'.` + + `getStaticPaths is required for dynamic SSG pages and is missing for '${page}'.` + `\nRead more: https://err.sh/next.js/invalid-getstaticpaths-value` ) } @@ -681,7 +681,7 @@ export async function isPageStatic( ;({ paths: prerenderRoutes, fallback: prerenderFallback, - } = await buildStaticPaths(page, mod.unstable_getStaticPaths)) + } = await buildStaticPaths(page, mod.getStaticPaths)) } const config = mod.config || {} diff --git a/packages/next/build/webpack/loaders/next-serverless-loader.ts b/packages/next/build/webpack/loaders/next-serverless-loader.ts index 0b6c770fc1a31c7..f46fa18cbb548f5 100644 --- a/packages/next/build/webpack/loaders/next-serverless-loader.ts +++ b/packages/next/build/webpack/loaders/next-serverless-loader.ts @@ -217,10 +217,10 @@ const nextServerlessLoader: loader.Loader = function() { const Component = ComponentInfo.default export default Component - export const unstable_getStaticProps = ComponentInfo['unstable_getStaticProp' + 's'] + export const getStaticProps = ComponentInfo['getStaticProp' + 's'] export const unstable_getStaticParams = ComponentInfo['unstable_getStaticParam' + 's'] - export const unstable_getStaticPaths = ComponentInfo['unstable_getStaticPath' + 's'] - export const unstable_getServerSideProps = ComponentInfo['unstable_getServerSideProp' + 's'] + export const getStaticPaths = ComponentInfo['getStaticPath' + 's'] + export const getServerSideProps = ComponentInfo['getServerSideProp' + 's'] ${dynamicRouteMatcher} ${handleRewrites} @@ -241,9 +241,9 @@ const nextServerlessLoader: loader.Loader = function() { App, Document, buildManifest, - unstable_getStaticProps, - unstable_getServerSideProps, - unstable_getStaticPaths, + getStaticProps, + getServerSideProps, + getStaticPaths, reactLoadableManifest, canonicalBase: "${canonicalBase}", buildId: "${buildId}", @@ -275,7 +275,7 @@ const nextServerlessLoader: loader.Loader = function() { ${page === '/_error' ? `res.statusCode = 404` : ''} ${ pageIsDynamicRoute - ? `const params = fromExport && !unstable_getStaticProps && !unstable_getServerSideProps ? {} : dynamicRouteMatcher(parsedUrl.pathname) || {};` + ? `const params = fromExport && !getStaticProps && !getServerSideProps ? {} : dynamicRouteMatcher(parsedUrl.pathname) || {};` : `const params = {};` } ${ @@ -320,7 +320,7 @@ const nextServerlessLoader: loader.Loader = function() { const previewData = tryGetPreviewData(req, res, options.previewProps) const isPreviewMode = previewData !== false - let result = await renderToHTML(req, res, "${page}", Object.assign({}, unstable_getStaticProps ? {} : parsedUrl.query, nowParams ? nowParams : params, _params, isFallback ? { __nextFallback: 'true' } : {}), renderOpts) + let result = await renderToHTML(req, res, "${page}", Object.assign({}, getStaticProps ? {} : parsedUrl.query, nowParams ? nowParams : params, _params, isFallback ? { __nextFallback: 'true' } : {}), renderOpts) if (_nextData && !fromExport) { const payload = JSON.stringify(renderOpts.pageData) @@ -331,7 +331,7 @@ const nextServerlessLoader: loader.Loader = function() { 'Cache-Control', isPreviewMode ? \`private, no-cache, no-store, max-age=0, must-revalidate\` - : unstable_getServerSideProps + : getServerSideProps ? \`no-cache, no-store, must-revalidate\` : \`s-maxage=\${renderOpts.revalidate}, stale-while-revalidate\` ) @@ -350,9 +350,9 @@ const nextServerlessLoader: loader.Loader = function() { if (err.code === 'ENOENT') { res.statusCode = 404 const result = await renderToHTML(req, res, "/_error", parsedUrl.query, Object.assign({}, options, { - unstable_getStaticProps: undefined, - unstable_getStaticPaths: undefined, - unstable_getServerSideProps: undefined, + getStaticProps: undefined, + getStaticPaths: undefined, + getServerSideProps: undefined, Component: Error })) return result @@ -360,9 +360,9 @@ const nextServerlessLoader: loader.Loader = function() { console.error(err) res.statusCode = 500 const result = await renderToHTML(req, res, "/_error", parsedUrl.query, Object.assign({}, options, { - unstable_getStaticProps: undefined, - unstable_getStaticPaths: undefined, - unstable_getServerSideProps: undefined, + getStaticProps: undefined, + getStaticPaths: undefined, + getServerSideProps: undefined, Component: Error, err })) diff --git a/packages/next/export/worker.js b/packages/next/export/worker.js index ae5b4bd0b0d785d..86e820cd43271ca 100644 --- a/packages/next/export/worker.js +++ b/packages/next/export/worker.js @@ -119,9 +119,9 @@ export default async function({ let renderMethod = renderToHTML // eslint-disable-next-line camelcase - const renderedDuringBuild = unstable_getStaticProps => { + const renderedDuringBuild = getStaticProps => { // eslint-disable-next-line camelcase - return !buildExport && unstable_getStaticProps && !isDynamicRoute(path) + return !buildExport && getStaticProps && !isDynamicRoute(path) } if (serverless) { @@ -147,9 +147,9 @@ export default async function({ } else { // for non-dynamic SSG pages we should have already // prerendered the file - if (renderedDuringBuild(mod.unstable_getStaticProps)) return results + if (renderedDuringBuild(mod.getStaticProps)) return results - if (mod.unstable_getStaticProps && !htmlFilepath.endsWith('.html')) { + if (mod.getStaticProps && !htmlFilepath.endsWith('.html')) { // make sure it ends with .html if the name contains a dot htmlFilename += '.html' htmlFilepath += '.html' @@ -174,15 +174,12 @@ export default async function({ // for non-dynamic SSG pages we should have already // prerendered the file - if (renderedDuringBuild(components.unstable_getStaticProps)) { + if (renderedDuringBuild(components.getStaticProps)) { return results } // TODO: de-dupe the logic here between serverless and server mode - if ( - components.unstable_getStaticProps && - !htmlFilepath.endsWith('.html') - ) { + if (components.getStaticProps && !htmlFilepath.endsWith('.html')) { // make sure it ends with .html if the name contains a dot htmlFilepath += '.html' htmlFilename += '.html' diff --git a/packages/next/lib/constants.ts b/packages/next/lib/constants.ts index 1070627fad7a23a..ffad8c242c4a087 100644 --- a/packages/next/lib/constants.ts +++ b/packages/next/lib/constants.ts @@ -24,10 +24,10 @@ 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 SSG_GET_INITIAL_PROPS_CONFLICT = `You can not use getInitialProps with unstable_getStaticProps. To use SSG, please remove your getInitialProps` +export const SSG_GET_INITIAL_PROPS_CONFLICT = `You can not use getInitialProps with getStaticProps. To use SSG, please remove your getInitialProps` -export const SERVER_PROPS_GET_INIT_PROPS_CONFLICT = `You can not use getInitialProps with unstable_getServerSideProps. Please remove one or the other` +export const SERVER_PROPS_GET_INIT_PROPS_CONFLICT = `You can not use getInitialProps with getServerSideProps. Please remove one or the other` -export const SERVER_PROPS_SSG_CONFLICT = `You can not use unstable_getStaticProps with unstable_getServerSideProps. To use SSG, please remove your unstable_getServerSideProps` +export const SERVER_PROPS_SSG_CONFLICT = `You can not use getStaticProps with getServerSideProps. To use SSG, please remove your getServerSideProps` export const PAGES_404_GET_INITIAL_PROPS_ERROR = `\`pages/404\` can not have getInitialProps/getServerSideProps, https://err.sh/zeit/next.js/404-get-initial-props` diff --git a/packages/next/next-server/server/load-components.ts b/packages/next/next-server/server/load-components.ts index 856ea05a6efd8b2..9861ab0a2c2b401 100644 --- a/packages/next/next-server/server/load-components.ts +++ b/packages/next/next-server/server/load-components.ts @@ -1,5 +1,3 @@ -import { IncomingMessage, ServerResponse } from 'http' -import { ParsedUrlQuery } from 'querystring' import { BUILD_MANIFEST, CLIENT_STATIC_FILES_PATH, @@ -12,7 +10,13 @@ import { join } from 'path' import { requirePage } from './require' import { BuildManifest } from './get-page-files' import { AppType, DocumentType } from '../lib/utils' -import { PageConfig, NextPageContext } from 'next/types' +import { + PageConfig, + NextPageContext, + GetStaticPaths, + GetServerSideProps, + GetStaticProps, +} from '../../types/index' export function interopDefault(mod: any) { return mod.default || mod @@ -41,27 +45,6 @@ export type ManifestItem = { type ReactLoadableManifest = { [moduleId: string]: ManifestItem[] } -type Unstable_getStaticProps = (ctx: { - params: ParsedUrlQuery | undefined - preview?: boolean - previewData?: any -}) => Promise<{ - props: { [key: string]: any } - revalidate?: number | boolean -}> - -export type Unstable_getStaticPaths = () => Promise<{ - paths: Array - fallback: boolean -}> - -type Unstable_getServerSideProps = (context: { - params: ParsedUrlQuery | undefined - req: IncomingMessage - res: ServerResponse - query: ParsedUrlQuery -}) => Promise<{ [key: string]: any }> - export type LoadComponentsReturnType = { Component: React.ComponentType pageConfig?: PageConfig @@ -70,9 +53,9 @@ export type LoadComponentsReturnType = { Document: DocumentType DocumentMiddleware?: (ctx: NextPageContext) => void App: AppType - unstable_getStaticProps?: Unstable_getStaticProps - unstable_getStaticPaths?: Unstable_getStaticPaths - unstable_getServerSideProps?: Unstable_getServerSideProps + getStaticProps?: GetStaticProps + getStaticPaths?: GetStaticPaths + getServerSideProps?: GetServerSideProps } export async function loadComponents( @@ -83,24 +66,16 @@ export async function loadComponents( ): Promise { if (serverless) { const Component = await requirePage(pathname, distDir, serverless) - const { - unstable_getStaticProps, - unstable_getStaticPaths, - unstable_getServerSideProps, - } = Component + const { getStaticProps, getStaticPaths, getServerSideProps } = Component - addComponentPropsId( - Component, - unstable_getStaticProps, - unstable_getServerSideProps - ) + addComponentPropsId(Component, getStaticProps, getServerSideProps) return { Component, pageConfig: Component.config || {}, - unstable_getStaticProps, - unstable_getStaticPaths, - unstable_getServerSideProps, + getStaticProps, + getStaticPaths, + getServerSideProps, } as LoadComponentsReturnType } const documentPath = join( @@ -141,17 +116,9 @@ export async function loadComponents( interopDefault(AppMod), ]) - const { - unstable_getServerSideProps, - unstable_getStaticProps, - unstable_getStaticPaths, - } = ComponentMod + const { getServerSideProps, getStaticProps, getStaticPaths } = ComponentMod - addComponentPropsId( - Component, - unstable_getStaticProps, - unstable_getServerSideProps - ) + addComponentPropsId(Component, getStaticProps, getServerSideProps) return { App, @@ -161,8 +128,8 @@ export async function loadComponents( DocumentMiddleware, reactLoadableManifest, pageConfig: ComponentMod.config || {}, - unstable_getServerSideProps, - unstable_getStaticProps, - unstable_getStaticPaths, + getServerSideProps, + getStaticProps, + getStaticPaths, } } diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index 4e1ec838ac3eefa..1c41aaf6e20011d 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -810,7 +810,7 @@ export default class Server { return { components, query: { - ...(components.unstable_getStaticProps + ...(components.getStaticProps ? { _nextDataReq: query._nextDataReq } : query), ...(params || {}), @@ -888,9 +888,9 @@ export default class Server { const isLikeServerless = typeof components.Component === 'object' && typeof (components.Component as any).renderReqToHTML === 'function' - const isSSG = !!components.unstable_getStaticProps - const isServerProps = !!components.unstable_getServerSideProps - const hasStaticPaths = !!components.unstable_getStaticPaths + const isSSG = !!components.getStaticProps + const isServerProps = !!components.getServerSideProps + const hasStaticPaths = !!components.getStaticPaths // Toggle whether or not this is a Data request const isDataReq = query._nextDataReq diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index 287dd361ed7dc21..1b063dacdeb06a1 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -273,9 +273,9 @@ export async function renderToHTML( buildManifest, reactLoadableManifest, ErrorDebug, - unstable_getStaticProps, - unstable_getStaticPaths, - unstable_getServerSideProps, + getStaticProps, + getStaticPaths, + getServerSideProps, isDataReq, params, previewProps, @@ -311,7 +311,7 @@ export async function renderToHTML( const isFallback = !!query.__nextFallback delete query.__nextFallback - const isSpr = !!unstable_getStaticProps + const isSpr = !!getStaticProps const defaultAppGetInitialProps = App.getInitialProps === (App as any).origGetInitialProps @@ -323,7 +323,7 @@ export async function renderToHTML( !hasPageGetInitialProps && defaultAppGetInitialProps && !isSpr && - !unstable_getServerSideProps + !getServerSideProps if ( process.env.NODE_ENV !== 'production' && @@ -349,23 +349,23 @@ export async function renderToHTML( throw new Error(SSG_GET_INITIAL_PROPS_CONFLICT + ` ${pathname}`) } - if (hasPageGetInitialProps && unstable_getServerSideProps) { + if (hasPageGetInitialProps && getServerSideProps) { throw new Error(SERVER_PROPS_GET_INIT_PROPS_CONFLICT + ` ${pathname}`) } - if (unstable_getServerSideProps && isSpr) { + if (getServerSideProps && isSpr) { throw new Error(SERVER_PROPS_SSG_CONFLICT + ` ${pathname}`) } - if (!!unstable_getStaticPaths && !isSpr) { + if (!!getStaticPaths && !isSpr) { throw new Error( - `unstable_getStaticPaths was added without a unstable_getStaticProps in ${pathname}. Without unstable_getStaticProps, unstable_getStaticPaths does nothing` + `getStaticPaths was added without a getStaticProps in ${pathname}. Without getStaticProps, getStaticPaths does nothing` ) } - if (isSpr && pageIsDynamic && !unstable_getStaticPaths) { + if (isSpr && pageIsDynamic && !getStaticPaths) { throw new Error( - `unstable_getStaticPaths is required for dynamic SSG pages and is missing for '${pathname}'.` + + `getStaticPaths is required for dynamic SSG pages and is missing for '${pathname}'.` + `\nRead more: https://err.sh/next.js/invalid-getstaticpaths-value` ) } @@ -467,7 +467,7 @@ export async function renderToHTML( // instantly. There's no need to pass this data down from a previous // invoke, where we'd have to consider server & serverless. const previewData = tryGetPreviewData(req, res, previewProps) - const data = await unstable_getStaticProps!({ + const data = await getStaticProps!({ ...(pageIsDynamic ? { params: query as ParsedUrlQuery, @@ -529,8 +529,8 @@ export async function renderToHTML( console.error(err) } - if (unstable_getServerSideProps && !isFallback) { - const data = await unstable_getServerSideProps({ + if (getServerSideProps && !isFallback) { + const data = await getServerSideProps({ params, query, req, @@ -549,7 +549,7 @@ export async function renderToHTML( if ( !isSpr && // we only show this warning for legacy pages - !unstable_getServerSideProps && + !getServerSideProps && process.env.NODE_ENV !== 'production' && Object.keys(props?.pageProps || {}).includes('url') ) { diff --git a/packages/next/server/static-paths-worker.ts b/packages/next/server/static-paths-worker.ts index 51d0047a5aadbf8..ba09801f0d6e08d 100644 --- a/packages/next/server/static-paths-worker.ts +++ b/packages/next/server/static-paths-worker.ts @@ -28,13 +28,13 @@ export async function loadStaticPaths( serverless ) - if (!components.unstable_getStaticPaths) { + if (!components.getStaticPaths) { // we shouldn't get to this point since the worker should // only be called for SSG pages with getStaticPaths throw new Error( - `Invariant: failed to load page with unstable_getStaticPaths for ${pathname}` + `Invariant: failed to load page with getStaticPaths for ${pathname}` ) } - return buildStaticPaths(pathname, components.unstable_getStaticPaths) + return buildStaticPaths(pathname, components.getStaticPaths) } diff --git a/packages/next/types/index.d.ts b/packages/next/types/index.d.ts index f0a1bfc0897a643..986e551fec9ed02 100644 --- a/packages/next/types/index.d.ts +++ b/packages/next/types/index.d.ts @@ -3,6 +3,8 @@ /// import React from 'react' +import { ParsedUrlQuery } from 'querystring' +import { IncomingMessage, ServerResponse } from 'http' import { NextPageContext, @@ -62,4 +64,25 @@ export { NextApiHandler, } +export type GetStaticProps = (ctx: { + params: ParsedUrlQuery | undefined + preview?: boolean + previewData?: any +}) => Promise<{ + props: { [key: string]: any } + revalidate?: number | boolean +}> + +export type GetStaticPaths = () => Promise<{ + paths: Array + fallback: boolean +}> + +export type GetServerSideProps = (context: { + params: ParsedUrlQuery | undefined + req: IncomingMessage + res: ServerResponse + query: ParsedUrlQuery +}) => Promise<{ [key: string]: any }> + export default next diff --git a/test/integration/catches-missing-getStaticProps/pages/[slug].js b/test/integration/catches-missing-getStaticProps/pages/[slug].js index 7a87525aacc99ce..df6775cabe94a51 100644 --- a/test/integration/catches-missing-getStaticProps/pages/[slug].js +++ b/test/integration/catches-missing-getStaticProps/pages/[slug].js @@ -1,4 +1,4 @@ -export async function unstable_getStaticPaths() { +export async function getStaticPaths() { return { paths: ['/hello', '/world'], fallback: true } } diff --git a/test/integration/catches-missing-getStaticProps/test/index.test.js b/test/integration/catches-missing-getStaticProps/test/index.test.js index af831925289130a..fec687871b6a9f9 100644 --- a/test/integration/catches-missing-getStaticProps/test/index.test.js +++ b/test/integration/catches-missing-getStaticProps/test/index.test.js @@ -14,7 +14,7 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1 const appDir = join(__dirname, '../') const nextConfig = join(appDir, 'next.config.js') -const errorRegex = /unstable_getStaticPaths was added without a unstable_getStaticProps in/ +const errorRegex = /getStaticPaths was added without a getStaticProps in/ describe('Catches Missing getStaticProps', () => { afterAll(() => fs.remove(nextConfig)) diff --git a/test/integration/dynamic-routing/pages/p1/p2/all-ssg/[...rest].js b/test/integration/dynamic-routing/pages/p1/p2/all-ssg/[...rest].js index 85ace5dad12e004..2feba203a2bfde9 100644 --- a/test/integration/dynamic-routing/pages/p1/p2/all-ssg/[...rest].js +++ b/test/integration/dynamic-routing/pages/p1/p2/all-ssg/[...rest].js @@ -2,11 +2,11 @@ function All({ params }) { return

{JSON.stringify(params)}
} -export function unstable_getStaticProps({ params }) { +export function getStaticProps({ params }) { return { props: { params } } } -export function unstable_getStaticPaths() { +export function getStaticPaths() { return { paths: [], fallback: true, diff --git a/test/integration/dynamic-routing/pages/p1/p2/nested-all-ssg/[...rest]/index.js b/test/integration/dynamic-routing/pages/p1/p2/nested-all-ssg/[...rest]/index.js index 03418a2de5c7d91..39b9da58c20a9ee 100644 --- a/test/integration/dynamic-routing/pages/p1/p2/nested-all-ssg/[...rest]/index.js +++ b/test/integration/dynamic-routing/pages/p1/p2/nested-all-ssg/[...rest]/index.js @@ -8,11 +8,11 @@ function All({ params }) { ) } -export function unstable_getStaticProps({ params }) { +export function getStaticProps({ params }) { return { props: { params } } } -export function unstable_getStaticPaths() { +export function getStaticPaths() { return { paths: [], fallback: true, diff --git a/test/integration/dynamic-routing/pages/p1/p2/predefined-ssg/[...rest].js b/test/integration/dynamic-routing/pages/p1/p2/predefined-ssg/[...rest].js index 55cf9c8a29e8613..0d1824ccaaf0164 100644 --- a/test/integration/dynamic-routing/pages/p1/p2/predefined-ssg/[...rest].js +++ b/test/integration/dynamic-routing/pages/p1/p2/predefined-ssg/[...rest].js @@ -2,11 +2,11 @@ function All({ params }) { return
{JSON.stringify(params)}
} -export function unstable_getStaticProps({ params }) { +export function getStaticProps({ params }) { return { props: { params } } } -export function unstable_getStaticPaths() { +export function getStaticPaths() { return { paths: [ `/p1/p2/predefined-ssg/one-level`, diff --git a/test/integration/getserversideprops/pages/another/index.js b/test/integration/getserversideprops/pages/another/index.js index de46cda8bd03413..5270b73f050ca14 100644 --- a/test/integration/getserversideprops/pages/another/index.js +++ b/test/integration/getserversideprops/pages/another/index.js @@ -3,7 +3,7 @@ import fs from 'fs' import findUp from 'find-up' // eslint-disable-next-line camelcase -export async function unstable_getServerSideProps() { +export async function getServerSideProps() { const text = fs .readFileSync( findUp.sync('world.txt', { diff --git a/test/integration/getserversideprops/pages/blog/[post]/[comment].js b/test/integration/getserversideprops/pages/blog/[post]/[comment].js index a5a42247ecb12a9..f1449358ae4f745 100644 --- a/test/integration/getserversideprops/pages/blog/[post]/[comment].js +++ b/test/integration/getserversideprops/pages/blog/[post]/[comment].js @@ -2,7 +2,7 @@ import React from 'react' import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getServerSideProps({ query }) { +export async function getServerSideProps({ query }) { return { props: { post: query.post, diff --git a/test/integration/getserversideprops/pages/blog/[post]/index.js b/test/integration/getserversideprops/pages/blog/[post]/index.js index ba9c5978ea8891c..c507475cf752370 100644 --- a/test/integration/getserversideprops/pages/blog/[post]/index.js +++ b/test/integration/getserversideprops/pages/blog/[post]/index.js @@ -3,7 +3,7 @@ import Link from 'next/link' import { useRouter } from 'next/router' // eslint-disable-next-line camelcase -export async function unstable_getServerSideProps({ params }) { +export async function getServerSideProps({ params }) { if (params.post === 'post-10') { await new Promise(resolve => { setTimeout(() => resolve(), 1000) diff --git a/test/integration/getserversideprops/pages/blog/index.js b/test/integration/getserversideprops/pages/blog/index.js index be51f782e42d6ca..627f651550edc1d 100644 --- a/test/integration/getserversideprops/pages/blog/index.js +++ b/test/integration/getserversideprops/pages/blog/index.js @@ -2,7 +2,7 @@ import React from 'react' import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getServerSideProps() { +export async function getServerSideProps() { return { props: { slugs: ['post-1', 'post-2'], diff --git a/test/integration/getserversideprops/pages/catchall/[...path].js b/test/integration/getserversideprops/pages/catchall/[...path].js index 45efb3e6c85f68f..de807caa3cdfbc1 100644 --- a/test/integration/getserversideprops/pages/catchall/[...path].js +++ b/test/integration/getserversideprops/pages/catchall/[...path].js @@ -3,7 +3,7 @@ import Link from 'next/link' import { useRouter } from 'next/router' // eslint-disable-next-line camelcase -export async function unstable_getServerSideProps({ params }) { +export async function getServerSideProps({ params }) { return { props: { world: 'world', diff --git a/test/integration/getserversideprops/pages/default-revalidate.js b/test/integration/getserversideprops/pages/default-revalidate.js index 582ef524ec1a408..57e55a20508b3cd 100644 --- a/test/integration/getserversideprops/pages/default-revalidate.js +++ b/test/integration/getserversideprops/pages/default-revalidate.js @@ -1,7 +1,7 @@ import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getServerSideProps() { +export async function getServerSideProps() { return { props: { world: 'world', diff --git a/test/integration/getserversideprops/pages/index.js b/test/integration/getserversideprops/pages/index.js index fc84e9848d8d6f6..becc0df1c5c0906 100644 --- a/test/integration/getserversideprops/pages/index.js +++ b/test/integration/getserversideprops/pages/index.js @@ -1,7 +1,7 @@ import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getServerSideProps() { +export async function getServerSideProps() { return { props: { world: 'world', diff --git a/test/integration/getserversideprops/pages/invalid-keys.js b/test/integration/getserversideprops/pages/invalid-keys.js index 4184eb8bcfabca8..3cf83a33bde52c0 100644 --- a/test/integration/getserversideprops/pages/invalid-keys.js +++ b/test/integration/getserversideprops/pages/invalid-keys.js @@ -3,7 +3,7 @@ import Link from 'next/link' import { useRouter } from 'next/router' // eslint-disable-next-line camelcase -export async function unstable_getServerSideProps({ params, query }) { +export async function getServerSideProps({ params, query }) { return { world: 'world', query: query || {}, diff --git a/test/integration/getserversideprops/pages/something.js b/test/integration/getserversideprops/pages/something.js index 361bbb44b5097ce..5b32921c5bbfceb 100644 --- a/test/integration/getserversideprops/pages/something.js +++ b/test/integration/getserversideprops/pages/something.js @@ -3,7 +3,7 @@ import Link from 'next/link' import { useRouter } from 'next/router' // eslint-disable-next-line camelcase -export async function unstable_getServerSideProps({ params, query }) { +export async function getServerSideProps({ params, query }) { return { props: { world: 'world', diff --git a/test/integration/getserversideprops/pages/user/[user]/profile.js b/test/integration/getserversideprops/pages/user/[user]/profile.js index c14992d56d4dc22..9a6b30f37b488af 100644 --- a/test/integration/getserversideprops/pages/user/[user]/profile.js +++ b/test/integration/getserversideprops/pages/user/[user]/profile.js @@ -2,7 +2,7 @@ import React from 'react' import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getServerSideProps({ query }) { +export async function getServerSideProps({ query }) { return { props: { user: query.user, diff --git a/test/integration/getserversideprops/test/index.test.js b/test/integration/getserversideprops/test/index.test.js index 963215c85850225..f2387061eae7f2d 100644 --- a/test/integration/getserversideprops/test/index.test.js +++ b/test/integration/getserversideprops/test/index.test.js @@ -337,7 +337,7 @@ const runTests = (dev = false) => { await fs.writeFile( urlPropPage, ` - export async function unstable_getServerSideProps() { + export async function getServerSideProps() { return { props: { url: 'something' @@ -396,7 +396,7 @@ const runTests = (dev = false) => { } } -describe('unstable_getServerSideProps', () => { +describe('getServerSideProps', () => { describe('dev mode', () => { beforeAll(async () => { stderr = '' diff --git a/test/integration/mixed-ssg-serverprops-error/pages/index.js b/test/integration/mixed-ssg-serverprops-error/pages/index.js index 7bf4d452e293240..46967c38fc4d20d 100644 --- a/test/integration/mixed-ssg-serverprops-error/pages/index.js +++ b/test/integration/mixed-ssg-serverprops-error/pages/index.js @@ -1,10 +1,10 @@ -export const unstable_getStaticProps = async () => { +export const getStaticProps = async () => { return { props: { world: 'world' }, } } -export const unstable_getServerSideProps = async () => { +export const getServerSideProps = async () => { return { props: { world: 'world' }, } diff --git a/test/integration/mixed-ssg-serverprops-error/pages/index.js.alt b/test/integration/mixed-ssg-serverprops-error/pages/index.js.alt index 9d6334f42ef07ad..1e3ee159ba571ed 100644 --- a/test/integration/mixed-ssg-serverprops-error/pages/index.js.alt +++ b/test/integration/mixed-ssg-serverprops-error/pages/index.js.alt @@ -1,10 +1,10 @@ -export const unstable_getStaticPaths = async () => { +export const getStaticPaths = async () => { return { props: { world: 'world' }, fallback: true } } -export const unstable_getServerSideProps = async () => { +export const getServerSideProps = async () => { return { props: { world: 'world' } } diff --git a/test/integration/prerender-invalid-catchall-params/pages/[...slug].js b/test/integration/prerender-invalid-catchall-params/pages/[...slug].js index f8413c5dc5ff027..f0eef4d55972d1a 100644 --- a/test/integration/prerender-invalid-catchall-params/pages/[...slug].js +++ b/test/integration/prerender-invalid-catchall-params/pages/[...slug].js @@ -1,12 +1,12 @@ import React from 'react' // eslint-disable-next-line camelcase -export async function unstable_getStaticPaths() { +export async function getStaticPaths() { return { paths: [{ params: { slug: 'hello' } }], fallback: true } } // eslint-disable-next-line camelcase -export async function unstable_getStaticProps({ params }) { +export async function getStaticProps({ params }) { return { props: { post: params.post, diff --git a/test/integration/prerender-invalid-catchall-params/test/index.test.js b/test/integration/prerender-invalid-catchall-params/test/index.test.js index d2b47c0c8b0978e..ac1f966335a3cd6 100644 --- a/test/integration/prerender-invalid-catchall-params/test/index.test.js +++ b/test/integration/prerender-invalid-catchall-params/test/index.test.js @@ -11,7 +11,7 @@ describe('Invalid Prerender Catchall Params', () => { const out = await nextBuild(appDir, [], { stderr: true }) expect(out.stderr).toMatch(`Build error occurred`) expect(out.stderr).toMatch( - 'A required parameter (slug) was not provided as an array in unstable_getStaticPaths for /[...slug]' + 'A required parameter (slug) was not provided as an array in getStaticPaths for /[...slug]' ) }) }) diff --git a/test/integration/prerender-invalid-paths/pages/[foo]/[post].js b/test/integration/prerender-invalid-paths/pages/[foo]/[post].js index ef80ed41e8761be..4dc8857278599c9 100644 --- a/test/integration/prerender-invalid-paths/pages/[foo]/[post].js +++ b/test/integration/prerender-invalid-paths/pages/[foo]/[post].js @@ -1,12 +1,12 @@ import React from 'react' // eslint-disable-next-line camelcase -export async function unstable_getStaticPaths() { +export async function getStaticPaths() { return { paths: [{ foo: 'bad', baz: 'herro' }], fallback: true } } // eslint-disable-next-line camelcase -export async function unstable_getStaticProps({ params }) { +export async function getStaticProps({ params }) { return { props: { post: params.post, diff --git a/test/integration/prerender-legacy/pages/blog/[post].js b/test/integration/prerender-legacy/pages/blog/[post].js index aeaa7c4a371c056..c7aa49e37b24399 100644 --- a/test/integration/prerender-legacy/pages/blog/[post].js +++ b/test/integration/prerender-legacy/pages/blog/[post].js @@ -6,7 +6,7 @@ export async function unstable_getStaticParams() { } // eslint-disable-next-line camelcase -export async function unstable_getStaticProps({ params }) { +export async function getStaticProps({ params }) { return { props: { post: params.post, diff --git a/test/integration/prerender-legacy/test/index.test.js b/test/integration/prerender-legacy/test/index.test.js index 1cec1d583466737..2f122c5b365fffc 100644 --- a/test/integration/prerender-legacy/test/index.test.js +++ b/test/integration/prerender-legacy/test/index.test.js @@ -14,7 +14,7 @@ describe('Legacy Prerender', () => { const out = await nextBuild(appDir, [], { stderr: true }) expect(out.stderr).toMatch(`Build error occurred`) expect(out.stderr).toMatch( - 'unstable_getStaticParams was replaced with unstable_getStaticPaths. Please update your code.' + 'unstable_getStaticParams was replaced with getStaticPaths. Please update your code.' ) }) @@ -27,7 +27,7 @@ describe('Legacy Prerender', () => { await fs.remove(nextConfig) expect(out.stderr).toMatch(`Build error occurred`) expect(out.stderr).toMatch( - 'unstable_getStaticParams was replaced with unstable_getStaticPaths. Please update your code.' + 'unstable_getStaticParams was replaced with getStaticPaths. Please update your code.' ) }) }) diff --git a/test/integration/prerender-preview/pages/index.js b/test/integration/prerender-preview/pages/index.js index a9da9aa293579a9..8fa4d03b6b519a8 100644 --- a/test/integration/prerender-preview/pages/index.js +++ b/test/integration/prerender-preview/pages/index.js @@ -1,4 +1,4 @@ -export function unstable_getStaticProps({ preview, previewData }) { +export function getStaticProps({ preview, previewData }) { return { props: { hasProps: true, preview, previewData } } } diff --git a/test/integration/prerender/pages/another/index.js b/test/integration/prerender/pages/another/index.js index 19a7709d3dbe195..f8eb26a2e003e68 100644 --- a/test/integration/prerender/pages/another/index.js +++ b/test/integration/prerender/pages/another/index.js @@ -3,7 +3,7 @@ import fs from 'fs' import findUp from 'find-up' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps() { +export async function getStaticProps() { const text = fs .readFileSync( findUp.sync('world.txt', { diff --git a/test/integration/prerender/pages/blog/[post]/[comment].js b/test/integration/prerender/pages/blog/[post]/[comment].js index 0c1174ffbb7725c..b2d89f3cba278b6 100644 --- a/test/integration/prerender/pages/blog/[post]/[comment].js +++ b/test/integration/prerender/pages/blog/[post]/[comment].js @@ -2,7 +2,7 @@ import React from 'react' import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getStaticPaths() { +export async function getStaticPaths() { return { paths: [ '/blog/post-1/comment-1', @@ -13,7 +13,7 @@ export async function unstable_getStaticPaths() { } // eslint-disable-next-line camelcase -export async function unstable_getStaticProps({ params }) { +export async function getStaticProps({ params }) { return { props: { post: params.post, diff --git a/test/integration/prerender/pages/blog/[post]/index.js b/test/integration/prerender/pages/blog/[post]/index.js index 2b63657aae2d3f1..1f33676647a8f4f 100644 --- a/test/integration/prerender/pages/blog/[post]/index.js +++ b/test/integration/prerender/pages/blog/[post]/index.js @@ -3,7 +3,7 @@ import Link from 'next/link' import { useRouter } from 'next/router' // eslint-disable-next-line camelcase -export async function unstable_getStaticPaths() { +export async function getStaticPaths() { return { paths: [ '/blog/post-1', @@ -20,7 +20,7 @@ export async function unstable_getStaticPaths() { let counter = 0 // eslint-disable-next-line camelcase -export async function unstable_getStaticProps({ params }) { +export async function getStaticProps({ params }) { if (params.post === 'post-10') { await new Promise(resolve => { setTimeout(() => resolve(), 1000) diff --git a/test/integration/prerender/pages/blog/index.js b/test/integration/prerender/pages/blog/index.js index 6ec9f06b567f334..c4a0b940819b03d 100644 --- a/test/integration/prerender/pages/blog/index.js +++ b/test/integration/prerender/pages/blog/index.js @@ -2,7 +2,7 @@ import React from 'react' import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps() { +export async function getStaticProps() { return { props: { slugs: ['post-1', 'post-2'], diff --git a/test/integration/prerender/pages/catchall-explicit/[...slug].js b/test/integration/prerender/pages/catchall-explicit/[...slug].js index e992e8c79bbd288..6b3bfbccacff39d 100644 --- a/test/integration/prerender/pages/catchall-explicit/[...slug].js +++ b/test/integration/prerender/pages/catchall-explicit/[...slug].js @@ -1,4 +1,4 @@ -export async function unstable_getStaticProps({ params: { slug } }) { +export async function getStaticProps({ params: { slug } }) { if (slug[0] === 'delayby3s') { await new Promise(resolve => setTimeout(resolve, 3000)) } @@ -11,7 +11,7 @@ export async function unstable_getStaticProps({ params: { slug } }) { } } -export async function unstable_getStaticPaths() { +export async function getStaticPaths() { return { paths: [ { params: { slug: ['first'] } }, diff --git a/test/integration/prerender/pages/catchall/[...slug].js b/test/integration/prerender/pages/catchall/[...slug].js index 5a4834499e3ee23..0ce61b73d3a5e06 100644 --- a/test/integration/prerender/pages/catchall/[...slug].js +++ b/test/integration/prerender/pages/catchall/[...slug].js @@ -1,6 +1,6 @@ import { useRouter } from 'next/router' -export async function unstable_getStaticProps({ params: { slug } }) { +export async function getStaticProps({ params: { slug } }) { if (slug[0] === 'delayby3s') { await new Promise(resolve => setTimeout(resolve, 3000)) } @@ -13,7 +13,7 @@ export async function unstable_getStaticProps({ params: { slug } }) { } } -export async function unstable_getStaticPaths() { +export async function getStaticPaths() { return { paths: [ { params: { slug: ['first'] } }, diff --git a/test/integration/prerender/pages/default-revalidate.js b/test/integration/prerender/pages/default-revalidate.js index 582a0c5e7c851a8..e8125f3ca32a8e1 100644 --- a/test/integration/prerender/pages/default-revalidate.js +++ b/test/integration/prerender/pages/default-revalidate.js @@ -1,7 +1,7 @@ import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps() { +export async function getStaticProps() { return { props: { world: 'world', diff --git a/test/integration/prerender/pages/index.js b/test/integration/prerender/pages/index.js index 1c9bf5259b50534..44960c35d3e76f1 100644 --- a/test/integration/prerender/pages/index.js +++ b/test/integration/prerender/pages/index.js @@ -1,7 +1,7 @@ import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps() { +export async function getStaticProps() { // throw new Error('oops from getStaticProps') return { props: { world: 'world', time: new Date().getTime() }, diff --git a/test/integration/prerender/pages/something.js b/test/integration/prerender/pages/something.js index fde8a1730767a71..d9e341e75570c67 100644 --- a/test/integration/prerender/pages/something.js +++ b/test/integration/prerender/pages/something.js @@ -3,7 +3,7 @@ import Link from 'next/link' import { useRouter } from 'next/router' // eslint-disable-next-line camelcase -export async function unstable_getStaticProps({ params }) { +export async function getStaticProps({ params }) { return { props: { world: 'world', diff --git a/test/integration/prerender/pages/user/[user]/profile.js b/test/integration/prerender/pages/user/[user]/profile.js index a80e175b6d20d02..3c95d23cd97b588 100644 --- a/test/integration/prerender/pages/user/[user]/profile.js +++ b/test/integration/prerender/pages/user/[user]/profile.js @@ -2,12 +2,12 @@ import React from 'react' import Link from 'next/link' // eslint-disable-next-line camelcase -export async function unstable_getStaticPaths() { +export async function getStaticPaths() { return { paths: [], fallback: true } } // eslint-disable-next-line camelcase -export async function unstable_getStaticProps({ params }) { +export async function getStaticProps({ params }) { return { props: { user: params.user, diff --git a/test/integration/prerender/test/index.test.js b/test/integration/prerender/test/index.test.js index 823437388dc27bb..a71b87e0773a2f7 100644 --- a/test/integration/prerender/test/index.test.js +++ b/test/integration/prerender/test/index.test.js @@ -496,7 +496,7 @@ const runTests = (dev = false, looseMode = false) => { await fs.writeFile( urlPropPage, ` - export async function unstable_getStaticProps() { + export async function getStaticProps() { return { props: { url: 'something' @@ -603,7 +603,7 @@ const runTests = (dev = false, looseMode = false) => { await fs.writeFile( curPage, ` - export async function unstable_getStaticProps() { + export async function getStaticProps() { return { props: { hello: 'world' @@ -617,7 +617,7 @@ const runTests = (dev = false, looseMode = false) => { try { const html = await renderViaHTTP(appPort, '/temp/hello') expect(html).toMatch( - /unstable_getStaticPaths is required for dynamic SSG pages and is missing for/ + /getStaticPaths is required for dynamic SSG pages and is missing for/ ) } finally { await fs.remove(curPage) @@ -630,12 +630,12 @@ const runTests = (dev = false, looseMode = false) => { await fs.writeFile( curPage, ` - export async function unstable_getStaticPaths() { + export async function getStaticPaths() { return { paths: [] } } - export async function unstable_getStaticProps() { + export async function getStaticProps() { return { props: { hello: 'world' @@ -1013,7 +1013,7 @@ describe('SSG Prerender', () => { await fs.writeFile( brokenPage, ` - export async function unstable_getStaticProps() { + export async function getStaticProps() { return { hello: 'world' } @@ -1028,7 +1028,7 @@ describe('SSG Prerender', () => { 'Additional keys were returned from `getStaticProps`' ) expect(stderr).not.toContain( - 'You can not use getInitialProps with unstable_getStaticProps' + 'You can not use getInitialProps with getStaticProps' ) }) }) diff --git a/test/unit/babel-plugin-next-ssg-transform.test.js b/test/unit/babel-plugin-next-ssg-transform.test.js index 7844ed417ecacfb..bb374923c6f231c 100644 --- a/test/unit/babel-plugin-next-ssg-transform.test.js +++ b/test/unit/babel-plugin-next-ssg-transform.test.js @@ -32,8 +32,8 @@ describe('babel plugin (next-ssg-transform)', () => { describe('getStaticProps support', () => { it('should remove separate named export specifiers', () => { const output = babel(trim` - export { unstable_getStaticPaths } from '.' - export { a as unstable_getStaticProps } from '.' + export { getStaticPaths } from '.' + export { a as getStaticProps } from '.' export default function Test() { return
@@ -46,7 +46,7 @@ describe('babel plugin (next-ssg-transform)', () => { it('should remove combined named export specifiers', () => { const output = babel(trim` - export { unstable_getStaticPaths, a as unstable_getStaticProps } from '.' + export { getStaticPaths, a as getStaticProps } from '.' export default function Test() { return
@@ -59,7 +59,7 @@ describe('babel plugin (next-ssg-transform)', () => { it('should retain extra named export specifiers', () => { const output = babel(trim` - export { unstable_getStaticPaths, a as unstable_getStaticProps, foo, bar as baz } from '.' + export { getStaticPaths, a as getStaticProps, foo, bar as baz } from '.' export default function Test() { return
@@ -72,11 +72,11 @@ describe('babel plugin (next-ssg-transform)', () => { it('should remove named export function declarations', () => { const output = babel(trim` - export function unstable_getStaticPaths() { + export function getStaticPaths() { return [] } - export function unstable_getStaticProps() { + export function getStaticProps() { return { props: {} } } @@ -92,11 +92,11 @@ describe('babel plugin (next-ssg-transform)', () => { it('should remove named export function declarations (async)', () => { const output = babel(trim` - export async function unstable_getStaticPaths() { + export async function getStaticPaths() { return [] } - export async function unstable_getStaticProps() { + export async function getStaticProps() { return { props: {} } } @@ -112,7 +112,7 @@ describe('babel plugin (next-ssg-transform)', () => { it('should not remove extra named export function declarations', () => { const output = babel(trim` - export function unstable_getStaticProps() { + export function getStaticProps() { return { props: {} } } @@ -130,11 +130,11 @@ describe('babel plugin (next-ssg-transform)', () => { it('should remove named export variable declarations', () => { const output = babel(trim` - export const unstable_getStaticPaths = () => { + export const getStaticPaths = () => { return [] } - export const unstable_getStaticProps = function() { + export const getStaticProps = function() { return { props: {} } } @@ -150,11 +150,11 @@ describe('babel plugin (next-ssg-transform)', () => { it('should remove named export variable declarations (async)', () => { const output = babel(trim` - export const unstable_getStaticPaths = async () => { + export const getStaticPaths = async () => { return [] } - export const unstable_getStaticProps = async function() { + export const getStaticProps = async function() { return { props: {} } } @@ -170,11 +170,11 @@ describe('babel plugin (next-ssg-transform)', () => { it('should not remove extra named export variable declarations', () => { const output = babel(trim` - export const unstable_getStaticPaths = () => { + export const getStaticPaths = () => { return [] }, foo = 2 - export const unstable_getStaticProps = function() { + export const getStaticProps = function() { return { props: {} } } @@ -190,11 +190,11 @@ describe('babel plugin (next-ssg-transform)', () => { it('should remove re-exported variable declarations', () => { const output = babel(trim` - const unstable_getStaticPaths = () => { + const getStaticPaths = () => { return [] } - export { unstable_getStaticPaths } + export { getStaticPaths } export default function Test() { return
@@ -208,11 +208,11 @@ describe('babel plugin (next-ssg-transform)', () => { it('should remove re-exported variable declarations (safe)', () => { const output = babel(trim` - const unstable_getStaticPaths = () => { + const getStaticPaths = () => { return [] }, a = 2 - export { unstable_getStaticPaths } + export { getStaticPaths } export default function Test() { return
@@ -226,11 +226,11 @@ describe('babel plugin (next-ssg-transform)', () => { it('should remove re-exported function declarations', () => { const output = babel(trim` - function unstable_getStaticPaths() { + function getStaticPaths() { return [] } - export { unstable_getStaticPaths } + export { getStaticPaths } export default function Test() { return
@@ -244,11 +244,11 @@ describe('babel plugin (next-ssg-transform)', () => { it('should not crash for class declarations', () => { const output = babel(trim` - function unstable_getStaticPaths() { + function getStaticPaths() { return [] } - export { unstable_getStaticPaths } + export { getStaticPaths } export class MyClass {} @@ -288,7 +288,7 @@ describe('babel plugin (next-ssg-transform)', () => { const b2 = function apples() {}; const bla = () => {inception1}; - function unstable_getStaticProps() { + function getStaticProps() { abc(); drop_me; b; @@ -297,7 +297,7 @@ describe('babel plugin (next-ssg-transform)', () => { return { props: {var1} } } - export { unstable_getStaticProps } + export { getStaticProps } export default function Test() { return
@@ -324,7 +324,7 @@ describe('babel plugin (next-ssg-transform)', () => { return { bug }; } - export { unstable_getStaticProps } from 'a' + export { getStaticProps } from 'a' `) expect(output).toMatchInlineSnapshot( @@ -334,7 +334,7 @@ describe('babel plugin (next-ssg-transform)', () => { it('should support class exports', () => { const output = babel(trim` - export function unstable_getStaticProps() { + export function getStaticProps() { return { props: {} } } @@ -352,7 +352,7 @@ describe('babel plugin (next-ssg-transform)', () => { it('should support class exports 2', () => { const output = babel(trim` - export function unstable_getStaticProps() { + export function getStaticProps() { return { props: {} } } @@ -372,7 +372,7 @@ describe('babel plugin (next-ssg-transform)', () => { it('should support export { _ as default }', () => { const output = babel(trim` - export function unstable_getStaticProps() { + export function getStaticProps() { return { props: {} } } @@ -390,7 +390,7 @@ describe('babel plugin (next-ssg-transform)', () => { it('should support export { _ as default } with other specifiers', () => { const output = babel(trim` - export function unstable_getStaticProps() { + export function getStaticProps() { return { props: {} } } @@ -410,7 +410,7 @@ describe('babel plugin (next-ssg-transform)', () => { it('should support export { _ as default } with a class', () => { const output = babel(trim` - export function unstable_getStaticProps() { + export function getStaticProps() { return { props: {} } } diff --git a/test/unit/next-babel-loader.test.js b/test/unit/next-babel-loader.test.js index 92220a4fab04d84..5bf4470950835c2 100644 --- a/test/unit/next-babel-loader.test.js +++ b/test/unit/next-babel-loader.test.js @@ -255,7 +255,7 @@ describe('next-babel-loader', () => { `import{c,d}from"e";` + `import{e as ee,f as ff}from"f";` + `` + - `export function unstable_getStaticProps() {foo;bar;baz;cats;baz2;ff; return { props: {} } }`, + `export function getStaticProps() {foo;bar;baz;cats;baz2;ff; return { props: {} } }`, { resourcePath: pageFile } ) expect(code).toMatchInlineSnapshot( @@ -276,12 +276,12 @@ describe('next-babel-loader', () => { `import{c,d}from"e";` + `import{e as ee,f as ff}from"f";` + `` + - `export function unstable_getStaticProps() {foo();baz2();ff();ooo(); return { props: {} }}` + + `export function getStaticProps() {foo();baz2();ff();ooo(); return { props: {} }}` + `export default function () { return bar(); }`, { resourcePath: pageFile, isServer: true } ) expect(code).toMatchInlineSnapshot( - `"import\\"core-js\\";import{foo,bar}from\\"a\\";import baz from\\"b\\";import ooo from\\"ooo\\";import*as React from\\"react\\";import baz2,{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee,f as ff}from\\"f\\";export function unstable_getStaticProps(){foo();baz2();ff();ooo();return{props:{}};}export default function(){return bar();}"` + `"import\\"core-js\\";import{foo,bar}from\\"a\\";import baz from\\"b\\";import ooo from\\"ooo\\";import*as React from\\"react\\";import baz2,{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee,f as ff}from\\"f\\";export function getStaticProps(){foo();baz2();ff();ooo();return{props:{}};}export default function(){return bar();}"` ) }) @@ -298,7 +298,7 @@ describe('next-babel-loader', () => { `import{c,d}from"e";` + `import{e as ee,f as ff}from"f";` + `` + - `export function unstable_getStaticProps() {foo();baz2();ff();ooo();cats; return { props: {} }}` + + `export function getStaticProps() {foo();baz2();ff();ooo();cats; return { props: {} }}` + `export default function () { return cats + bar(); }`, { resourcePath: pageFile, isServer: false } ) @@ -320,7 +320,7 @@ describe('next-babel-loader', () => { `import{c,d}from"e";` + `import{e as ee,f as ff}from"f";` + `` + - `export function unstable_getStaticProps() {foo();baz2();ff();ooo(); return { props: {} }}` + + `export function getStaticProps() {foo();baz2();ff();ooo(); return { props: {} }}` + `export default function () { return
{cats + bar()}
}`, { resourcePath: pageFile, isServer: false } ) From c3ed0a798477e051d47a63ec52b7990b756467dd Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 27 Feb 2020 11:18:31 -0600 Subject: [PATCH 3/7] Add error when legacy methods are detected --- packages/next/build/utils.ts | 21 +++++ .../webpack/loaders/next-serverless-loader.ts | 7 +- .../legacy-ssg-methods-error/pages/index.js | 7 ++ .../test/index.test.js | 81 +++++++++++++++++++ 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 test/integration/legacy-ssg-methods-error/pages/index.js create mode 100644 test/integration/legacy-ssg-methods-error/test/index.test.js diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index 3c9786b3a094448..e7a95e3a0538d52 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -637,6 +637,9 @@ export async function isPageStatic( const hasStaticProps = !!mod.getStaticProps const hasStaticPaths = !!mod.getStaticPaths const hasServerProps = !!mod.getServerSideProps + const hasLegacyServerProps = !!mod.unstable_getServerProps + const hasLegacyStaticProps = !!mod.unstable_getStaticProps + const hasLegacyStaticPaths = !!mod.unstable_getStaticPaths const hasLegacyStaticParams = !!mod.unstable_getStaticParams if (hasLegacyStaticParams) { @@ -645,6 +648,24 @@ export async function isPageStatic( ) } + if (hasLegacyStaticPaths) { + throw new Error( + `unstable_getStaticPaths was replaced with getStaticPaths. Please update your code.` + ) + } + + if (hasLegacyStaticProps) { + throw new Error( + `unstable_getStaticProps was replaced with getStaticProps. Please update your code.` + ) + } + + if (hasLegacyServerProps) { + throw new Error( + `unstable_getServerProps was replaced with getServerSideProps. Please update your code.` + ) + } + // A page cannot be prerendered _and_ define a data requirement. That's // contradictory! if (hasGetInitialProps && hasStaticProps) { diff --git a/packages/next/build/webpack/loaders/next-serverless-loader.ts b/packages/next/build/webpack/loaders/next-serverless-loader.ts index f46fa18cbb548f5..e8c8176b513d6b4 100644 --- a/packages/next/build/webpack/loaders/next-serverless-loader.ts +++ b/packages/next/build/webpack/loaders/next-serverless-loader.ts @@ -217,11 +217,16 @@ const nextServerlessLoader: loader.Loader = function() { const Component = ComponentInfo.default export default Component - export const getStaticProps = ComponentInfo['getStaticProp' + 's'] export const unstable_getStaticParams = ComponentInfo['unstable_getStaticParam' + 's'] + export const getStaticProps = ComponentInfo['getStaticProp' + 's'] export const getStaticPaths = ComponentInfo['getStaticPath' + 's'] export const getServerSideProps = ComponentInfo['getServerSideProp' + 's'] + // kept for detecting legacy exports + export const unstable_getStaticProps = ComponentInfo['unstable_getStaticProp' + 's'] + export const unstable_getStaticPaths = ComponentInfo['unstable_getStaticPath' + 's'] + export const unstable_getServerProps = ComponentInfo['unstable_getServerProp' + 's'] + ${dynamicRouteMatcher} ${handleRewrites} diff --git a/test/integration/legacy-ssg-methods-error/pages/index.js b/test/integration/legacy-ssg-methods-error/pages/index.js new file mode 100644 index 000000000000000..8bf35c1ea71781f --- /dev/null +++ b/test/integration/legacy-ssg-methods-error/pages/index.js @@ -0,0 +1,7 @@ +export async function unstable_getStaticProps() { + return { + props: {}, + } +} + +export default () => 'hi' diff --git a/test/integration/legacy-ssg-methods-error/test/index.test.js b/test/integration/legacy-ssg-methods-error/test/index.test.js new file mode 100644 index 000000000000000..23f85e10ffd284d --- /dev/null +++ b/test/integration/legacy-ssg-methods-error/test/index.test.js @@ -0,0 +1,81 @@ +/* eslint-env jest */ +/* global jasmine */ +import fs from 'fs-extra' +import { join } from 'path' +import { nextBuild } from 'next-test-utils' + +jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1 + +const appDir = join(__dirname, '..') +const indexPage = join(appDir, 'pages/index.js') +let origIndexPage = '' + +const runTests = (serverless = false) => { + if (serverless) { + const nextConfig = join(appDir, 'next.config.js') + + beforeEach(() => + fs.writeFile( + nextConfig, + ` + module.exports = { + target: 'experimental-serverless-trace' + } + ` + ) + ) + + afterAll(() => fs.remove(nextConfig)) + } + + it('should error when legacy unstable_getStaticProps', async () => { + const { stderr, code } = await nextBuild(appDir, [], { stderr: true }) + expect(code).toBe(1) + expect(stderr).toContain( + `unstable_getStaticProps was replaced with getStaticProps. Please update your code.` + ) + }) + + it('should error when legacy unstable_getServerProps', async () => { + await fs.writeFile( + indexPage, + origIndexPage.replace('getStaticProps', 'getServerProps') + ) + + const { stderr, code } = await nextBuild(appDir, [], { stderr: true }) + + expect(code).toBe(1) + expect(stderr).toContain( + `unstable_getServerProps was replaced with getServerSideProps. Please update your code.` + ) + }) + + it('should error when legacy unstable_getServerProps', async () => { + await fs.writeFile( + indexPage, + origIndexPage.replace('getStaticProps', 'getStaticPaths') + ) + + const { stderr, code } = await nextBuild(appDir, [], { stderr: true }) + + expect(code).toBe(1) + expect(stderr).toContain( + `unstable_getStaticPaths was replaced with getStaticPaths. Please update your code.` + ) + }) +} + +describe('Mixed getStaticProps and getServerSideProps error', () => { + beforeAll(async () => { + origIndexPage = await fs.readFile(indexPage, 'utf8') + }) + afterEach(() => fs.writeFile(indexPage, origIndexPage)) + + describe('server mode', () => { + runTests(false) + }) + + describe('serverless mode', () => { + runTests(true) + }) +}) From 5f912d3d5b85d715478d0ebe7d061abf2ae325e2 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 27 Feb 2020 11:31:01 -0600 Subject: [PATCH 4/7] Add legacy methods for babel transform --- packages/next/build/babel/plugins/next-ssg-transform.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/next/build/babel/plugins/next-ssg-transform.ts b/packages/next/build/babel/plugins/next-ssg-transform.ts index 3e38ea2226a8963..0ab23912b232400 100644 --- a/packages/next/build/babel/plugins/next-ssg-transform.ts +++ b/packages/next/build/babel/plugins/next-ssg-transform.ts @@ -16,6 +16,12 @@ const ssgExports = new Set([ EXPORT_NAME_GET_STATIC_PROPS, EXPORT_NAME_GET_STATIC_PATHS, EXPORT_NAME_GET_SERVER_PROPS, + + // legacy methods added so build doesn't fail from importing + // server-side only methods + `unstable_getStaticProps`, + `unstable_getStaticPaths`, + `unstable_getServerProps`, ]) type PluginState = { From 3eb91b90059f776c927bf6b7d1967028d63a4299 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 27 Feb 2020 11:32:07 -0600 Subject: [PATCH 5/7] Add unstable_getServerSideProps also --- packages/next/build/babel/plugins/next-ssg-transform.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/next/build/babel/plugins/next-ssg-transform.ts b/packages/next/build/babel/plugins/next-ssg-transform.ts index 0ab23912b232400..e8d5ad60cb2e937 100644 --- a/packages/next/build/babel/plugins/next-ssg-transform.ts +++ b/packages/next/build/babel/plugins/next-ssg-transform.ts @@ -22,6 +22,7 @@ const ssgExports = new Set([ `unstable_getStaticProps`, `unstable_getStaticPaths`, `unstable_getServerProps`, + `unstable_getServerSideProps`, ]) type PluginState = { From 868a20d079087266cbd66961eb999441df9482eb Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 27 Feb 2020 11:37:24 -0600 Subject: [PATCH 6/7] Apply suggestions from code review Co-Authored-By: Joe Haddad --- packages/next/lib/constants.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/lib/constants.ts b/packages/next/lib/constants.ts index ffad8c242c4a087..e8025b25e9ec28b 100644 --- a/packages/next/lib/constants.ts +++ b/packages/next/lib/constants.ts @@ -26,8 +26,8 @@ export const PUBLIC_DIR_MIDDLEWARE_CONFLICT = `You can not have a '_next' folder export const SSG_GET_INITIAL_PROPS_CONFLICT = `You can not use getInitialProps with getStaticProps. To use SSG, please remove your getInitialProps` -export const SERVER_PROPS_GET_INIT_PROPS_CONFLICT = `You can not use getInitialProps with getServerSideProps. Please remove one or the other` +export const SERVER_PROPS_GET_INIT_PROPS_CONFLICT = `You can not use getInitialProps with getServerSideProps. Please remove getInitialProps.` -export const SERVER_PROPS_SSG_CONFLICT = `You can not use getStaticProps with getServerSideProps. To use SSG, please remove your getServerSideProps` +export const SERVER_PROPS_SSG_CONFLICT = `You can not use getStaticProps with getServerSideProps. To use SSG, please remove getServerSideProps` export const PAGES_404_GET_INITIAL_PROPS_ERROR = `\`pages/404\` can not have getInitialProps/getServerSideProps, https://err.sh/zeit/next.js/404-get-initial-props` From 75ec579bc9fee6871f651e0bebc93a30f8705070 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 27 Feb 2020 11:40:14 -0600 Subject: [PATCH 7/7] Update types import --- packages/next/build/utils.ts | 2 +- packages/next/next-server/server/load-components.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index e7a95e3a0538d52..e0e9ae3d605ffa7 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -15,7 +15,7 @@ import { recursiveReadDir } from '../lib/recursive-readdir' import { getRouteMatcher, getRouteRegex } from '../next-server/lib/router/utils' import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic' import { findPageFile } from '../server/lib/find-page-file' -import { GetStaticPaths } from '../types/index' +import { GetStaticPaths } from 'next/types' const fileGzipStats: { [k: string]: Promise } = {} const fsStatGzip = (file: string) => { diff --git a/packages/next/next-server/server/load-components.ts b/packages/next/next-server/server/load-components.ts index 9861ab0a2c2b401..3b8fa7accd0eb9e 100644 --- a/packages/next/next-server/server/load-components.ts +++ b/packages/next/next-server/server/load-components.ts @@ -16,7 +16,7 @@ import { GetStaticPaths, GetServerSideProps, GetStaticProps, -} from '../../types/index' +} from 'next/types' export function interopDefault(mod: any) { return mod.default || mod