ginad
ginad

Reputation: 1985

How do you disable caching in jade or swig

I've been playing with express js lately and I'm loving it. My problem is that when I submit an invalid form a validation error message appears but when I reload the page the error message is still displaying I think the error message is being cached. Is there a way to disable caching in jade / swig and also is there a way to just specify which pages to cache and not to cache(like the form)?

sample code:

filename: index.js
// app.get('/form', index.getForm)
exports.getForm = function(req, res){
 res.render('form');
}

// app.post('/form', index.postForm)
exports.postForm = function(req, res){
 // express form validation goes here ...
 if(errors){
  res.locals.errors = errors;
  res.locals.form = req.body;
  exports.getForm(req, res);
 }else{
  res.redirect('/welcome');
 }
}

and here's my jade

filename: form.jade
 if error.email
  .error=error.email.msg
  input#email(type='email', name='email', value='#{form.email}')
 else
  input#email(type='email', name='email', value='#{form.email}')

Upvotes: 1

Views: 878

Answers (1)

jmingov
jmingov

Reputation: 14013

Theres no cache problems unless you are running express in production mode.

It looks like when you press F5, after the form error, your request is re-submiting the same form via post (with the error).

Option 1:

  • AJAX <3

Option 2:

  • Use 2 diferent jade templates, one for the exports.postForm with errors and another for the exports.getForm. Something like:

    exports.getForm = function(req, res) {
        res.render('form');
        }
    
    
    exports.postForm = function(req, res) {
        // express form validation goes here.
        if(errors) {
            res.locals.errors = errors;
            res.locals.form = req.body;
            res.render('form_fail');  <----
        }
        else {
            res.redirect('/welcome');
       }
    }
    

Upvotes: 1

Related Questions