Cacheable Cookie Handling

Component ID

2788855

Component name

Cacheable Cookie Handling

Component type

module

Maintenance status

Development status

Component security advisory coverage

covered

Downloads

349

Component created

Component changed

Component body

This module provides a framework for setting cookies that works well with page caching. It does not do anything on its own, but rather provides an API that other modules can use.

If your module needs to set cookies that depend on server-side information but that need to be accessed client-side by JavaScript, it can be difficult to do that in a way that is compatible with page caching. For example, if you set the cookie server-side when building the page, or if you put the relevant information into the HTML (via Drupal.settings) when building the page and then let the client-side code use that to set the cookie, the page cannot be properly cached since user-specific information was involved in building it. This can be a particular problem on sites that cache pages for authenticated users.

This module provides a simple, scalable solution to the problem:

Behind the scenes, the Cacheable Cookie Handling module will send a single Ajax request to ask the server to set all relevant cookies, only when JavaScript asks for a cookie that isn't already available. The Ajax request is uncached, but because everything is consolidated into a single request it should rarely need to be invoked (it will only need to be invoked again if some of the cookies expire or are deleted). And since regular Drupal page requests do not set the cookie, they can be safely cached like normal.

For additional information, see the README.txt file.

Credits

Contributions to the initial technical architecture and code reviews were provided by David_Rothstein, alexjarvis, Fabianx, and damiankloip.