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

fix: redirect paginated requests that pollute cache #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bazzargh
Copy link

@bazzargh bazzargh commented Jan 9, 2024

Fixes lobsters/lobsters#1219

Requests that attempt to use pagination via ?page=N for anything but search will pollute the rails cache because rails ignores the parameters when writing to the file cache: see eg
rails/actionpack-page_caching#52

There are two sides to this bug: we should not read from the cached parameterless page if there is a page parameter present, and we should not write to the cache for the parameterless page if there is a page parameter present.

Both cases can be avoided by redirecting requests with the page parameter to the path format that lobste.rs prefers, so /hottest.json?page=10 becomes /hottest/page/10.json.

There is one page which does use the page parameter instead of path parameters for pagination, and that is /search. However, search does not use the full page cache at all:
https://github.com/search?q=repo%3Alobsters%2Flobsters%20%20caches_page&type=code

Hence we can skip the redirect for /search, but also safely skip the other cache checks. This logic would be simpler if nginx supported nested ifs but it does not: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if Note that the if directive is not allowed in an 'if' context (whereas the rewrite directive is)

Fixes lobsters/lobsters#1219

Requests that attempt to use pagination via ?page=N for anything but
search will pollute the rails cache because rails ignores the parameters
when writing to the file cache: see eg
rails/actionpack-page_caching#52

There are two sides to this bug: we should not read from the cached
parameterless page if there is a page parameter present, and we should
not write to the cache for the parameterless page if there is a page
parameter present.

Both cases can be avoided by redirecting requests with the page parameter
to the path format that lobste.rs prefers, so /hottest.json?page=10
becomes /hottest/page/10.json.

There is one page which does use the page parameter instead of path
parameters for pagination, and that is /search. However, search
does not use the full page cache at all:
https://github.com/search?q=repo%3Alobsters%2Flobsters%20%20caches_page&type=code

Hence we can skip the redirect for /search, but also safely skip the
other cache checks. This logic would be simpler if nginx supported nested ifs
but it does not: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if
Note that the if directive is not allowed in an 'if' context (whereas
the rewrite directive is)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird cache behaviour on hottest.json?
1 participant