Proposed Bugfix: Prevent Login Redirect From Browser Cache Rules
-
I have a scenario where I’m using a Private Website via Profile Builder and then also have W3 Total Cache set to have browser caching being optimized for pages that are public (which then doesn’t really allow for individual page exceptions for the browser caching rules). I imagine this also applies to Restricted Content that’s set to redirect if not logged in as a suitable user.
The problem I had was that a user could try to go to a page that’s protected while not being logged in as a sufficient user so then they’re redirected to the login page (expected behavior), but then it appears the page result with the redirect would actually get cached by the web browser even though it’s the default 302 redirect from wp_redirect and wp_safe_redirect. That meant that trying to view that page after logging in would still have the redirect occur that sends them back to the login page. Using a browser’s Network inspector within the Inspect tool showed that the redirect was being loaded from the browser cache and not actually checking with the site again.
I came across https://github.com/qTranslate-Team/qtranslate-x/issues/306 where another plugin encountered this same problem where they had a redirect that really should be 100% temporary (not cached in any way), but then browsers were still caching the page result that included the 302 redirect so it was sticking around for longer than needed/wanted.
– The Fix –
The fix was to patch Profile Builder to use what it appears other plugins have found to be a reliable resolution for the issue and actually utilizes a function that’s native to WordPress’ core.In short, I edited
profile-builder/features/functions.php
andprofile-builder/features/content-restriction/content-restriction-functions.php
so that any & all instances ofwp_redirect
andwp_safe_redirect
havenocache_headers(); // Prevent browser caching of page with the redirect header
included as the line immediately before it. I found 4 instances of this in these 2 files, but it might be worthwhile to see if there are additional spots for this to be included as well (these just seemed to be the ones related to this particular issue.)This prevents the problem(s) that result from the possibility of the various temporary redirects from being cached by the web browser in an official, low-overhead, and graceful way.
I’d love to see this included in the next version of Profile Builder, and let me know if you have any questions or anything.
- The topic ‘Proposed Bugfix: Prevent Login Redirect From Browser Cache Rules’ is closed to new replies.