Cookie notice Page Caching to: Common mistakes that can easily be avoided

Anyone who operates a Website, used at least since the new DSGVO Cookie notice. In connection with Page Caching, however, often problems is less well known. The Following are common errors and their correction are presented.

If you are running a Website, you likely have a Cookie-a reference to the fact installed. In order for you to inform your visitors about the use of Cookies or asks the permission to use. To optimize your loading time, you probably have a Caching System on the Website or the Server installed. Most of the Hosting providers to select the today, by the way automatically. (If you are not yet a Caching System to use, you should catch up quickly, because the load time of your site is an important SEO factor! ;-))

The combination of Caching and Cookie notice often leads to problems, but can be easily fixed.

First of all: What is Caching?

Although there are different types of Caching, but almost all of them outstaffing work the same way: you create a “Version” of the site, which can be delivered to the users that the Server will have to spend additional resources.

If you want to use, for example, a Content Management System like WordPress, it runs every Time a user comes to the site, a PHP Code in the Server. The retrieves some information from the database and then creates an HTML page requested by the user.

To avoid that a user visited repeated every Time that the Website stores a Caching System-generated HTML-page in the first creation. When someone visits the Website, the Server returns only the HTML Version of the page, without the use of PHP needs to be re-executed. This reduces the number of queries to the database to a Minimum. The work load on the Server is reduced enormously, and the page load time is greatly accelerated.

What problems can arise in the case of a Caching System?

The Problem is that this HTML is generated page only once. Any other user will receive exactly the same Version of the Website. When a Cookie-note only works with server-side languages (such as PHP for WordPress), is no longer running this PHP Code for displaying the Cookie notice or by the Setting of Cookies is responsible. This means that, If the generation of the HTML page to the Cookie notice was displayed, it will appear with high probability, then for every user, even those who had accepted the Cookies in advance. The reverse is the case – that the Cookie note never appears – can occur naturally, as well.

If PHP or other server side language is no longer running, it can happen that the System is set up, either at each visit, or at all out of Cookies.

Much worse is that the Cookie notification, and the use of Cookies can be managed on a site in different ways. This means that you may, regardless of whether a user has accepted the Cookies installed in the Browser. A Problem that occurs very often, as the next point will show.

How can I avoid problems with Caching and cookies?

The answer is obvious: by using JavaScript! Unlike PHP or another server-side language, the use of web sites contain JavaScript in the HTML Version of the page, (so also in the Cache memory) and is executed in the Browser of the user. This means that the Code is always available and running, regardless of whether the user will see a cached Version of the Website.

JavaScript can check whether the use of Cookies has already been approved. It can also be the Cookie-note – display or hide the Cookies once they have been accepted.

Unfortunately, it can also come here sometimes to problems. Some of the Cookie-Plugins are not really geared up to be a Caching System. Thus, it may be that, for example, to Display or to Hide the Cookie notice JavaScript is used for the Installation, or Checking the “Cookies” but a server-side language (which, in turn, is executed when the site is cached). This site owners don’t go out, then, as already mentioned, that the Cookie-System works, what is true in reality.

I need to learn JavaScript?

Do not panic, this is not necessary! For almost every System that will be used to create a Website, there are Plugins or extensions that do the work for you! It would be beyond the scope of this article, all of these Plugins list. But you can be sure that there are hundreds! Just read the Details of the plug-in and ensures that the Plugin uses only JavaScript.

To be sure, can you also check what Cookies your Website uses. You can with any popular Browser. You have available the “developer tools” and click on the tab simply opens Cookies. In the case of Chrome, you can do so: Open a private Tab and click on the menu View -> Developer -> Developer tools. It is a new surface in your Browser opens. Click on the tab Application and the area Cookies. Here you can also remove previously installed Cookies. In the port you can go to your site again to check whether the Cookie notice or not.

(Screenshot: Antonio Sanchez)

You want to program, but a little even, we also have a simple solution that you need to copy only:

1. Using JavaScript to check whether a specific Cookie has been set:

All Cookies will be stored as a string in the document object that contains a semicolon-separated list of the Name and the value of the Cookies:

cookie-name=value; other-cookie=other-value;

This means that you only need the name of the Cookies, you look like, so:

if( document.cookie.indexOf("cookie-name=") >= 0)

Please be careful to use the Symbol”=”!

2. With JavaScript, the value of the cookie, to extract:

After you’ve checked, whether the Cookie exists, do you want to know perhaps the value:

if (document.cookie.indexOf("cookie-name="))) {
    var cookieValue = document.cookie.replace(/(?:(?:(?:^|.*;s*)name of the cookies*=s*([^;]*).*$)|^.*$/, "$1");

After that, you can simply “if”sentence to use your Cookie note is to show or hide. If you want to, for example, the Cookie-note only hide when the value of the cookie “visited” is, it must be used in the following sentence:

  ... euer Code zum Verstecken des Hinweises (der genaue Code ist von eurer jeweiligen Website beziehungsweise dem Plugin, das ihr verwendet, abhängig)

More about the document.cookie object you can find in the MDN Web Docs.

Antonio Sanchez is a native of Spain, and the founder and CEO of Kokoen GmbH, a web Agency, Web and App development, SEO, and Social Media. As a Mobile – and Web App Developer, he shares his passion for programming and his Dedicated Team.

This could also interest you

Ready to see us in action:

More To Explore
Enable registration in settings - general
Have any project in mind?

Contact us: