升級至版本 9
若要升級至版本 9,請執行下列指令
npm i next@9
yarn add next@9
pnpm up next@9
bun add next@9
小知識: 如果您使用 TypeScript,請確保您也將
@types/react
和@types/react-dom
升級到對應的版本。
在 Vercel 上生產環境部署
如果您先前在 vercel.json
檔案中為動態路由設定了 routes
,則在使用 Next.js 9 的新動態路由功能時,可以移除這些規則。
Next.js 9 的動態路由已在 Vercel 上自動設定,並且不需要任何 vercel.json
自訂設定。
您可以在此處閱讀更多關於動態路由的資訊。
檢查您的自訂 App 檔案 (pages/_app.js
)
如果您先前複製了自訂 <App>
範例,您或許可以移除您的 getInitialProps
。
從 pages/_app.js
中移除 getInitialProps
(如果可能) 對於利用新的 Next.js 功能非常重要!
以下 getInitialProps
不執行任何操作,可以移除
class MyApp extends App {
// Remove me, I do nothing!
static async getInitialProps({ Component, ctx }) {
let pageProps = {}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return { pageProps }
}
render() {
// ... etc
}
}
重大變更
@zeit/next-typescript
已不再需要
Next.js 現在將忽略 @zeit/next-typescript
的使用,並警告您將其移除。請從您的 next.config.js
中移除此插件。
從您的自訂 .babelrc
(如果存在) 中移除對 @zeit/next-typescript/babel
的參考。
也應從您的 next.config.js
中移除 fork-ts-checker-webpack-plugin
的使用。
TypeScript 定義已與 next
套件一起發布,因此您需要解除安裝 @types/next
,因為它們會衝突。
以下類型有所不同
此清單由社群建立,以協助您升級,如果您發現其他差異,請發送 pull request 到此清單以協助其他使用者。
從
import { NextContext } from 'next'
import { NextAppContext, DefaultAppIProps } from 'next/app'
import { NextDocumentContext, DefaultDocumentIProps } from 'next/document'
到
import { NextPageContext } from 'next'
import { AppContext, AppInitialProps } from 'next/app'
import { DocumentContext, DocumentInitialProps } from 'next/document'
config
鍵現在是頁面上的匯出
您可能不再從頁面匯出名為 config
的自訂變數 (即 export { config }
/ export const config ...
)。此匯出的變數現在用於指定頁面層級的 Next.js 設定,例如選擇加入 AMP 和 API 路由功能。
您必須將非 Next.js 用途的 config
匯出重新命名為其他名稱。
next/dynamic
在載入時預設不再渲染 "loading..."
動態元件在載入時預設不會渲染任何內容。您仍然可以透過設定 loading
屬性來自訂此行為
import dynamic from 'next/dynamic'
const DynamicComponentWithCustomLoading = dynamic(
() => import('../components/hello2'),
{
loading: () => <p>Loading</p>,
}
)
withAmp
已移除,改用匯出的設定物件
Next.js 現在具有頁面層級設定的概念,因此為了保持一致性,已移除 withAmp
高階元件。
此變更可以透過在您的 Next.js 專案根目錄中執行以下命令來自動遷移:
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
若要手動執行此遷移,或檢視 codemod 將產生的結果,請參閱下方
之前
import { withAmp } from 'next/amp'
function Home() {
return <h1>My AMP Page</h1>
}
export default withAmp(Home)
// or
export default withAmp(Home, { hybrid: true })
之後
export default function Home() {
return <h1>My AMP Page</h1>
}
export const config = {
amp: true,
// or
amp: 'hybrid',
}
next export
不再將頁面匯出為 index.html
先前,匯出 pages/about.js
會產生 out/about/index.html
。此行為已變更為產生 out/about.html
。
您可以透過建立具有以下內容的 next.config.js
來還原為先前的行為
module.exports = {
trailingSlash: true,
}
pages/api/
的處理方式不同
pages/api/
中的頁面現在被視為 API 路由。此目錄中的頁面將不再包含用戶端套件。
已棄用的功能
next/dynamic
已棄用一次載入多個模組
next/dynamic
中已棄用一次載入多個模組的功能,使其更接近 React 的實作 (React.lazy
和 Suspense
)。
更新依賴此行為的程式碼相對簡單!我們提供了一個之前/之後的範例,以協助您遷移應用程式
之前
import dynamic from 'next/dynamic'
const HelloBundle = dynamic({
modules: () => {
const components = {
Hello1: () => import('../components/hello1').then((m) => m.default),
Hello2: () => import('../components/hello2').then((m) => m.default),
}
return components
},
render: (props, { Hello1, Hello2 }) => (
<div>
<h1>{props.title}</h1>
<Hello1 />
<Hello2 />
</div>
),
})
function DynamicBundle() {
return <HelloBundle title="Dynamic Bundle" />
}
export default DynamicBundle
之後
import dynamic from 'next/dynamic'
const Hello1 = dynamic(() => import('../components/hello1'))
const Hello2 = dynamic(() => import('../components/hello2'))
function HelloBundle({ title }) {
return (
<div>
<h1>{title}</h1>
<Hello1 />
<Hello2 />
</div>
)
}
function DynamicBundle() {
return <HelloBundle title="Dynamic Bundle" />
}
export default DynamicBundle
這有幫助嗎?