Skip to content

Use flatted instead of devalue to be able to revive objects #5117

@vhf

Description

@vhf

What problem does this feature solve?

It is sometimes useful to save complex data (i.e. not primitive types nor POJOs) in the store.

Store data is passed from server to client in serialized form using devalue. Using flatted instead of devalue, together with some configuration mechanism, would allow reviving objects that would then be usable client-side.

For instance developers would add revivers to the store and these would be called client-side to revive serialized values.

What does the proposed changes look like?

I created an example repo which consists of two npx create-nuxt-app: https://github.com/vhf/nuxt-flatted-feature/commits/master . It's a dumb example because I wanted to keep it minimal, in a real-world use case I'm storing things like RDF dataset objects ( https://github.com/rdf-ext/rdf-dataset-indexed ) (initialized during serverIniti based on server data/request data).

In this example I'm storing an object based on:

export default class Bar {
  constructor (id = Math.random()) {
    this.id = id
  }
  transform () {
    return this.id * 2
  }
}

and the goal is to do someBarInstance.transform() in the frontend. The two apps have the same index.vue, the basic one shows an error because .transform is not a function on the devalued object, the other one uses flatted to serialize and revive the object, properly displaying .transform()'s result.

This feature request is available on Nuxt community (#c8733)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions