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

load 函数中(以及 表单操作hooksAPI 路由,我们稍后会了解),你可以访问 setHeaders 函数,不出所料,该函数可用于设置响应的标头。

¥Inside a load function (as well as in form actions, hooks and API routes, which we’ll learn about later) you have access to a setHeaders function, which — unsurprisingly — can be used to set headers on the response.

最常见的是,你会使用它来通过 Cache-Control 响应标头自定义缓存行为,但为了本教程的目的,我们将做一些不太可取且更具戏剧性的事情:

¥Most commonly, you’d use it to customise caching behaviour with the Cache-Control response header, but for the sake of this tutorial we’ll do something less advisable and more dramatic:

src/routes/+page.server
export function load({ setHeaders }) {
	setHeaders({
		'Content-Type': 'text/plain'
	});
}

(你可能需要重新加载 iframe 才能看到效果。)

¥(You may need to reload the iframe to see the effect.)

1
<h1>hello world</h1>