frandiox / vite-ssr

Use Vite for server side rendering in Node
MIT License
830 stars 92 forks source link

[SSR] On state serialization - TypeError: Converting circular structure to JSON #35

Closed ondrej-langr closed 3 years ago

ondrej-langr commented 3 years ago

Hey comunity!

I found myself in weird position where I found something that may be a bug(?) - namely: When this plugin tries to stringify state (in utils/state.js) that is then injected to FE, the JSON.stringify throws and error that theres circular structure.

It throws error like this:

`[SSR] On state serialization - TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'something' -> object with constructor 'Object'
    |     property 'something' -> object with constructor 'Object'
    |     ...
    |     property 'something' -> object with constructor 'Array'
    --- index 0 closes the circle
    at JSON.stringify ()
    at serializeState (/node_modules/vite-ssr/utils/state.js:32:36)
    at eval (/node_modules/vite-ssr/vue/entry-server.js:64:36)
    at runMicrotasks ()
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async handleSsrRequest (C:\Users\Workgroup-L\Desktop\Work\radixal\mortage\mortgage\FE\node_modules\vite-ssr\dev\server.js:88:31)`

Should it use flatted package or have something like this?

Open to make a PR myself! Thanks.

frandiox commented 3 years ago

@daichi-ranguru Hi! The default serialization strategy assumes you have an object that can be passed to JSON.stringify. Circular references break JSON.stringify so you need to either fix that on your end (removing circular refs in your initialState object), or overwrite the default serialization strategy.

To overwrite it, there's a hook mentioned here where you can use something like devalue. Hope that helps!

ondrej-langr commented 3 years ago

@frandiox Hey! Glad to hear from you this quickly - writing my custom hook helped! Ill leave this here for future if someone gets this error too, but somehow didnt figure it out from docs.

Thanks again!