Skip to main content
Svelte基础
介绍
响应
属性Props
逻辑表达式
事件
绑定
Classes和样式
动作Actions
转场
Svelte高阶
响应式进阶
内容复用
动画
高级绑定
高级转场
上下文API
特殊元素
脚本模块
接下来
SvelteKit基础
介绍
路由
加载数据
Headers和cookies
Shared modules
Forms
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Hooks
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion

Prerendering means generating HTML for a page once, at build time, rather than dynamically for each request.

The advantage is that serving static data is extremely cheap and performant, allowing you to easily serve large numbers of users without worrying about cache-control headers (which are easy to get wrong).

The tradeoff is that the build process takes longer, and prerendered content can only be updated by building and deploying a new version of the application.

To prerender a page, set prerender to true:

src/routes/+page.server
export const prerender = true;

Here in the tutorial, this won’t have any observable effect, since the application is running in dev mode.

Not all pages can be prerendered. The basic rule is this: for content to be prerenderable, any two users hitting it directly must get the same content from the server, and the page must not contain form actions. Pages with dynamic route parameters can be prerendered as long as they are specified in the prerender.entries configuration or can be reached by following links from pages that are in prerender.entries.

Setting prerender to true inside your root +layout.server.js effectively turns SvelteKit into a static site generator (SSG).

Edit this page on GitHub

previous next
1
2
<h1>Prerendering</h1>