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

两个 URL(如 /foo/foo/)可能看起来相同,但实际上不同。像 ./bar 这样的相对 URL 在第一种情况下将解析为 /bar,在第二种情况下解析为 /foo/bar,搜索引擎会将它们视为单独的条目,从而损害你的 SEO。

¥Two URLs like /foo and /foo/ might look the same, but they’re actually different. A relative URL like ./bar will resolve to /bar in the first case and /foo/bar in the second, and search engines will treat them as separate entries, harming your SEO.

简而言之,对尾部斜杠过于宽松不是一个好主意。默认情况下,SvelteKit 会删除尾随斜杠,这意味着对 /foo/ 的请求将导致重定向到 /foo

¥In short, being loosey-goosey about trailing slashes is a bad idea. By default, SvelteKit strips trailing slashes, meaning that a request for /foo/ will result in a redirect to /foo.

如果你想要确保尾部斜杠始终存在,则可以相应地指定 trailingSlash 选项:

¥If you instead want to ensure that a trailing slash is always present, you can specify the trailingSlash option accordingly:

src/routes/always/+page.server
export const trailingSlash = 'always';

为了适应这两种情况(不推荐!),请使用 'ignore'

¥To accommodate both cases (this is not recommended!), use 'ignore':

src/routes/ignore/+page.server
export const trailingSlash = 'ignore';

默认值为 'never'

¥The default value is 'never'.

是否应用尾部斜杠会影响预渲染。像 /always/ 这样的 URL 将作为 always/index.html 保存到磁盘,而像 /never 这样的 URL 将保存为 never.html

¥Whether or not trailing slashes are applied affects prerendering. A URL like /always/ will be saved to disk as always/index.html whereas a URL like /never will be saved as never.html.

上一页 下一页
1
2
<h1>trailingSlash</h1>