Neko Jiru
Neko Jiru

Reputation: 45

Weird Express Jade view caching behavior

Just updated my Node.js and Express versions to 0.10.21 and 3.4.4, respectively, and now I'm seeing some weird view caching in development (and production).

It seems the html generated from views included within other views (or maybe all views?) is being cached.

For example:

layout.jade

doctype 5
html
    head
        title= title
        meta(name="viewport", content="width=device-width, initial-scale=1.0")
        link(rel='stylesheet', href='/stylesheets/style.css')
    body
        #container
            include header
            block content
            include footer

header.jade

- var headerClass = ""

if pageVars.headerOverBackground
    - headerClass = "overbackground"   

#header(class=headerClass)
    [snip]

somepage.jade

extends layout

block content

    [snip]

The first time I call /somepage, passing pageVars.headerOverBackground set to true, the view is rendered correctly. If I visit a different URL, /someotherpage, with the same layout and header, passing in pageVars.headerOverBackground set to false, I still see the header.jade portion rendered as if it was on the previous page (with the "overbackground" class on #header), as if pageVars.headerOverBackground is still true.

But it's false, and I've got the console.log()'s to prove it.

Am I doing something wrong? Am I just horribly confused? I've run Node in development and production modes and even peppered my Express with

app.disable('view cache');

to no avail...

edit:

No matter how many times I reload the page, it loads the cached view. If I restart Node and reload, the correct view appears.

Upvotes: 1

Views: 2179

Answers (1)

Neko Jiru
Neko Jiru

Reputation: 45

Okay, I think I may have solved this.

One of the routes mistakenly had

pageVars = {};

instead of

var pageVars = {}; 

resulting in a global variable being declared. Oops! And somewhere between the older versions of Node/Express/Jade and the current versions, Jade began preferring the global variable even when explicitly passed a local one of the same name.

So if one route mistakenly does

pageVars = {};  //global, oops
pageVars.headerOverBackground = true;
res.render("onepage", {pageVars:pageVars}); 

Then another route is called

var pageVars = {};  //local
// pageVars.headerOverBackground is undefined
res.render("anotherpage", {pageVars:pageVars});

The jade in the anotherpage view will use the global version of pageVars instead of the local variable that was passed, and will still think pageVars.headerOverBackground is true.

Upvotes: 2

Related Questions