-
-
Notifications
You must be signed in to change notification settings - Fork 434
[question] use of modules/classes in preload #112
Comments
@samhatchett can you use |
good idea - though I'm not sure where to call For now, I am going to test for class-ness in the computed property and no-op if the expected functions aren't there, then reconstruct the classes in If I would say anything from this adventure, it would be that perhaps a mention in the documentation would be in order. Not sure how many other people will try to use classes in this way. |
This came up in Gitter just now — basically Sapper is trying to serialize the data so |
Just released a package that should help with this: https://github.com/rich-harris/devalue It will throw an error if it encounters a POJO, which allows us to bail (meaning the preloading will happen again on the client). It also means we can now serialize dates, regexes, Maps and Sets and so on. |
use devalue instead of serialize-javascript
This should work as of 0.9 — thanks |
I think this issue has to do with the serialization/deserialization needed for the preload mechanism.
What I'm trying to do is use imported classes in my svelte component. I'd like to get some data and construct my objects in
preload
- this seems to work when preload is called in the browser, but of course fails when the server preloads, and sends the data to the browser. The browser just gets the json representation devoid of functions.Is there a good place to intercept the server-preloaded content to ensure that I can
Object.assign
that data back into my real objects? Or is it enough to trust the server-rendered content for the first load, and just try/catch to gracefully detect when the objects are not what I expect?Hope this makes sense - just in case i have a demonstration here, based on the template project.
https://github.com/samhatchett/sapper-module-preload
The text was updated successfully, but these errors were encountered: