How to cache a page with cookies



  • charlieputh

    I'm so curious about to this. Any response?

    Comment actions Permalink
  • Andrew Betts

    A few options here:

    1. 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 in vcl_deliver every time you output your language specific pages, and check for it in vcl_recv to issue a redirect if the req.url is / and the cookie is present.

    2. 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 a Set-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.

    3. 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 add Vary: Accept-Language to your response. We provide some tools to help make this solution easier.

    Comment actions Permalink

Please sign in to leave a comment.