kit: return fail() fails with Error: Data returned from action inside / is not serializable: Cannot stringify arbitrary non-POJOs
Describe the bug
As of sveltekit 1.0.13, returning a fail() object from an Action fails with the following error when using npm run dev
Error: Data returned from action inside / is not serializable: Cannot stringify arbitrary non-POJOs
Everything worked fine in 1.0.12. Also works as expected using npm run build
/ npm run preview
Reproduction
+page.server.js
import { fail } from '@sveltejs/kit';
/** @type {import('./$types').Actions} */
export const actions = {
test: async () => {
return fail(500);
},
};
+page.svelte
<form method="POST" action="?/test">
<button>Submit</button>
</form>
Logs
Error: Data returned from action inside / is not serializable: Cannot stringify arbitrary non-POJOs
at try_deserialize (/node_modules/@sveltejs/kit/src/runtime/server/page/actions.js:262:10)
at stringify_action_response (/node_modules/@sveltejs/kit/src/runtime/server/page/actions.js:244:9)
at Module.handle_action_json_request (/node_modules/@sveltejs/kit/src/runtime/server/page/actions.js:67:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async resolve (/node_modules/@sveltejs/kit/src/runtime/server/respond.js:358:17)
at async Module.respond (/node_modules/@sveltejs/kit/src/runtime/server/respond.js:229:20)
at async file:///C:/Users/geoff/slingshot/slingshot-webui/frontend/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:468:22
System Info
System:
OS: Windows 10 10.0.19045
CPU: (12) x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
Memory: 33.87 GB / 63.95 GB
Binaries:
Node: 19.3.0 - C:\Program Files\nodejs\node.EXE
npm: 9.3.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: 109.0.5414.74
Edge: Spartan (44.19041.1266.0), Chromium (108.0.1462.76)
npmPackages:
@sveltejs/adapter-auto: ^1.0.0 => 1.0.0
@sveltejs/adapter-node: ^1.0.0 => 1.1.1
@sveltejs/kit: ^1.0.12 => 1.1.0
svelte: ^3.46.0 => 3.55.1
vite: ^4.0.0 => 4.0.4
Severity
blocking an upgrade
Additional Information
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 21 (6 by maintainers)
Commits related to this issue
- fix: check for wrong return values from form actions Related to #8523 — committed to sveltejs/kit by dummdidumm a year ago
- fix: check for wrong return values from form actions (#8553) * fix: check for wrong return values from form actions Related to #8523 * Update packages/kit/src/runtime/server/page/actions.js ... — committed to sveltejs/kit by dummdidumm a year ago
- fix: reapply exports alignment ...after Vite dependency optimizations fixes #8523 — committed to sveltejs/kit by dummdidumm a year ago
- fix: reapply exports alignment (#8690) * fix: reapply exports alignment ...after Vite dependency optimizations fixes #8523 * Update packages/kit/src/exports/vite/dev/index.js Co-authored-by... — committed to sveltejs/kit by dummdidumm a year ago
@blerrgh I’m not sure if you’re still looking into this, but I had the same issue. I found that updating all packages
npm update
updateddevalue
to4.2.2
. This should have fixed it, but I still kept getting the error. Once I deleted the.svelte-kit
folder, and restarted the server, it fixed itThank you @blerrgh for the reproduction repo! I have a fix PR up that hopefully fixes this 🙏
I’m also seeing this same behaviour when the issue occurs. I can’t reproduce the error consistently, but when it does pop up it comes from a
throw redirect()
statement which somehow makes it into thehandleError
function.In my logs I see (from
console.error('SERVER ERROR:\n', error);
)Thanks @bobthered, removing the
.svelte-kit
folder solved this issue for me as well.I’ve had the same problem infrequently, but it usually went away after restarting VS Code. But more frequently I have had a similar problem with
redirect
in+page.server.ts
files.After throwing a redirect, it won’t get caught but instead goes to the
handleError
function inhooks.server.ts
, instead of redirecting. Restarting the dev server makes it go away, but it appears again frequently enough now to be annoying.Stack trace:
There could be some correlation with the dependency optimization and SSR compilation that
vite
andvite-plugin-svelte
are doing, but I’m not sure.@Yiak you cannot
return redirect(..)
, that doesn’t do a redirect, it will try to serializeredirect
as a value, which is why you get that error.Shot in the blue why this might have happened: We changed how things are resolved under the hood in #8429, and that could have led to
instanceof
checks failing inside the server runtime, leading to wrong code paths being chosen, ultimately resulting in this error. I’m not sure though why this has an effect in dev mode.Check if you are returning any values in
return fail()
that cannot be serialized. A common one isFile
if your form handles multipart file uploads.https://github.com/blerrgh/sveltekit-devalue-not-serializable-repro try this. Steps to reproduce are in the readme.
I spent some time this morning trying to get a minimal reproduction. The key seems to have something to do with vite optimizing dependencies. I couldn’t get it to work unless I added a random dependency to package.json (svelte-headless-table in this case, but I don’t think it matters specifically what the package is.)
Fail() and throw() work fine until I see
And then they start erroring. Re-running
npm run dev
usually fixes the issue until it optimizes dependencies again.Let me know if you’re able to reproduce it.
(Also, I am on windows 10 – haven’t tested this under linux.)