Skip to content
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

allow requesting a page that is out of range to return empty #68

Closed
grosser opened this issue Jul 9, 2018 · 19 comments
Closed

allow requesting a page that is out of range to return empty #68

grosser opened this issue Jul 9, 2018 · 19 comments

Comments

@grosser
Copy link
Contributor

grosser commented Jul 9, 2018

atm it raises OutOfRangeError which is confusing for users when they click on the last page, but some item got deleted or apis consumers that iterate until they find an empty result
also impractical since now I cannot render the page but need to make up a fake pagy or redirect to a page that I don't know wether it exists (how do I get the last page valid ?)

so either of these:

  • make empty the default since that's what most expect
  • add optional empty_when_out_of_range: true or so
@ddnexus
Copy link
Owner

ddnexus commented Jul 9, 2018 via email

@grosser
Copy link
Contributor Author

grosser commented Jul 9, 2018

Using this atm:

# https://github.com/ddnexus/pagy/issues/68
Pagy::Backend.prepend(Module.new do
  private
  def pagy(*args)
    super
  rescue Pagy::OutOfRangeError => e
    e.pagy.instance_variable_set(:@page, e.pagy.last)
    [e.pagy, []]
  end
end)

which makes it behave like it's on the actual last page but has no items

@ddnexus
Copy link
Owner

ddnexus commented Jul 9, 2018

I understand it now :).

However your solution looks wrong to me: you are serving the last page that has items at the time of the request (or it wouldn't be the last page), but you are hiding them, hence providing a wrong inconsistent response.

The right and consistent thing to do is redirecting to the actual last page, eventually providing a feedback to the user, but showing the items as they are.

The last page is the last valid page at the time of the request/error. I don't see any problem here.

@ddnexus
Copy link
Owner

ddnexus commented Jul 9, 2018

And if you're paginating with an API, you should return the error and the last page number so giving a feedback usable for the next request.

@grosser
Copy link
Contributor Author

grosser commented Jul 9, 2018 via email

@ddnexus
Copy link
Owner

ddnexus commented Jul 9, 2018

Rendering an empty last page while it has items seems the worse solution to me.

An API rendering an exception with the reason and feedback to get the next request in the correct range seems pretty correct to me.

I didn't get the example of the 200 empty reply thought...

@grosser
Copy link
Contributor Author

grosser commented Jul 9, 2018

lots of clients do pagination by requesting next page until it is empty, returning a 404 would break that behavior

@grosser
Copy link
Contributor Author

grosser commented Jul 9, 2018

it's not telling the user what page it is, just showing empty content

@ddnexus
Copy link
Owner

ddnexus commented Jul 9, 2018

BTW, a default empty last page is not a possible solution, not only because it would be wrong as explained above, but it would obliterate the freedom to rescue and do something else.

It is so simple to rescue - even to a wrong last empty page if you really want that - that adding an option doesn't seem so useful to me at this point.

Easier than that... I only see an extra that could be explicitly required... but only if it would provide a solution consistent with the data, avoiding empty pages that are not really empty.

@ddnexus
Copy link
Owner

ddnexus commented Jul 9, 2018

returning a 404 would break that behavior

Which is a quite questionable behavior :)... but I understand that the problem is that you have to create a pagy instance that works, hence you add the last page... Which is inconsistent but works because stops API clients with bad behaviors :)

I understand it and it may be the best way to do it in your context, but don't feel like that should be in the pagy code.

Any alternative solution?

@grosser
Copy link
Contributor Author

grosser commented Jul 9, 2018 via email

@ddnexus
Copy link
Owner

ddnexus commented Jul 9, 2018

Both good suggestions. Thanks! How Pagy.empty should look like in order to work?
What about Pagy.new count: 0?

@grosser
Copy link
Contributor Author

grosser commented Jul 9, 2018 via email

@ddnexus
Copy link
Owner

ddnexus commented Jul 10, 2018

it's not telling the user what page it is, just showing empty content
last page I guess ...otherwise it might to weird pagination displays

I think we are mixing up 2 different cases: UI and API.

With an UI you actually have to show something to the user, and an empty page would also likely have the page number in the pagy_info, pagy_nav and URL: that raises the consistency problem with the last page wrongly empty.

With an API you may avoid the problem by not even showing the page number, but what abut the prev and next headers? You should have a pretty lame API with no pagination headers nor page number, nor feedback in the response in order to avoid the inconsistency problem.

I don't think that neither cases should be officially supported by Pagy. I am trying to find a Pagy instance that could solve both problems but I cannot.

@grosser
Copy link
Contributor Author

grosser commented Jul 10, 2018 via email

@ddnexus
Copy link
Owner

ddnexus commented Jul 11, 2018

Maybe you could write a brief paragraph in the how to about how to handle it?

@ddnexus
Copy link
Owner

ddnexus commented Jul 12, 2018

Thanks for the PR...
Meanwhile I think I have found a cool way to consistently handle this... working on it now...

@ddnexus ddnexus added the WIP label Jul 12, 2018
ddnexus added a commit that referenced this issue Jul 13, 2018
@ddnexus
Copy link
Owner

ddnexus commented Jul 13, 2018

@grosser
I think the out_of_range extra that I pushed with v0.13.0 satisfied all the requirements - yours and mine :)
Hope you will like it.

@ddnexus ddnexus closed this as completed Jul 13, 2018
@ddnexus ddnexus removed the WIP label Jul 13, 2018
ddnexus added a commit that referenced this issue Jul 13, 2018
Co-authored-by: Michael Grosser <michael@grosser.it>
@ddnexus
Copy link
Owner

ddnexus commented Jul 13, 2018

@grosser now it should be less hacky :).

@ddnexus ddnexus changed the title allow requesting a page that is put of range to return empty allow requesting a page that is out of range to return empty Sep 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants