Chapter20: the cookie consent message still show after click OK and refresh the web page

Hello ,

I have a problem regarding the topic " Cookies" in Page 325 of Chapter 20,
The cookies consent message still show after click OK and refresh the web page.
and even check in both safari and google chrome browser > setting page to enable javascript or enable cookie .

Much appreciated if you could advise.

Can you see the cookie being saved? Does the project code work?

Yes , I see the cookies being saved.(pls see attach picture)

I can create acronyms without login every request until log out.

Do you have a cookie called cookies-accepted?

@0xtim Yes, I have a cookie called cookies-accepted in cookies.js and in websiteController.swift.

What about in the browser? Is it getting set? If not, can you show the code for the Javascript and the route handler?

I get the error as below picture in the browser.
and found the cookies named “vapor session” instead ,not “cookies-accepted”.

Below are the code of javascript and route handler.

In Public/scripts/cookies.js

function cookiesConfirmed() {
var d = new Date();
d.setTime(d.getTime() + (3652460601000));
var expires = “expires=”+ d.toUTCString();
document.cookie = “cookies-accepted=true;” + expires;

The route handler in websiteController.swift

func indexHandler(_ req: Request) throws → Future {
return Acronym.query(on:req).all().flatMap(to: View.self) {
acronyms in
let acronymsData = acronyms.isEmpty ? nil: acronyms
let userLoggedIn = try req.isAuthenticated(User.self)
let showCookieMessage = req.http.cookies[“cookies-accepted”] == nil
let context = IndexContext(title: “Homepage”,acronyms: acronymsData ,userLoggedIn: userLoggedIn,showCookieMessage: showCookieMessage)
return try req.view().render(“index” , context)


Ok, have you imported the full jQuery? The next thing to check is the quotation marks - try adding them manually, they look off

First problem is that you copied the js from the book. That’s often a problem with code from books, the publishing software replaces quotes we use in software with typographical quotes. If you copy them into your code, you’ll get a syntax error.

  ^              ^ those should be ' like this:

I have no idea about the Uncaught ReferenceError though. Please post the part of base.leaf that’s inside the #if(showCookieMessage) {} block.

That’ll be the issue - it can’t interpret the variable which in JS is just a ReferenceError

@0xtim @fluchtpunkt

Solved!! ( after type quote manually instead of copy the code)
Thanks very much for pointing me how to solve the problem.
That is help me a lot.