lobsters / lobsters-ansible

Ansible playbook for lobste.rs
ISC License
78 stars 25 forks source link

fix: redirect paginated requests that pollute cache #83

Open bazzargh opened 7 months ago

bazzargh commented 7 months ago

Fixes https://github.com/lobsters/lobsters/issues/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 https://github.com/rails/actionpack-page_caching/issues/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)