You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SvelteKit 1.0.0升级后表单重定向报错:找不到default action

问题

在我的SvelteKit应用中,有一个使用表单action的登出表单,代码如下:

<form action="/login?/logout" method="POST">
    <button type="submit">Logout</button>
</form>

对应的action逻辑最后会执行重定向:

export const actions: Actions = {
    logout: async (event) => {
        // ...
        throw redirect(307, '/login');
    }
}

原本该逻辑正常运行,但升级到SvelteKit 1.0.0后,action虽能被调用,但重定向时出现错误:

Error: No action with name 'default' found

解决方法

SvelteKit 1.0.0对表单action的路由规则做了调整,只需修改表单的action属性即可解决问题:

  • 将表单的action="/login?/logout"改为action="/login/logout",也就是去掉路径中的问号,用路径层级的方式指定命名action:
<form action="/login/logout" method="POST">
    <button type="submit">Logout</button>
</form>
  • 你的actions对象中的logout方法无需修改,重定向逻辑可以正常保留。

原因说明

SvelteKit 1.0.0之前支持通过/?/actionName的查询参数方式调用命名action,但正式版发布后统一改为/actionName的路径式调用。旧方式提交后,重定向到/login时,SvelteKit会默认尝试寻找该页面的default action,而你的login页面只有logout命名action,因此触发了找不到default action的错误。

内容的提问来源于stack exchange,提问作者Nausika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 19:40:26