Upgrade Guide

Upgrading from version 8 to 9.0.x

Preamble

Production Deployment on Vercel

If you previously configured routes in your vercel.json file for dynamic routes, these rules can be removed when leveraging Next.js 9’s new Dynamic Routing feature.

Next.js 9’s dynamic routes are automatically configured on Vercel and do not require any vercel.json customization.

You can read more about Dynamic Routing here.

Check your Custom (pages/_app.js)

If you previously copied the Custom <App> example, you may be able to remove your getInitialProps.

Removing getInitialProps from pages/_app.js (when possible) is important to leverage new Next.js features!

The following getInitialProps does nothing and may be removed:

  1. class MyApp extends App {
  2. // Remove me, I do nothing!
  3. static async getInitialProps({ Component, ctx }) {
  4. let pageProps = {}
  5. if (Component.getInitialProps) {
  6. pageProps = await Component.getInitialProps(ctx)
  7. }
  8. return { pageProps }
  9. }
  10. render() {
  11. // ... etc
  12. }
  13. }

Breaking Changes

@zeit/next-typescript is no longer necessary

Next.js will now ignore usage @zeit/next-typescript and warn you to remove it. Please remove this plugin from your next.config.js.

Remove references to @zeit/next-typescript/babel from your custom .babelrc (if present).

Usage of fork-ts-checker-webpack-plugin should also be removed from your next.config.js.

TypeScript Definitions are published with the next package, so you need to uninstall @types/next as they would conflict.

The following types are different:

This list was created by the community to help you upgrade, if you find other differences please send a pull-request to this list to help other users.

From:

  1. import { NextContext } from 'next'
  2. import { NextAppContext, DefaultAppIProps } from 'next/app'
  3. import { NextDocumentContext, DefaultDocumentIProps } from 'next/document'

to

  1. import { NextPageContext } from 'next'
  2. import { AppContext, AppInitialProps } from 'next/app'
  3. import { DocumentContext, DocumentInitialProps } from 'next/document'

The config key is now a special export on a page

You may no longer export a custom variable named config from a page (i.e. export { config } / export const config ...). This exported variable is now used to specify page-level Next.js configuration like Opt-in AMP and API Route features.

You must rename a non-Next.js-purposed config export to something different.

next/dynamic no longer renders “loading…” by default while loading

Dynamic components will not render anything by default while loading. You can still customize this behavior by setting the loading property:

  1. import dynamic from 'next/dynamic'
  2. const DynamicComponentWithCustomLoading = dynamic(
  3. () => import('../components/hello2'),
  4. {
  5. loading: () => <p>Loading</p>,
  6. }
  7. )

withAmp has been removed in favor of an exported configuration object

Next.js now has the concept of page-level configuration, so the withAmp higher-order component has been removed for consistency.

This change can be automatically migrated by running the following commands in the root of your Next.js project:

  1. curl -L https://github.com/vercel/next-codemod/archive/master.tar.gz | tar -xz --strip=2 next-codemod-master/transforms/withamp-to-config.js npx jscodeshift -t ./withamp-to-config.js pages/**/*.js

To perform this migration by hand, or view what the codemod will produce, see below:

Before

  1. import { withAmp } from 'next/amp'
  2. function Home() {
  3. return <h1>My AMP Page</h1>
  4. }
  5. export default withAmp(Home)
  6. // or
  7. export default withAmp(Home, { hybrid: true })

After

  1. export default function Home() {
  2. return <h1>My AMP Page</h1>
  3. }
  4. export const config = {
  5. amp: true,
  6. // or
  7. amp: 'hybrid',
  8. }

next export no longer exports pages as index.html

Previously, exporting pages/about.js would result in out/about/index.html. This behavior has been changed to result in out/about.html.

You can revert to the previous behavior by creating a next.config.js with the following content:

  1. // next.config.js
  2. module.exports = {
  3. trailingSlash: true,
  4. }

./pages/api/ is treated differently

Pages in ./pages/api/ are now considered API Routes. Pages in this directory will no longer contain a client-side bundle.

Deprecated Features

next/dynamic has deprecated loading multiple modules at once

The ability to load multiple modules at once has been deprecated in next/dynamic to be closer to React’s implementation (React.lazy and Suspense).

Updating code that relies on this behavior is relatively straightforward! We’ve provided an example of a before/after to help you migrate your application:

Before

  1. import dynamic from 'next/dynamic'
  2. const HelloBundle = dynamic({
  3. modules: () => {
  4. const components = {
  5. Hello1: () => import('../components/hello1').then((m) => m.default),
  6. Hello2: () => import('../components/hello2').then((m) => m.default),
  7. }
  8. return components
  9. },
  10. render: (props, { Hello1, Hello2 }) => (
  11. <div>
  12. <h1>{props.title}</h1>
  13. <Hello1 />
  14. <Hello2 />
  15. </div>
  16. ),
  17. })
  18. function DynamicBundle() {
  19. return <HelloBundle title="Dynamic Bundle" />
  20. }
  21. export default DynamicBundle

After

  1. import dynamic from 'next/dynamic'
  2. const Hello1 = dynamic(() => import('../components/hello1'))
  3. const Hello2 = dynamic(() => import('../components/hello2'))
  4. function HelloBundle({ title }) {
  5. return (
  6. <div>
  7. <h1>{title}</h1>
  8. <Hello1 />
  9. <Hello2 />
  10. </div>
  11. )
  12. }
  13. function DynamicBundle() {
  14. return <HelloBundle title="Dynamic Bundle" />
  15. }
  16. export default DynamicBundle