question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow dynamic meta content

See original GitHub issue

@SeyZ - first off, thumbs up for a great framework-agnostic, lightweight and easy-to-use library 👍 Just what I’ve been looking for!

The only thing I find lacking at the moment is the ability to add dynamic meta data at the point of serialisation. At the moment, you can only add meta when creating the serialiser instance, and seems a bit unintuitive to recreate the serialiser every time I need to change the meta.

In my case, I’m looking to add some extra information regarding pagination, such as total count. In keeping with the spec, this type of additional information should live in the top-level meta. An example of this can be seen in the extensions section here, under Profiles: link

The easiest way I can think of is to add an optional opts param to the serialise method. You can then specify a meta value in opts, and on the plus side, this also serves as a nice container if you want to allow any other options for the serialise method in the future. We can then merge it with any existing meta from the serialiser instance like so:

module.exports = function (collectionName, records, opts) {
  this.serialize = function (records, opts) {
    var that = this;
    var payload = {};

    ...

    if (that.opts.meta) {
      payload.meta = that.opts.meta;
    }

    if (opts.meta) {
      if (payload.meta) {
        for (var i in opts.meta) {
          payload.meta[i] = opts.meta[i];
        }
      } else {
        payload.meta = opts.meta;
      }
    }

    ...

Just gave it a run-through, and seems to work well. If anybody seconds the approach, I’m happy to do a PR.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
SeyZcommented, Apr 1, 2017

Hey @allistercsmith @bradbumbalough @mrkvon @sazzer

I’ve added the possibility to set a function for the meta key: https://github.com/SeyZ/jsonapi-serializer/blob/master/test/serializer.js#L198

If you need a dynamic meta for a relationship, it was already implemented: https://github.com/SeyZ/jsonapi-serializer/blob/master/test/serializer.js#L1549

I think it’s fixed this issue, right? @bradbumbalough Hope you will come back here 👍 💃

0reactions
bradbytecommented, Mar 6, 2017

This was a requirement for my use case. Ended up switching to https://github.com/kutlerskaggs/json-api-ify as they provide a somewhat dynamic meta option.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding dynamic meta tags to a React app without SSR
If you want to set up dynamic meta tags based on different URLs in your project (e.g., /home , /about , /detail?id=1 ,...
Read more >
Dynamic META tags in HTML5 and Javascript/jQuery
You can't do dynamic meta tags (or well, you can, but Google and Facebook won't run your JavaScript so they won't do you...
Read more >
Advanced SEO Tips: Static or Dynamic Meta Tags? What's ...
Dynamic meta tags are quite similar to static ones with the difference that they are not set in stone, so to speak, but...
Read more >
Render Dynamic Title and Meta Tags in a Next.js App
Dynamic title and meta tags are those which changes with change in data. Usually, we add title and meta tags for each page....
Read more >
How to Configure meta tags in React Apps Dynamically with a ...
Let's say we want to set the meta tags of a page called 'Contact us', ... us</title> <meta name='description' content='Contact us if you ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found