Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly serialize undefined in vuex store #3913

Merged
merged 6 commits into from Sep 25, 2018

Conversation

aldarund
Copy link

@aldarund aldarund commented Sep 18, 2018

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

Resolves #3825
serialize-javascript dont serialize undefined values.
yahoo/serialize-javascript#35
This PR use https://github.com/Rich-Harris/devalue for json serialization instead. JSON.stringify not used to avoid XSS

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (PR: #)
  • I have added tests to cover my changes (if not applicable, please state why)
  • All new and existing tests passed.

@codecov-io
Copy link

codecov-io commented Sep 18, 2018

Codecov Report

Merging #3913 into dev will decrease coverage by 0.08%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev    #3913      +/-   ##
==========================================
- Coverage   97.56%   97.48%   -0.09%     
==========================================
  Files          18       18              
  Lines        1232     1232              
  Branches      337      337              
==========================================
- Hits         1202     1201       -1     
- Misses         29       30       +1     
  Partials        1        1
Impacted Files Coverage Δ
lib/builder/builder.js 96.31% <ø> (ø) ⬆️
lib/core/renderer.js 100% <100%> (ø) ⬆️
lib/core/nuxt.js 94.16% <0%> (-0.84%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 55a153c...c0a5ed4. Read the comment docs.

@aldarund aldarund changed the title Properly serialize undefined in vuex store fix: properly serialize undefined in vuex store Sep 19, 2018
@manniL
Copy link
Member

manniL commented Sep 20, 2018

Thanks for you effort @aldarund ☺️

If devalue provides more value (no pun intended) and is a drop-in replacement for serialize-javascript I'd advocate to fully replace serialize-javascript with it.

  • No inconsistencies
  • Better support of common structures
    • Map
    • Set
    • undefined
    • cyclical references,
    • repeated references

@manniL
Copy link
Member

manniL commented Sep 20, 2018

We should also keep in mind that devalue does not support function serialization and has no plan to add this.

@aldarund
Copy link
Author

aldarund commented Sep 20, 2018

@manniL i did replaced serialize-javascript everywhere where it was used for json. As i see in other places it is used for javascript serialization ( at least calls to serialize-javascript dont have isJson argument) so it probably cant be replaced entirely

@galvez galvez added post-2.0 and removed post-2.0 labels Sep 20, 2018
@Atinux
Copy link
Member

Atinux commented Sep 24, 2018

Could you fix conflicts @aldarund ?

@aldarund
Copy link
Author

conflicts fixed

# Conflicts:
#	package.json
#	yarn.lock
@pi0
Copy link
Member

pi0 commented Sep 25, 2018

Waiting to CI passing :)

@Atinux Atinux merged commit df148a8 into nuxt:dev Sep 25, 2018
@appinteractive
Copy link

Hey the new option kills all methods (like formatters for tables) when serializing. That’s intended right?

@aldarund
Copy link
Author

@appinteractive not quite follow. What u do mean by methods? Functions? If functions they were killed before as well
See here
https://runkit.com/5baa3cac92a46f00121e2985/5babafa1191da800121dc83f

@benfavre
Copy link

benfavre commented Oct 1, 2018

Getting isues with this now.

✖ error NuxtServerError: Cannot stringify a function
at walk (xxx/node_modules/devalue/dist/devalue.umd.js:16:19)
at xxx/node_modules/devalue/dist/devalue.umd.js:49:72
at Array.forEach ()
at walk (xxx/node_modules/devalue/dist/devalue.umd.js:49:40)
at xxx/node_modules/devalue/dist/devalue.umd.js:49:72
at Array.forEach ()
at walk (xxx/node_modules/devalue/dist/devalue.umd.js:49:40)
at xxx/node_modules/devalue/dist/devalue.umd.js:49:72
at Array.forEach ()
at walk (xxx/node_modules/devalue/dist/devalue.umd.js:49:40)
at devalue (xxx/node_modules/devalue/dist/devalue.umd.js:53:5)
at Renderer.renderRoute (xxx/node_modules/nuxt/dist/nuxt.js:2042:50)
at process._tickCallback (internal/process/next_tick.js:68:7)

@manniL
Copy link
Member

manniL commented Oct 1, 2018

@benfavre can you provide us your state? Likely there is a function inside

@benfavre
Copy link

benfavre commented Oct 1, 2018

image

@benfavre
Copy link

benfavre commented Oct 1, 2018

apparently we have some new i18n issues now 💃
Stupid fix for now :
image

@manniL
Copy link
Member

manniL commented Oct 1, 2018

@benfavre Could you open up an issue here ?

@manniL
Copy link
Member

manniL commented Oct 1, 2018

@benfavre According to the source code messages should never be a function, only an object 🤔

@benfavre
Copy link

benfavre commented Oct 1, 2018

This is because I use the Lazy option.
https://nuxt-community.github.io/nuxt-i18n/lazy-load-translations.html

@manniL
Copy link
Member

manniL commented Oct 1, 2018

@benfavre But even with lazy it should await the promise properly (source)

@benfavre
Copy link

benfavre commented Oct 1, 2018

apparently not, nothing special in my lang files

image

image

image

@manniL
Copy link
Member

manniL commented Oct 1, 2018

Weird. Could you open up a new issue with reproduction as request (here)? Let's continue the discussion there.

@benfavre
Copy link

benfavre commented Oct 1, 2018

https://cmty.app forces a repro link ... randomly pasted jsfiddle (probably going to get many of theses)

@vhf
Copy link

vhf commented Oct 3, 2018

I think this should have been a breaking change, I rolled back to nuxt<2.1.0 because my project throws Cannot stringify arbitrary non-POJOs from devalue now. ;)

(Just a heads up, I'm not complaining, thanks for the awesome work!)

Repro is basically this: https://github.com/Rich-Harris/devalue/blob/d73708c51f70bf9b962103976b44e782b1e3a068/test/test.ts#L89-L93 , putting foo in nuxt's vuex store.

I know what I'm doing is weird and I would totally understand if nuxt doesn't want to support that kind of hacks. I'm storing stuff that I only need server-side and stuff that I only need client-side. These are complex values and it works well as long as I put them in the store and retrieve them on the same side. Before devalue was added to this project nuxt was simply deserializing into something unusable (as expected since state is passed between client/server as JSON, therefore dropping prototype infos, methods on objects, etc) but I didn't care anyway as long as it wasn't crashing the server (or even showing a warning for that matter), which is now the case. 😄

@manniL
Copy link
Member

manniL commented Oct 3, 2018

@vhf We are working on replacing the error with "just" a warning to revert the breaking change.

@lock
Copy link

lock bot commented Nov 2, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 2, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Computed value that depends on a state in Vuex store with initial value 'undefined' lose reactivity
10 participants