Home > Net >  Should vue filtering use REST API or url parameters
Should vue filtering use REST API or url parameters

Time:07-28

I'm designing a website with a REST API using Django Rest Framework and Vue for the front end and I'm trying to work out what the proper way to do filtering is.

As far as I can see, I can either:-

a) Allow filtering via the API by using URL parameters like /?foo=bar

or

b) Do all the filtering on the Vue side by only displaying the items that are returned that have foo=bar

Are there any strong reasons for doing one over the other?

CodePudding user response:

The real answer to this question is "it depends".

Here are a few questions to ask yourself to help determine what the best approach is:

How much data will be returned if I don't filter at the API level?

If you're returning just a few records, there won't be a noticeable performance hit when the query runs. If you're returning thousands, you'll likely want to consider server side querying/paging.

If you're building an application where the amount of data will grow over time, it's best to build the server side querying from the get-go.

What do I want the front-end experience to be like?

For API calls that return small amounts of data, the user experience will be much more responsive if you return all records up front and do client-side filtering. That way if users change filters or click through paged data, the UI can update almost instantaneously.

Will any other applications be consuming my API?

If you plan to build other apps that consume the API, you may want to build the filtering at the API level so you don't need to recreate front-end filtering logic in every consuming application.

Hopefully these questions can help guide you to the best answer for your use case.

CodePudding user response:

Whenever I come across this issue I ask myself just one question: How many items are you working with? If you're only returning a few items from the API you can easily do the filtering on the front-end side and save yourself a bunch of requests whenever the results are filtered. Also, if the result set is quite small, it's a lot faster to do it this way rather than sending off a request every time the filters change.

However, if you're working with a very large number of items, it's probably best to just filter them out in the API, or even via your database query if that's what you're working with. This will save you from returning a large number of results to the front-end. Also, filtering large numbers of items on the front-end can significantly impact performance since it usually involves looping over a collection.

  • Related