How to cache a page with cookies
Hi everyone,
I have a ‘splash’ page used by visitors to choose in which country and language visit the site.
I want (with a cookie?) trace this choice to perform a redirect directly to the correct country and option previously selected when the user come back to the home page and I need to cache this ‘splash’ page.
What is the best pratice that you suggest?
-
A few options here:
If you would like to have
/
redirect to/lang/en
or whatever if the user has previously visited the english version, then you can add a cookie invcl_deliver
every time you output your language specific pages, and check for it invcl_recv
to issue a redirect if thereq.url
is/
and the cookie is present.If you want the user to make an explicit choice (and maybe tick a 'remember my choice' box on the homepage before having the redirect become automatic), then you could instead have the language options on the homepage link to special URLs such as
/lang/en?save=1
and then in vcl_recv detect that query parameter, and respond with aSet-Cookie
and a redirect response that strips the query param. The user ends up on/lang/en
and now has the cookie set, but if another user visits/lang/en
directly without going via the homepage, they don't get the remember-my-choice cookie.Often, a more elegant method of doing language customisation is to use the
Accept-Language
header in the browser's HTTP request. This will list the languages that the user understands, in their order of preference. You can then serve the right language automatically, with no need for language-specific URLs, and simply need to remember to addVary: Accept-Language
to your response. We provide some tools to help make this solution easier.
Please sign in to leave a comment.
Comments
2 comments