服务器端渲染 (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:
export const ssr = false;
在根
+layout.server.js
中将ssr
设置为false
可有效地将整个应用变成单页应用 (SPA)。¥[!NOTE] Setting
ssr
tofalse
inside your root+layout.server.js
effectively turns your entire app into a single-page app (SPA).
<h1>{window.innerWidth}x{window.innerHeight}</h1>