Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

[question] use of modules/classes in preload #112

Closed
samhatchett opened this issue Feb 1, 2018 · 5 comments · Fixed by #188
Closed

[question] use of modules/classes in preload #112

samhatchett opened this issue Feb 1, 2018 · 5 comments · Fixed by #188

Comments

@samhatchett
Copy link
Contributor

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

@Crisfole
Copy link

Crisfole commented Feb 4, 2018

@samhatchett can you use observe here to watch for changes to the data points that should be further deserialized into a class? I'm pretty sure (although certainly not 100% sure) that observers get hit client side even after hydration.

@samhatchett
Copy link
Contributor Author

good idea - though I'm not sure where to call observe, since oncreate doesn't seem to be called until after the class methods are used in a computed property. Also changing an observed property from the observer's callback sounds like it would need some extra logic to un-observe and then re-observe.

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 oncreate which eventually gets called. This appears to work.

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.

@Rich-Harris
Copy link
Member

This came up in Gitter just now — basically Sapper is trying to serialize the data so preload doesn't need to run again on the client, which is an error in a situation like this. It should probably bail if it encounters a non-POJO.

@Rich-Harris
Copy link
Member

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.

Rich-Harris added a commit that referenced this issue Mar 11, 2018
Rich-Harris added a commit that referenced this issue Mar 11, 2018
use devalue instead of serialize-javascript
@Rich-Harris
Copy link
Member

This should work as of 0.9 — thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants