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..e8d5ad60cb2e937 100644 --- a/packages/next/build/babel/plugins/next-ssg-transform.ts +++ b/packages/next/build/babel/plugins/next-ssg-transform.ts @@ -8,14 +8,21 @@ 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, 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`, + `unstable_getServerSideProps`, ]) type PluginState = { 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..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 { Unstable_getStaticPaths } from '../next-server/server/load-components' +import { GetStaticPaths } from 'next/types' 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,35 @@ 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 hasLegacyServerProps = !!mod.unstable_getServerProps + const hasLegacyStaticProps = !!mod.unstable_getStaticProps + const hasLegacyStaticPaths = !!mod.unstable_getStaticPaths 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.` + ) + } + + 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.` ) } @@ -663,14 +684,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 +702,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..e8c8176b513d6b4 100644 --- a/packages/next/build/webpack/loaders/next-serverless-loader.ts +++ b/packages/next/build/webpack/loaders/next-serverless-loader.ts @@ -217,10 +217,15 @@ const nextServerlessLoader: loader.Loader = function() { const Component = ComponentInfo.default export default Component - export const unstable_getStaticProps = ComponentInfo['unstable_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_getServerSideProps = ComponentInfo['unstable_getServerSideProp' + 's'] + export const unstable_getServerProps = ComponentInfo['unstable_getServerProp' + 's'] ${dynamicRouteMatcher} ${handleRewrites} @@ -241,9 +246,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 +280,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 +325,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 +336,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 +355,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 +365,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..e8025b25e9ec28b 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 getInitialProps.` -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 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..3b8fa7accd0eb9e 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 'next/types' 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/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) + }) +}) 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 } )