Skip to content

Commit

Permalink
Add error when legacy methods are detected
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Feb 27, 2020
1 parent cde3cdd commit c3ed0a7
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/next/build/utils.ts
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Expand Up @@ -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}
Expand Down
7 changes: 7 additions & 0 deletions test/integration/legacy-ssg-methods-error/pages/index.js
@@ -0,0 +1,7 @@
export async function unstable_getStaticProps() {
return {
props: {},
}
}

export default () => 'hi'
81 changes: 81 additions & 0 deletions 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)
})
})

0 comments on commit c3ed0a7

Please sign in to comment.