Pagination with rel=“next” and rel=“prev” (2011)
webmasters.googleblog.com
webmasters.googleblog.com
Opera seemed like the only browser vendor that truly championed next/prev.
I only do it now as a habit because of Opera - and, since switching to Chrome after Opera's nadir, for accessibility, regardless of whether screenreaders respect it. But I probably wouldn't know about it if not for Opera.
It's both wistful and aspirational to use them, because this is the way browsers were supposed to work - as was generally the case with most things Opera, of course.
edit: While looking for the addon at addons.mozilla, I find that it has been discontinued by Evernote: 'This add-on has been removed by its author.' https://addons.mozilla.org/en-GB/firefox/addon/clearly/
Anyone know of another addon that works similarly?
There are extensions/bookmarklets available for other browsers at https://www.readability.com.
Personally, I've been reading long form and paginated content using the readability Send to Kindle bookmarklet which works great.
Fewer webpages where this would be useful now for me. Most either have infinite scroll, or forums/comments are in a complex tree-structure instead of pages.
Neither are blogs. Granted, Medium is making a complete mess of the semantic web, but all the more reason for them to support a semantic standard that might guide hapless readers such as myself to find their way.
For the most part, the "change" that "obviates" the standard are developers writing shitty code with all kinds of JavaScript nonsense that wreak havoc on accessibility, load, and UX. It's a little like saying right-clicking and using the back button in the browser aren't needed anymore, because dumb new frameworks are breaking them. :)
Dear lord, I just remembered Twitter's hashbang urls. To this day, they still break my Pinboard bookmarks.
If it's not present we start, like opera did, analyzing links, which is a little messy.
But also consider websites such as forums that allow users to post custom HTML to a page. Although there is often some sanitisation, that often doesn't stretch to removing attributes from <a> tags. In those cases, it might be possible for a user to hijack the sequential links and make them point to some other website.
[1] https://www.w3.org/TR/html/links.html#sequential-link-types
The next keyword may be used with link, a, and area elements
of course it is allowed to put it in <link>. And if your badly sanitized HTML (which hopefully did strip the malicious onClick handler ;)) includes an <a rel="" href="">, are we sure that parsers will prefer the <link> in the header? Or will Google and others actively ignore the <a>, despite the standard allowing it on both? That would be useful to know.
For simple bots putting it in an easy-to-discover link in <head> might be good, but a search engine and a browser both have to parse the entire page anyways...
There's a whole lot of advantages that follow, but aren't quite reasons why (e.g. like a physical book, I don't need to read the page itself to figure out where the next page is), but it follows from applying the semantics of webpage (meta) data in a consistent manner.
(except for a CYOA book)
I don't know whether Google sends you there, or whether the site redirects you from the "view all" page to the first page, but the result is pretty annoying.
[1]: https://developer.github.com/guides/traversing-with-paginati...
[2]: https://github.com/backbone-paginator/backbone.paginator
I know there are a bunch of gotcha's to look out for
On the other hand, you could have an "articles" table and a "pages" table with the relationship:
articles 1 <--> 1..* pages
Each page's text is stored as a separate row in the "pages" table, along with the article ID and a page number. When a page is requested, only the page that matches the requested article's ID and the appropriate page number. The database schema here is more complex, but is likely to be more efficient.Whichever method you choose, the software will need to be able to transform the data between two formats: one suitable for the user interface, and one suitable for the database.
However you designed the UI, it would be possible to use either approach for the database. For example, suppose the UI used a single textarea and had a special notation for delimiting pages. For the first approach, the contents of the textarea can simply be stored in its own field. For the second approach, the software would take the contents of the textarea, split it into its individual pages, then store each page separately.
If the UI had separate textareas for each page, then for the first approach, the software combines the contents of the textareas and puts in delimiters. For the second approach, each textarea is mapped to their own database field.
I suppose some of combinations are easier for the programmer than others, but so long as the UI is designed to be easy to use, the user need never know how the article is actually represented in the database.
limit = page_size
offset = (current_page - 1) * limit
For example, let's say you have a table with 1000 products in it, and you want to display page 3 with a page size of 50. Here's how you would write it: SELECT *
FROM products
LIMIT 50
OFFSET 100At which point you could assign each post in a topic an indexed `position` column that always increases from 0 within that topic, which lets you jump to arbitrary pages and parameterize perPage.
fromPos = (page - 1) * perPage
SELECT *
FROM posts
WHERE topic_id = $1
AND position >= fromPos
AND position < fromPos + perPage
ORDER BY id
For simpler needs, your "Next" button could load items that come after the last ID on the current page but with a LIMIT. <ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
<a href="/items?afterId={{ items[items.length - 1].id }}">Next</a>
SELECT *
FROM items
WHERE id > $afterId
LIMIT $perPage
That's fast and really easy.The top-level comment is asking for good ways to paginate. OFFSET works on localhost and with hundreds of rows. My solution is one that works in production once OFFSET fails you. For me, it was day 2.
You don't need to heapsort the entire dataset before you offset to a page, and there are engines capable of doing this.
I empirically know this was an issue back then. I also agree it's something a DB engine should handle, and I do hope that databases do a better job today.
So instead of calling out "premature optimization", maybe instead come with a practical example of a DB engine that properly handles this?
This is very efficient when your table is indexed by that field; you can seek directly to the next record.
Some database APIs do this for you!
I wrote up my experience implementing this on App Engine here, http://johntantalo.com/blog/paginating-with-bookmarks-in-app...
For NoSQL, specifically (only?) CouchDB, you start iterating from a given key (in SQL, this would effectively be "WHERE id >= $start ORDER BY id ASC LIMIT n").
This difference is why I would recommend, especially for paginated REST APIs, to provide links to the next/prev pages, so API clients can follow these links without thinking about how pagination is accomplished exactly. To prevent people from guessing the inner workings and working around your links, you can encode the parameters in a "cursor" (so instead of ?pagesize=20&page=10 you could have ?cursor=base64encode("20,10") (SQL) or ?cursor=base64encode("$startKey") (NoSQL)). IIRC Facebook does it this way. Encrypting or authenticating the cursor is IMHO overblown here, given cursor values can be easily validated and rejected if fiddled with.
Using an opaque cursor also gives you the ability to change how pagination works without breaking existing API clients.
If you plan for lots of data and many pages, think twice before outputting links to all pages (on a website) or outputting the total number of elements (in an API). COUNT(*) on InnoDB is slow and I heard it's not the quickest thing in NoSQL databases either.
Forcing artificial page-breaks is abusive.
Yes, don't unnecessarily break up content. But most pages are going to include lists of some sort that you want to be paginated in some way.
If you look at HNs /new queue, there are submission IDs in the next links, so you can click "more" multiple times without seeing content multiple times, even if new submissions have pushed them down in the global list by now. Useful if there are many updates and new entries come in at the "top" of the list.
Essentially, once your rows are ordered, your next page is "rows > the last row on the previous page, limit <number of items to return>"
This has the fantastic benefit of being index-friendly, which OFFSET is not, and if you structure the URLs for the pages well, obvious what you're getting back. (e.g., if your records are by date, you might get /daily-reports?since=2015-01-04&limit=50 for the page that starts at 2015-01-04)
It's from an SQL site, but the concept is not specific to SQL. I'm working on an implementation of it for data stored in Mongo, at the moment.
http://use-the-index-luke.com/sql/partial-results/fetch-next...
edit: more on this via a slideshare presentation or pdf:
http://use-the-index-luke.com/blog/2013-07/pagination-done-t...
You will also end up losing your reading position after a browser restart or on iOS where Mobile Safari reloads a page to save memory.
It's a dumb, pointless feature that at the very least shouldn't be the default option. I'm sure there are hypothetical scenarios that may or may not warrant it, but it is a pain and a half for the user. Tumblr blogs have it in spades, and it's a miserable experience to browse.
That said, the problem of losing your reading position can be solved using something like history.replaceState(). As the user scrolls, update the history entry with a reference to the current reading location. Then, when the browser restarts, the website can know where the user was previously at and can return them to that location. This is the approach Discourse uses [1]. Alternatively, I suppose cookies could be used, but that's a slightly messier solution, imo.
Unfortunately, I haven't seen many websites adopt such a feature. Which is a shame because infinite scroll can make for a great user experience -- if done right. But too many websites actually suffer as a result of poorly implemented infinite scroll.
[1] https://eviltrout.com/2013/02/16/infinite-scrolling-that-wor...
Infinite scroll has other UX problems. For example, you cannot see the footer of a page or cannot navigate to the items by page number. As there is no page numbers you cannot even remember how to find some item.
Regarding footers, who's to say the footer needs to go beneath the infinite scroll area? You could instead place the contents of the footer in a sidebar that stays in a fixed position. Alternatively the footer could be overlayed at the bottom of the page so it is always visible (a recent design trend would have it hide when the user scrolls down, but reappear when the user scrolls up).
As for page numbers, there's nothing stopping you from adding them. Each item should probably have some sort of permalink to allow you access it directly, and there should be suitable navigation to allow you to find individual items.
I find it quite sad that infinite scroll gets a bad rap due to poorly designed implementations. Perhaps it will get a better reputation if more websites actually put some thought into how infinite scroll is used.
- usually breaks the back button (e.g. scroll to the 20th page, click something, go back. welcome to page 1!)
- usually breaks links (how do you show someone things-on-page-10?) (fixable by tweaking the URL as you go, but nothing is really consistent + predictable by non-technical users)
- usually performs hideously on low-power machines (e.g. mobile) due to memory growth (there are techniques, but few use them). can even tank powerful machines in time.
- footers. headers. etc.
This is easily overcome by pushState or what have you. As you scroll, the URL in the address bar changes to like `/page/20/` and then going back takes you to that URL where only the results from page 20 are displayed.
Another approach is with "virtual scrolling", by preserving space for the items above and loading whatever you scroll to - I've seen that once (I forget where) and it was really nice. They didn't tackle the linkability problem tho, and had other issues.
But all of this is fairly complex, and very few sites (or apps!) actually do so. Which is part of the problem.
I sometimes hate it (e.g. in facebook because it bluntly breaks the scrollbar), but at other times I miss it (e.g. in gmail).
Amazing how nice it is on the phone, but how broken it is (because of modal inconsistencies, the way you'll eventually have to press your browser back button) on the web version.
Similar is google maps. If you make it a large canvas where the missing data is paged in then it becomes quite natural.
Salient points seem to be:
# Update URL during scrolling so links don't break, and link to the "deep" item not just the first page.
# Don't break history, so back/forward/reload buttons act as would be expected.
# Truly endless scrolling kills browsers due to consuming increasing resources, so don't do it. Seems that a compromise might be: larger page sizes that lazy-load content up to a sensible max length. For example, in a data set of 1,000 results perhaps display the first 10 immediately and lazy-load up to 50 during scrolling. Then normal paging occurs. (Max items per page could be user-selected, as is currently seen sometimes.) Items that have scrolled off the top of the page could be removed from the DOM too, and lazily reloaded on demand.
# Endless scrolling is not always appropriate, but there may be appropriate use cases. It would be good to identify these cases, perhaps through user testing.
# Obviously, it shouldn't break screen readers or text-only browsers etc. These should be handled gracefully.... which raises the question of how to effectively navigate through a large list, possibly of unknowable length, in these browsers.
I feel sure I've seen various of these, but not all together in one implementation. Anyone seen somewhere that does all this well?
Any further suggestions/criticisms of this pattern?
I think I might start overriding the existing methods which users are used to with this method because I've build websites for a couple of years and think I know better than years of UI development by much more experienced people than I.
It would be nice to know if we're making progress on problems like this. Paging and scrolling both evolved to solve problems. Perhaps they're imperfect, or could stand improvement. Perhaps not. Regardless, an open-minded discussion would probably shed some light on current thinking.