本页还没有译文
本页 30 段还没译成日本語,下面显示的是英文原文。Static site generation
还没翻译译自英文(svelte.dev)本页 30 段都还没译
To use SvelteKit as a static site generator (SSG), use adapter-static.
This will prerender your entire site as a collection of static files. If you'd like to prerender only some pages and dynamically server-render others, you will need to use a different adapter together with the prerender option.
Usage
Run npx sv add sveltekit-adapter="adapter:static", or install with npm i -D @sveltejs/adapter-static and add the adapter to your vite.config.js:
import adapter from '@sveltejs/adapter-static';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
sveltekit({
adapter: adapter({
// default options are shown. On some platforms
// these options are set automatically — see below
pages: 'build',
assets: 'build',
fallback: undefined,
precompress: false,
strict: true
})
})
]
});...and add the prerender option to your root layout:
// If you're using a fallback (i.e. SPA mode) you don't need to prerender all
// pages by setting this here, but should prerender as many as possible to
// avoid large performance and SEO impacts
export const prerender = true;Note
You must ensure SvelteKit's trailingSlash option is set appropriately for your environment. If your host does not render /a.html upon receiving a request for /a then you will need to set trailingSlash: 'always' in your root layout to create /a/index.html instead.
Note
You must ensure SvelteKit's ssr option isn't set to false. Otherwise, prerendering will save an empty 'shell' page instead of the fully rendered content.
Zero-config support
Some platforms have zero-config support (more to come in future):
On these platforms, you should omit the adapter options so that adapter-static can provide the optimal configuration:
import adapter from '@sveltejs/adapter-static';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
sveltekit({
adapter: adapter({...})
})
]
});Options
pages
The directory to write prerendered pages to. It defaults to build.
assets
The directory to write static assets (the contents of static, plus client-side JS and CSS generated by SvelteKit) to. Ordinarily this should be the same as pages, and it will default to whatever the value of pages is, but in rare circumstances you might need to output pages and assets to separate locations.
fallback
To create a single page app (SPA) you must specify the name of the fallback page to be generated by SvelteKit, which is used as the entry point for URLs that have not been prerendered. This is commonly 200.html, but can vary depending on your deployment platform. You should avoid index.html where possible to avoid conflicting with a prerendered homepage.
Note
This option has large negative performance and SEO impacts. It is only recommended in certain circumstances such as wrapping the site in a mobile app. See the single page apps documentation for more details and alternatives.
precompress
If true, precompresses files with brotli and gzip. This will generate .br and .gz files.
strict
By default, adapter-static checks that either all pages and endpoints (if any) of your app were prerendered, or you have the fallback option set. This check exists to prevent you from accidentally publishing an app where some parts of it are not accessible, because they are not contained in the final output. If you know this is ok (for example when a certain page only exists conditionally), you can set strict to false to turn off this check.
GitHub Pages
When building for GitHub Pages, if your repo name is not equivalent to your-username.github.io, make sure to update config.paths.base to match your repo name. This is because the site will be served from https://your-username.github.io/your-repo-name rather than from the root.
You'll also want to generate a fallback 404.html page to replace the default 404 page shown by GitHub Pages.
A config for GitHub Pages might look like the following:
import process from 'node:process';
import adapter from '@sveltejs/adapter-static';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
sveltekit({
adapter: adapter({
fallback: '404.html'
}),
paths: {
base: process.argv.includes('dev') ? '' : process.env.BASE_PATH
},
})
]
});You can use GitHub actions to automatically deploy your site to GitHub Pages when you make a change. Here's an example workflow:
name: Deploy to GitHub Pages
on:
push:
branches: 'main'
permissions:
contents: read
pages: read
concurrency:
group: pages
cancel-in-progress: false
jobs:
build_site:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v7
# 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@v6
- name: Install Node.js
uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
- name: Install dependencies
run: npm i
- name: Configure Pages
id: pages
uses: actions/configure-pages@v6
- name: build
env:
BASE_PATH: ${{ steps.pages.outputs.base_path }}
run: |
npm run build
- name: Upload Artifacts
uses: actions/upload-pages-artifact@v5
with:
# this should match the `pages` option in your adapter-static options
path: 'build/'
# keep dotfiles such as `static/.well-known`
include-hidden-files: true
deploy:
needs: build_site
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy
id: deployment
uses: actions/deploy-pages@v5If you're not using GitHub actions to deploy your site (for example, you're pushing the built site to its own repo), add an empty .nojekyll file in your static directory to prevent Jekyll from interfering.