Skip to main content
基本 Svelte
介绍
反应性
属性
逻辑
事件
绑定
类和样式
动作
转换
高级 Svelte
高级反应性
重用内容
运动
高级绑定
高级转换
上下文 API
特殊元素
<script module>
后续步骤
基本 SvelteKit
介绍
路由
加载数据
标题和 cookie
共享模块
表单
API 路由
$app/state
错误和重定向
高级 SvelteKit
钩子
页面选项
链接选项
高级路由
高级加载
环境变量
结论

服务器端渲染 (SSR) 是在服务器上生成 HTML 的过程,也是 SvelteKit 默认执行的操作。这对性能和 resilience 很重要,对搜索引擎优化 (SEO) 非常有益 - 虽然一些搜索引擎可以使用 JavaScript 索引在浏览器中渲染的内容,但这种情况发生的频率和可靠性较低。

¥Server-side rendering (SSR) is the process of generating HTML on the server, and is what SvelteKit does by default. It’s important for performance and resilience, and is very beneficial for search engine optimization (SEO) — while some search engines can index content that is rendered in the browser with JavaScript, it happens less frequently and reliably.

也就是说,某些组件无法在服务器上渲染,可能是因为它们希望能够立即访问浏览器全局变量(如 window)。如果可以,你应该更改这些组件,以便它们可以在服务器上渲染,但如果不能,则可以禁用 SSR:

¥That said, some components can’t be rendered on the server, perhaps because they expect to be able to access browser globals like window immediately. If you can, you should change those components so that they can render on the server, but if you can’t then you can disable SSR:

src/routes/+page.server
export const ssr = false;

在根 +layout.server.js 中将 ssr 设置为 false 可有效地将整个应用变成单页应用 (SPA)。

¥[!NOTE] Setting ssr to false inside your root +layout.server.js effectively turns your entire app into a single-page app (SPA).

上一页 下一页
1
2
<h1>{window.innerWidth}x{window.innerHeight}</h1>