I have an expressjs app with angularjs on the frontend, and i'm serving jade out of express, i.e.

app.set('view engine', 'jade');

I'm also using jade partials with angular. The app is based off the Yeoman angular fullstack generator.

Here's a link to the navbar.html partial from the generator's demo app for reference (except I am using Jade)

I inject the current logged in user server-side into the template engine via express middleware to add it to res.locals so it is available in the template at runtime, e.g.

app.get('/partials/*', function(req, res, next) { 
  res.locals.user = req.user; next(); 
}, index.partials);

Now, this app has user roles, let's assume there are two: ['user', 'admin'], and I wanted to conditionally output the markup from navbar.jade depending on the current user's role.

For example, something like this (admin users should only see admin menu item):

ul.nav
  if user
    li
      a(href='/settings')
  if (user && user.role == 'admin')
    li
      a(href='/admin')

I know I can ng-hide / ng-show these links cient side with Angular, but I'd prefer to not even output the markup if the user shouldn't have the authorization to use them (they are protected server-side as well.)

So, this approach kinda works. If I login with a normal user, the navbar is fine, then I logout, and re-login as an admin user, but the browser makes a request for navbar.html, and ExpressJS returns a 304 Not Modified, so the content is stale. If I refresh/F5, it returns the proper version based on the user's role (i.e. the unbuffered Jade JavaScript is interpreted on the refresh).

I'm trying to figure out how to force it. I can experiment with cache-control headers and Etag disablement hackery, but I'm trying to resolve a clean way to fix this.

有帮助吗?

解决方案

Answering my own question. The issue was not with Jade/Express -- the problem was with this bit of code:

div(ng-include="/partials/navbar")

When a user logged out / in, I was mistaken that the browser was issuing a new request for the partial from the server. Chrome DevTools network tab showed the request with a 304 Not Modified but on closer inspection, the server was not receiving the request. To fix it, I now bind the ng-include source attribute to a scope variable, and when the user logs out/in, I refresh the scope variable which forces angular to issue a new request, and Jade/Express execute the template and respond with an updated partial that matches what I expect.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top