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

Most upvoted comments

@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 updated devalue to 4.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 it

Thank you @blerrgh for the reproduction repo! I have a fix PR up that hopefully fixes this 🙏

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 in hooks.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:

at Object.handleError (/src/hooks.server.ts:55:13)
at Module.handle_error_and_jsonify (/node_modules/.pnpm/@sveltejs+kit@1.2.1_262zhp54rffy2bklrl2ynkhpha/node_modules/@sveltejs/kit/src/runtime/server/utils.js:149:25)
at Module.handle_action_json_request (/node_modules/.pnpm/@sveltejs+kit@1.2.1_262zhp54rffy2bklrl2ynkhpha/node_modules/@sveltejs/kit/src/runtime/server/page/actions.js:93:40)
at async resolve (/node_modules/.pnpm/@sveltejs+kit@1.2.1_262zhp54rffy2bklrl2ynkhpha/node_modules/@sveltejs/kit/src/runtime/server/respond.js:365:17)
at async eval (/node_modules/.pnpm/@lucia-auth+sveltekit@0.5.2_fec62evc6llxeegip47f74apjq/node_modules/@lucia-auth/sveltekit/server/hooks.js:87:16)
at async Module.respond (/node_modules/.pnpm/@sveltejs+kit@1.2.1_262zhp54rffy2bklrl2ynkhpha/node_modules/@sveltejs/kit/src/runtime/server/respond.js:236:20)
at async file:///.../node_modules/.pnpm/@sveltejs+kit@1.2.1_262zhp54rffy2bklrl2ynkhpha/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:467:22

There could be some correlation with the dependency optimization and SSR compilation that vite and vite-plugin-svelte are doing, but I’m not sure.

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 the handleError function.

In my logs I see (from console.error('SERVER ERROR:\n', error);)

SERVER ERROR:
 Redirect { status: 303, location: '/' }

Once I deleted the .svelte-kit folder, and restarted the server, it fixed it

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 in hooks.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:

at Object.handleError (/src/hooks.server.ts:55:13)
at Module.handle_error_and_jsonify (/node_modules/.pnpm/@sveltejs+kit@1.2.1_262zhp54rffy2bklrl2ynkhpha/node_modules/@sveltejs/kit/src/runtime/server/utils.js:149:25)
at Module.handle_action_json_request (/node_modules/.pnpm/@sveltejs+kit@1.2.1_262zhp54rffy2bklrl2ynkhpha/node_modules/@sveltejs/kit/src/runtime/server/page/actions.js:93:40)
at async resolve (/node_modules/.pnpm/@sveltejs+kit@1.2.1_262zhp54rffy2bklrl2ynkhpha/node_modules/@sveltejs/kit/src/runtime/server/respond.js:365:17)
at async eval (/node_modules/.pnpm/@lucia-auth+sveltekit@0.5.2_fec62evc6llxeegip47f74apjq/node_modules/@lucia-auth/sveltekit/server/hooks.js:87:16)
at async Module.respond (/node_modules/.pnpm/@sveltejs+kit@1.2.1_262zhp54rffy2bklrl2ynkhpha/node_modules/@sveltejs/kit/src/runtime/server/respond.js:236:20)
at async file:///.../node_modules/.pnpm/@sveltejs+kit@1.2.1_262zhp54rffy2bklrl2ynkhpha/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:467:22

There could be some correlation with the dependency optimization and SSR compilation that vite and vite-plugin-svelte are doing, but I’m not sure.

@Yiak you cannot return redirect(..), that doesn’t do a redirect, it will try to serialize redirect 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.

Am I the only one having a problem with this issue even with the latest sveltekit & vite ?

Check if you are returning any values in return fail() that cannot be serialized. A common one is File 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

11:08:56 AM [vite] ✨ new dependencies optimized: svelte-headless-table
11:08:56 AM [vite] ✨ optimized dependencies changed. reloading

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.)