Emmacheng
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
###### tags: `Rendering Patterns` # [Render] Static Rendering(SSG靜態渲染) ## introduction - SSR - 缺點:TTFB(Time to First Byte)需要較多時間 - CSR - 缺點:因為JS bundle size較大,可能影響FCP(First Contentful Paint), LCP(Largest Contentful Paint) and TTI(Time to Interactive) - SSG解決以上問題的方法 - server build time的時候預先產生html page, 達到當client-side發出請求可以快速拿到html頁面並互動,且可使client-side的JS bundle size縮小, 達到較快的 **FCP/TTI** [SSG Video](https://www.patterns.dev/posts/static-rendering/) ![](https://i.imgur.com/zk8o6d3.png) ## SSG - Basic Structure - 顧名思義就是使用在靜態內容的頁面: 電子商務的‘About us'頁面, ‘Contact us'頁面, etc.而非 ’logged-in user‘客製化頁面 - 支援SSG的Frameworks有:Next.js, Gatsby, and VuePress - 當後端sever build next.js時about頁面會先被render成HTML file(about.html), 當前端發出route “/about”請求時回傳給前端 [Next.js demo-SSG](https://codesandbox.io/s/ssg-fmkqyx) ## SSG with Data ### intro - 不須先跟Database拿資料: 靜態頁面'About us' or 'Contact us', 不需要跟Database拿資料就可以直接轉成HTML - 需要先跟Database拿資料: 個別化頁面'blog pages' or 'product pages', 在server build time時透過getStaticProps函式先跟Database拿資料完成(resolve)後,跟template merge 轉成 HTML - HTML pages的數量取決於個人化頁面資料量 - 個別化頁面與其他頁面的相依性:'product pages'跟navigation頁面有相依性所以需要先跟Database要navigation頁面所需的資料像是分類和格式化資料render產品的分類列表 [Next.js Demo-n6wrkh](https://codesandbox.io/s/musing-spence-n6wrkh?file=/pages/index.js) ### Listing Page(列表頁面) - All Items - 在**sever build time**時匯出page的同時也會匯出 **`getStaticProps()`** 函式 - `getStaticProps`是一個 **`async`** 函式, 利用這個函式跟外部Database獲取資料, 另一方面, 有`getStaticProps`函式 Next.js 就會知道要先跟外部要資料成功後, 再把資料塞進 **`props`** render html - **`getStaticProps`** 只會在sever-side被執行, 不會在client-side被執行, 所以不會被打包傳進client-side的JS檔 - [Next.js Demo-98ze4k](https://codesandbox.io/s/amazing-torvalds-98ze4k?file=/pages/index.js) ### Individual Details Page(個別化詳細頁面) - Per Item - 為列表頁面上列出的每個產品提供一個單獨的詳細頁面可使用**dynamic routes**, `getStaticPaths` 可以事先定義哪些單獨頁面需要產生 HTML 檔案 - 在sever build time時匯出共用 products/[id].js page的同時也會匯出 `getStaticPaths()` 函式 - `getStaticPaths`是一個 **`async`** 函式回傳值包含兩個 key,分別是 **paths** 與 **fallback** - ==`getStaticPaths`把所有 dynamic routes 的 key 透過 paths 傳入 `getStaticProps`== - ==`getStaticProps`透過 paths 取得 params 的參數來取得該頁面的資料內容== - [Demo post page at localhost](https://github.com/vercel/next.js/tree/canary/examples/with-static-export) ## SSG - Key Considerations 正如討論的,SSG有好的performance(縮短TTFB)和SEO(回應給client-side已經build好的html),在評估 SSG 對特定應用的適用性時,需要考慮以下因素: 1. **A large number of HTML files**: 需要為用戶可能訪問的每個可能的路徑生成單獨的 HTML 文件,例如:build每篇post貼文儲存在sever端,編輯任意一個post就要重新rebuild一大包靜態資料夾,維護大量 HTML 文件可能具有挑戰性 2. **Hosting Dependency** - 託管在平台: 用於存儲和提供 HTML 文件的託管平台效能要不錯,可使 SSG 站點超快並快速響應 - 託管在多個CDN: 如果把調整好的SSG網站託管在多個CDN上可以有最高的效能 4. **Dynamic Content**: - 每次內容更改時,都需要build和re-deployed SSG 站點 - 如果網站在任何內容更改後尚未re-build+re-deployed,則顯示的內容可能是舊的 - 以上兩點使得 SSG 不適合高度動態的內容 ## Reference - [Next.js dynamic Routes](https://nextjs.org/docs/routing/dynamic-routes) - [ref: Next.js getStaticProps](https://nextjs.org/docs/basic-features/data-fetching/get-static-props) - [Next.js getStaticPaths](https://nextjs.org/docs/basic-features/data-fetching/get-static-paths) - [Demo dynamic Routes](https://codesandbox.io/s/ecstatic-frog-6ecuck?file=/pages/index.js) - [Vercel](https://vercel.com/blog/nextjs-server-side-rendering-vs-static-generation#individual-product-page-static-generation-with-data)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully