1

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.

Neko Jiru
  • 45
  • 6
  • Sounds similar to what's happening [here](http://stackoverflow.com/questions/16922802/express-view-cache-acting-funny) (not solved yet though) – robertklep Oct 30 '13 at 07:46
  • (btw I can't reproduce it using `express@3.4.4`, `jade@0.35.0` and `node@0.10.21`) – robertklep Oct 30 '13 at 07:57
  • I'm not sure if it's a typo of yours or not, but... have you tried appending an `-` to the `if pageVars.headerOverBackground` as well? Or you may want to simplify it by just `- headerClass = pageVars.headerOverBackground ? 'overbackground' : ''`. Let me know if it helps in some way. – Cristian Douce Oct 31 '13 at 05:24
  • `-` isn't necessary as far as I know, and in fact when I append it to the line I get an error. In fact I whipped up a short app with 2 views and a header and was unable to reproduce the problem, either. I guess I'm left paring down my code until I get to whatever is causing the issue. One note is that I can add `= console.log(pageVars.headerOverBackground)` to header.jade and see "undefined" print out... just after the route printed out "true" for the same var the line before calling for a render of the view. – Neko Jiru Oct 31 '13 at 05:49

1 Answers1

2

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.

Neko Jiru
  • 45
  • 6