Next.js
getStaticProps & getInitialProps
getStaticProps
: Use this method trigger "Static generation", which can lead to faster loading experience. (Because the page is rendered at build time)getInitialProps
: This is server-side rendering.- Should we change all
getInitialProps
togetStaticProps
?
No, we don't have to. Because some pages would not be visited directly by URL. For example, the checkout page in the purchase flow. It would be visited with front-end router (the page is rendered by JS), so the static generation wouldn't help with the loading speed.
https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation
https://nextjs.org/docs/api-reference/data-fetching/getInitialProps
頁面 render: 網址 直接對應 資料夾路徑
Dynamic route segments
https://nextjs.org/docs/routing/introduction#dynamic-route-segments
pages/[username]/settings.js
→ /:username/settings
(/foo/settings)
Link & a tag
在我們的專案,用 I18nLink 統一處理前端 routing;用 a tag 的話,就不是 SPA 的跳轉了,而會像是重新整理那樣的 server side rendering 跳轉
- 但在新版的 Next.js,例如這個部落格的專案,用 a tag 的話,Next.js 會幫你做前端 routing 處理,而不會造成重整式的跳頁