Content: Hook backend APIs for stars/comments/reads

Created on 9 Aug 2020  路  6Comments  路  Source: nuxt/content

Hi all,

I'm new to nuxt content and I am trying to build a personal blog with stars / comments / reads. Since nuxt content is generating static files, how can make it work with an express backend for storing stars / comments / reads?

I'm planning to use express talk to a mongo db for storing the stars / comments / reads.

One way I can think about is to use slug as the identifier of an article, and fetch db using this as article id. However if I end up changing the name of the slug, all the backend data will be messed up.

Is there a way the nuxt content apis can call my backend apis to decorate the response?

Any suggestions will be very helpful!

question

All 6 comments

You can use something like this:

  hooks: {
    'content:file:beforeInsert': (document) => {
      if (!document.postid) {
        document.postid = randomID()
      }
    }
  }

You can use something like this:

  hooks: {
    'content:file:beforeInsert': (document) => {
      if (!document.postid) {
        document.postid = randomID()
      }
    }
  }

I tried this approach but the randomId will be changing everytime I update the content of the .md file.

Couple of ideas:

  • don't change the name of the slug once it's posted. This is the simplest and is what I do - my naming convention is YYYY-MM-DD-my-blog-title so it's very easy to make sure they are never duplicated.
  • use the full createdAt timestamp as the id. It would be really rare to end up with the exact same timestamp on the md file, even if you're checking in a few articles at the same time. And even if you overrode createdAt, I would suggest it would be rare to publish two blog articles on the same day
  • use an md5 hash of the file

Hi @davydnorris Thanks for your reply! I'm curious about md5 hash of the file. Will the md5 hash change if the file content change?

@kevinwchn you can set an ID directly in the front matter of you Markdown file so it won't change

I guess I am late at this but seeing the issue open I just though of sharing my point of view as well..
Just like Atinux said, you can include a unique ID in the front matter and use it. THATS THE WAY FOR YOU TO GO

I have done something similar with my personal bog as well. but instead of setting id in front matter I have used the slug as the unique id itself (because I am naming every file uniquely)

https://github.com/TheLearneer/santoshb.com.np/blob/master/pages/blog/_slug.vue#L75
https://github.com/TheLearneer/santoshb.com.np/blob/master/pages/blog/_slug.vue#L121-L127

Also I am using disqus commenting system but still everything you are telling accounts.

Was this page helpful?
0 / 5 - 0 ratings