伊欧 d77cf0196b
Prepare for 0.22 release (#3750)
* Update CHANGELOG
https://github.com/yewstack/yew/actions/runs/11314974928/job/31465588862

* Add items.

* Write blog.

* Archive the documents.

* Add author.

* Update SSR document.

* Fix typo.

* Add simplified Chinese translation.

* Update package.json

* Sync documents

* Add traditional Chinese translation.

* Sync documents

* Add Japanese translation.

* Sync documents

* Fix typo by `fmt:write`.

* Fix typo by `write-translations`.

* Apply suggestions from code review

* Fix typo.

* #3769 in changelog

---------

Co-authored-by: Elina <imelina@elina.website>
2024-12-17 16:27:02 +08:00

46 lines
2.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: '部署'
description: '部署 Yew 應用程式'
---
當您準備將 Yew 應用程式部署到伺服器時,您有多種部署方案可以選擇。
`trunk build --release` 會以發布模式建立您的應用程式。設定您的HTTP 伺服器,以便在存取您的網站時提供`index.html`,並且對於靜態路徑(例如`index_<hash>.js` 和`index_bg_<hash>.wasm`的請求應該從trunk產生的dist 目錄中提供相應的內容。
:::important 有關 `trunk serve --release`
不要在生產環境中使用 `trunk serve --release` 來提供您的應用程式。
它只應該用於在開發過程中測試發布版本建置。
:::
## 伺服器配置
### 將 `index.html` 當作回退提供
如果應用程式使用了 [Yew 路由](concepts/router.mdx),您必須設定伺服器在請求不存在的檔案時傳回 `index.html`。
具有 Yew 路由的應用程式被建構為 [單頁應用程式 (SPA)](https://developer.mozilla.org/en-US/docs/Glossary/SPA)。當使用者從正在執行的用戶端導覽到 URL 時,路由器會解釋 URL 並路由到該頁面。
但是在刷新頁面或在網址列中輸入 URL 時,這些操作都是由瀏覽器本身處理的,而不是由正在執行的應用程式處理。瀏覽器直接向伺服器請求該 URL繞過了路由器。錯誤配置的伺服器會回傳 404 - 未找到 狀態。
透過返回 `index.html`,應用程式會像通常一樣加載,就好像請求是 `/`,直到路由器注意到路由是 `/show/42` 並顯示相應的內容。
### 為 Web Assembly 資源配置正確的 MIME 類型。
WASM 檔案必須使用 `application/wasm` MIME 類型設定 [Content-Type 頭](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type)。
大多數伺服器和託管服務預設已經這樣做。如果您的伺服器沒有這樣做,請查閱其文件。在大多數 Web 瀏覽器中,錯誤的 MIME 類型會導致類似以下的錯誤:
```ignore
`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:
TypeError: WebAssembly: Response has unsupported MIME type 'text/plain' expected 'application/wasm'
```
## 為相對路徑構建
預設情況下trunk 會假定您的網站在 `/` 處提供,並相應地建立網站。可以透過在 `index.html` 檔案中加入 `<base data-trunk-public-url />` 來覆寫此行為。 Trunk 會重寫此標籤以包含傳遞給 `--public-url` 的值。 Yew 路由會自動偵測 `<base />` 的存在並適當處理。
## 使用環境變數自訂行為
通常使用環境變數來自訂建構環境。由於應用程式在瀏覽器中運行,我們無法在運行時讀取環境變數。
[`std::env!`](https://doc.rust-lang.org/std/macro.env.html) 巨集可以在編譯時取得環境變數的值。