建置和部署
靜態網站產生
在 GitHub 上編輯此頁面若要將 SvelteKit 用作靜態網站產生器 (SSG),請使用 adapter-static
。
這將預先渲染您的整個網站作為靜態檔案的集合。如果您只想預先渲染某些頁面,並動態伺服器渲染其他頁面,則需要將不同的轉接器與 prerender
選項 搭配使用。
使用永久連結
使用 npm i -D @sveltejs/adapter-static
安裝,然後將轉接器新增至您的 svelte.config.js
ts
importCannot find module '@sveltejs/adapter-static' or its corresponding type declarations.2307Cannot find module '@sveltejs/adapter-static' or its corresponding type declarations.adapter from'@sveltejs/adapter-static' ;export default {kit : {adapter :adapter ({// default options are shown. On some platforms// these options are set automatically — see belowpages : 'build',assets : 'build',fallback :undefined ,precompress : false,strict : true})}};
...並將 prerender
選項新增至您的根佈局
ts
// This can be false if you're using a fallback (i.e. SPA mode)export constprerender = true;
ts
// This can be false if you're using a fallback (i.e. SPA mode)export constprerender = true;
您必須確保 SvelteKit 的
trailingSlash
選項已適當地設定為您的環境。如果您的主機在收到/a
的請求時沒有渲染/a.html
,則您需要在根佈局中設定trailingSlash: 'always'
來建立/a/index.html
。
零組態支援永久連結
有些平台有零組態支援(未來會有更多)
在這些平台上,您應該省略轉接器選項,以便 adapter-static
可以提供最佳組態
export default {
kit: {
adapter: adapter({...})
adapter: adapter()
}
};
選項永久連結
pages永久連結
要寫入預先渲染頁面的目錄。預設為 build
。
assets永久連結
要寫入靜態資產的目錄(static
的內容,加上 SvelteKit 產生的用戶端 JS 和 CSS)。通常這應該與 pages
相同,並且會預設為 pages
的值,但在某些罕見情況下,您可能需要將頁面和資產輸出到不同的位置。
備用永久連結
為 SPA 模式 指定備用頁面,例如 index.html
或 200.html
或 404.html
。
預先壓縮永久連結
如果為 true
,會使用 brotli 和 gzip 預先壓縮檔案。這會產生 .br
和 .gz
檔案。
嚴格永久連結
預設情況下,adapter-static
會檢查應用程式的所有頁面和端點(如果有)是否已預先渲染,或者您已設定 fallback
選項。此檢查的存在是為了防止您意外發布應用程式,其中某些部分無法存取,因為它們未包含在最終輸出中。如果您知道這沒問題(例如,當某個頁面僅有條件存在時),您可以將 strict
設定為 false
以關閉此檢查。
GitHub Pages永久連結
在為 GitHub Pages 建置時,如果您的儲存庫名稱不等於 your-username.github.io
,請務必更新 config.kit.paths.base
以符合您的儲存庫名稱。這是因為網站會從 https://your-username.github.io/your-repo-name
提供,而不是從根目錄提供。
您還需要產生一個備用的 404.html
頁面,以取代 GitHub Pages 顯示的預設 404 頁面。
GitHub Pages 的設定檔可能如下所示
ts
importCannot find module '@sveltejs/adapter-static' or its corresponding type declarations.2307Cannot find module '@sveltejs/adapter-static' or its corresponding type declarations.adapter from'@sveltejs/adapter-static' ;/** @type {import('@sveltejs/kit').Config} */constconfig = {kit : {adapter :adapter ({fallback : '404.html'}),paths : {Type 'string | undefined' is not assignable to type '"" | `/${string}` | undefined'. Type 'string' is not assignable to type '"" | `/${string}` | undefined'.2322Type 'string | undefined' is not assignable to type '"" | `/${string}` | undefined'. Type 'string' is not assignable to type '"" | `/${string}` | undefined'.: base process .argv .includes ('dev') ? '' :process .env .BASE_PATH }}};export defaultconfig ;
當您進行變更時,您可以使用 GitHub 操作自動將您的網站部署到 GitHub Pages。以下是一個範例工作流程
yaml
name: Deploy to GitHub Pageson:push:branches: 'main'jobs:build_site:runs-on: ubuntu-lateststeps:- name: Checkoutuses: actions/checkout@v4# If you're using pnpm, add this step then change the commands and cache key below to use `pnpm`# - name: Install pnpm# uses: pnpm/action-setup@v3# with:# version: 8- name: Install Node.jsuses: actions/setup-node@v4with:node-version: 20cache: npm- name: Install dependenciesrun: npm install- name: buildenv:BASE_PATH: '/${{ github.event.repository.name }}'run: |npm run build- name: Upload Artifactsuses: actions/upload-pages-artifact@v3with:# this should match the `pages` option in your adapter-static optionspath: 'build/'deploy:needs: build_siteruns-on: ubuntu-latestpermissions:pages: writeid-token: writeenvironment:name: github-pagesurl: ${{ steps.deployment.outputs.page_url }}steps:- name: Deployid: deploymentuses: actions/deploy-pages@v4
如果您沒有使用 GitHub 操作來部署您的網站(例如,您將建置的網站推送到其自己的儲存庫),請在您的 static
目錄中新增一個空的 .nojekyll
檔案,以防止 Jekyll 造成干擾。