Content: `surround` to loop for first and last item

Created on 10 Jun 2020  路  3Comments  路  Source: nuxt/content

Is your feature request related to a problem? Please describe.

_Not sure if the title is very descriptive, but I failed to find better English phrasing :)_

I am fetching surrounding blog posts on a single post page. Currently when it's the first post, no previous post is returned (null is returned instead). Similar thing happens for the last post. This is expected and well documented in fetching content docs.

Describe the solution you'd like

I would love to have an option to add a wrap or loop param that - when set to true - would force surround to return a post. For example, when fetching surrounding posts for the last post, it would return the first post.

Example:

['post-1', 'post-2', 'post-3']

Current behaviour:

$content(...).surround('post-3').fetch() // returns [ post2, null]

Expected behaviour:

$content(...).surround('post-3', { loop: true }).fetch() // returns [post2, post1]

Describe alternatives you've considered

For the moment being I just check whether surround returned something and if not I backfill it manually.

enhancement

Most helpful comment

Not sure how useful this feature can be... _It is just my personal opinion_

Basically I would use this feature to display the previous and next article...
But the feature you requested will make things wiered...

If I am previewing the last article then the up next would be the first article ever..
Wouldn't that be misleading information ??

Also what if there is only 1 or 2 articles in total (_Most likely such things won't happen but think of it_)
If there is only 1 article your current, up next and the previous one would be the same...

Other than my personal opinions above, it is completely upto the developers to make the decision and pass the decision :smile:

All 3 comments

Not sure how useful this feature can be... _It is just my personal opinion_

Basically I would use this feature to display the previous and next article...
But the feature you requested will make things wiered...

If I am previewing the last article then the up next would be the first article ever..
Wouldn't that be misleading information ??

Also what if there is only 1 or 2 articles in total (_Most likely such things won't happen but think of it_)
If there is only 1 article your current, up next and the previous one would be the same...

Other than my personal opinions above, it is completely upto the developers to make the decision and pass the decision :smile:

Hey @LukaszRados,

Thank you for your suggestion, however I think this is a bit too specific to include in this module for now.

You could use a workaround by selecting how many prev and next you want in the surround options:

const [prev1, prev2, next1, next2] = await this.$content('articles')
  .surround('slug', { before: 2, after: 2 })
  .fetch()

Hi @benjamincanac,
it makes sense, thanks for having a look :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pi0 picture pi0  路  3Comments

uporot1k picture uporot1k  路  4Comments

giticon picture giticon  路  3Comments

jhull picture jhull  路  4Comments

pxwee5 picture pxwee5  路  4Comments