Pagination

Learn how to limit the data shown to the user per page.

Technically, we could say that the project is now finished, but we want to cover some essential details about optimization. The topics we will discuss in this chapter are the following:

  • Pagination
  • Caching
  • Optimization of SQL queries
  • Activation of CORSCross-Origin Resource Sharing (CORS) is used to allow a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.

We will mostly focus on covering some common scenarios. Hopefully, working through these scenarios now will be helpful in some future project.

A strategy to optimize an array of records from the database is to load only a few at a time by paginating them. Pagination is the task of dividing the potential result into pages and retrieving the required pages on demand. In Rails, this is easy to achieve through either will_paginate or kaminari.

The tricky part here is figuring out how to handle the JSON output to give the client enough information on how the array is paginated.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy