最佳實務
SEO
在 GitHub 上編輯此頁面SEO 最重要的面向是建立高品質的內容,並廣泛地連結至網路上的各處。然而,對於建立排名良好的網站,有一些技術上的考量。
開箱即用永久連結
SSR永久連結
雖然搜尋引擎在近年來已能更有效地索引使用客戶端 JavaScript 呈現的內容,但伺服器端呈現的內容能更頻繁且可靠地被索引。SvelteKit 預設採用 SSR,雖然你可以在 handle
中停用它,但除非你有充分的理由,否則你應該保持啟用狀態。
SvelteKit 的呈現高度可設定,而且你可以在必要時實作 動態呈現。通常不建議這麼做,因為 SSR 除了 SEO 之外還有其他好處。
效能永久連結
諸如 核心網路指標 等訊號會影響搜尋引擎排名。由於 Svelte 和 SvelteKit 產生的額外負擔很小,因此建立高效能網站會更容易。你可以使用 Google 的 PageSpeed Insights 或 Lighthouse 來測試你的網站效能。請閱讀 效能頁面 以取得更多詳細資訊。
正規化網址永久連結
SvelteKit 會將帶有尾斜線的路徑名稱重新導向到不帶尾斜線的路徑名稱(或反之,這取決於您的設定),因為重複的網址對 SEO 不利。
手動設定永久連結
<title> 和 <meta>永久連結
每個頁面都應該在 <svelte:head>
內部有寫得好的且獨特的 <title>
和 <meta name="description">
元素。關於如何撰寫描述性標題和說明的指南,以及其他關於讓搜尋引擎了解內容的建議,可以在 Google 的 Lighthouse SEO 審核 文件中找到。
一個常見的模式是從頁面
load
函式傳回與 SEO 相關的data
,然後在根部 版面 的<svelte:head>
中使用它(作為$page.data
)。
網站地圖永久連結
網站地圖 能幫助搜尋引擎優先處理您網站中的頁面,特別是在您有大量內容時。您可以使用端點動態建立網站地圖
ts
export async functionGET () {return newResponse (`<?xml version="1.0" encoding="UTF-8" ?><urlsetxmlns="https://www.sitemaps.org/schemas/sitemap/0.9"xmlns:xhtml="https://www.w3.org/1999/xhtml"xmlns:mobile="https://www.google.com/schemas/sitemap-mobile/1.0"xmlns:news="https://www.google.com/schemas/sitemap-news/0.9"xmlns:image="https://www.google.com/schemas/sitemap-image/1.1"xmlns:video="https://www.google.com/schemas/sitemap-video/1.1"><!-- <url> elements go here --></urlset>`.trim (),{headers : {'Content-Type': 'application/xml'}});}
ts
export async functionGET () {return newResponse (`<?xml version="1.0" encoding="UTF-8" ?><urlsetxmlns="https://www.sitemaps.org/schemas/sitemap/0.9"xmlns:xhtml="https://www.w3.org/1999/xhtml"xmlns:mobile="https://www.google.com/schemas/sitemap-mobile/1.0"xmlns:news="https://www.google.com/schemas/sitemap-news/0.9"xmlns:image="https://www.google.com/schemas/sitemap-image/1.1"xmlns:video="https://www.google.com/schemas/sitemap-video/1.1"><!-- <url> elements go here --></urlset>`.trim (),{headers : {'Content-Type': 'application/xml',},},);}
AMP永久連結
現代網頁開發一個不幸的現實是,有時有必要建立您網站的 加速行動網頁 (AMP) 版本。在 SvelteKit 中,這可以透過設定 inlineStyleThreshold
選項來完成...
ts
/** @type {import('@sveltejs/kit').Config} */constconfig = {kit : {// since <link rel="stylesheet"> isn't// allowed, inline all stylesinlineStyleThreshold :Infinity }};export defaultconfig ;
...在您的根部 +layout.js
/+layout.server.js
中停用 csr
...
ts
export constcsr = false;
ts
export constcsr = false;
...將 amp
新增到您的 app.html
<html amp>
...
...並使用從 @sveltejs/amp
匯入的 transform
,透過 transformPageChunk
轉換 HTML
ts
import * asamp from '@sveltejs/amp';/** @type {import('@sveltejs/kit').Handle} */export async functionhandle ({event ,resolve }) {letbuffer = '';return awaitresolve (event , {transformPageChunk : ({html ,done }) => {buffer +=html ;if (done ) returnamp .transform (buffer );}});}
ts
import * asamp from '@sveltejs/amp';import type {Handle } from '@sveltejs/kit';export consthandle :Handle = async ({event ,resolve }) => {letbuffer = '';return awaitresolve (event , {transformPageChunk : ({html ,done }) => {buffer +=html ;if (done ) returnamp .transform (buffer );},});};
為了防止在將頁面轉換為 amp 後傳送任何未使用的 CSS,我們可以使用 dropcss
ts
import * asamp from '@sveltejs/amp';importCannot find module 'dropcss' or its corresponding type declarations.2307Cannot find module 'dropcss' or its corresponding type declarations.dropcss from'dropcss' ;/** @type {import('@sveltejs/kit').Handle} */export async functionhandle ({event ,resolve }) {letbuffer = '';return awaitresolve (event , {transformPageChunk : ({html ,done }) => {buffer +=html ;if (done ) {letcss = '';constmarkup =amp .transform (buffer ).replace ('⚡', 'amp') // dropcss can't handle this character.replace (/<style amp-custom([^>]*?)>([^]+?)<\/style>/, (match ,attributes ,contents ) => {css =contents ;return `<style amp-custom${attributes }></style>`;});css =dropcss ({css ,html :markup }).css ;returnmarkup .replace ('</style>', `${css }</style>`);}}});}
ts
import * asamp from '@sveltejs/amp';importCannot find module 'dropcss' or its corresponding type declarations.2307Cannot find module 'dropcss' or its corresponding type declarations.dropcss from'dropcss' ;import type {Handle } from '@sveltejs/kit';export consthandle :Handle = async ({event ,resolve }) => {letbuffer = '';return awaitresolve (event , {transformPageChunk : ({html ,done }) => {buffer +=html ;if (done ) {letcss = '';constmarkup =amp .transform (buffer ).replace ('⚡', 'amp') // dropcss can't handle this character.replace (/<style amp-custom([^>]*?)>([^]+?)<\/style>/, (match ,attributes ,contents ) => {css =contents ;return `<style amp-custom${attributes }></style>`;});css =dropcss ({css ,html :markup }).css ;returnmarkup .replace ('</style>', `${css }</style>`);}},});};
使用
handle
鉤子來驗證轉換後的 HTML,使用amphtml-validator
是個好主意,但僅限於您在預先渲染頁面時,因為它非常慢。