Next.js - 部署



到目前為止,我們在開發模式中開發並運行了 NEXT.JS 示例應用程式,現在我們將使用以下步驟在本地執行生產就緒部署。

  • npm run build − 構建生產就緒且高度最佳化的版本。

  • npm run start − 啟動伺服器。

與開發模式相比,生產就緒版本缺少源對映和熱程式碼過載,因為這些功能主要用於除錯。

準備構建

執行以下命令以準備生產就緒版本 −.

npm run build

> nextjs@1.0.0 build \Node\nextjs
> next build

info  - Loaded env from \Node\nextjs\.env.local
Creating an optimized production build

Compiled successfully.

Automatically optimizing pages

Page                                                           Size     First Load JS
+ ? /                                                          2.25 kB        60.3 kB
+   /_app                                                      288 B          58.1 kB
+        /404                                                       3.25 kB   61.3 kB
+ ? /api/user
+ ? /posts/[id]                                                312 B          61.6 kB
+   + /posts/one
+   + /posts/two
+ ? /posts/env                                                 2.71 kB        60.8 kB
+ ? /posts/first                                               374 B          61.7 kB
+ First Load JS shared by all                                  58.1 kB
  + static/pages/_app.js                                       288 B
  + chunks/3458401054237127135bcd3ee8eb2a19d67af299.a1a019.js  10.5 kB
  + chunks/framework.c6faae.js                                 40 kB
  + runtime/main.60464f.js                                     6.54 kB
  + runtime/webpack.c21266.js                                  746 B
  + css/9706b5b8ed8e82c0fba0.css                               175 B

?  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
          (Static)  automatically rendered as static HTML (uses no initial props)
?  (SSG)     automatically generated as static HTML + JSON (uses getStaticProps)

啟動伺服器

執行以下命令以啟動生產伺服器 −.

npm run start

> nextjs@1.0.0 start \Node\nextjs
> next start

info  - Loaded env from \Node\nextjs\.env.local
ready - started server on https://:3000

驗證輸出

在瀏覽器中開啟 localhost:3000/api/user,您將看到以下輸出。

{"name":"Robert"}
廣告