-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
API: Next and Previous post #4262
Comments
I'm curious if it would be possible to also get next/previous for posts in tags as well? It'd make it possible to iterate over posts the same way say Medium does it with their collections, so when you've scrolled to the bottom of a post in the tag |
That'd sure be cool. " |
@sondr3 this is a potential upgrade / improvement / extension to the behaviour described by this issue but is not expected. For now, next/previous refers only to the global chronological list. |
Starting to work on this one. |
Progress update. I've hit 2 issues so far. One in bookshelf and one in knex. to get the next post I wanted to extend the query like this: First problem: knex doesn't support subqueries with any operator other than 'in'. There is no way that I'm aware of to generate this in knex: Second problem: bookshelf doesn't allow me to relax constraints on generated query. There's clearly an easier path to the solution that I don't see yet. But I'm new to all three: knex, bookshelf & ghost, so oh well, the battle goes on. |
@ekulabuhov not sure if it will help, but there was a previous PR which had a set of queries which worked: https://github.com/TryGhost/Ghost/pull/1545/files. The main reason for not merging was that it was before we had the concept of include?= in our API and the queries would have been called every single time you fetched a post. |
@ErisDS thank you, I saw that. My main concern about that implementation is that it will do 3 requests to the database instead of one. But maybe that's okay for the first version. |
closes TryGhost#4262 - implementation based on TryGhost#1545 - added integration test. Modified mocked posts because code requires published_at timestamps to be different. - fixed 2 broken tests that depended on mocked posts to have "new Date()" as their timestamps - added checks to only query db if next/previous post requested
Many moons ago, #529 was raised to describe a future in which theme developers were able to access information about the next and previous post, according to the chronological order which posts are published on the blog.
Back then, bookshelf didn't support subqueries, we didn't have a clear plan for the API, and we didn't have any idea about having a query helper. All of these things combined to make this issue quite tricky to solve.
The solution that was put forward was an always-on addition to the API which meant making 2 extra queries for every post page. This seemed like unnecessary overhead for the
GET /posts/
request for a feature that wouldn't be used by everyone. Not having performance tests made it riskier.Now that we have a clearer idea of what our API is supposed to look like (in short, JSON API but with inline objects rather than
linked
), it is much easier to design clean solutions to problems like this one. The proposal for adding next & previous support to the API is to update the posts endpoint to acceptnext
andprevious
asincludes
E.g.GET
/posts/?include=next,previous
It should be possible to add one or both of these options, and get the corresponding post returned in the response in the following format:
Implementing this at the model level is going to require some fancy bookshelf-ery. May be worth using the subquery support here. Performance testing would be very much appreciated.
Notes:
next
is the post published after the current post, so should be more recentprevious
is the post published beforenext
orprevious
posts are requested but don't exist because we are at one end of the list or another, the key should be included, but the body should be null.next
andprevious
keys with the id of the post, such that adding them toinclude
causes the whole object to be included, rather than the keys to magically appear. This definitely needs perf tests and will likely be a separate issue.The text was updated successfully, but these errors were encountered: