Skip to content

Commit

Permalink
Fix async helpers not working when cache enabled
Browse files Browse the repository at this point in the history
fixes #140
closes #141
  • Loading branch information
CaroseKYS authored and dougwilson committed Sep 27, 2019
1 parent 80fe665 commit 891014a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Fix async helpers not working when cache enabled
* deps: handlebars@4.1.2
* deps: walk@2.3.14

Expand Down
11 changes: 10 additions & 1 deletion lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ function middleware(filename, options, cb) {
// cached?
var template = cache[filename];
if (template) {
return cb(null, template(locals, handlebarsOpts));
var res = template(locals, handlebarsOpts)
self.async.done(function (values) {
Object.keys(values).forEach(function (id) {
res = res.replace(id, values[id])
})

cb(null, res)
})

return
}

fs.readFile(filename, 'utf8', function(err, str){
Expand Down
11 changes: 10 additions & 1 deletion test/4.x/async_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ before(function () {
app.engine('hbs', hbs.__express)

// set the view engine to use handlebars
app.set('view cache', true)
app.set('view engine', 'hbs')
app.set('views', path.join(__dirname, 'views'))

app.use(express.static(path.join(__dirname, 'public')))

// value for async helper
// it will be called a few times from the template
var indx = 0
var vals = ['foo', 'bar', 'baz']
hbs.registerAsyncHelper('async', function (context, cb) {
process.nextTick(function () {
cb(vals.shift())
cb(vals[indx++ % 3])
})
})

Expand Down Expand Up @@ -90,6 +92,13 @@ test('async', function(done) {
.end(done)
});

test('cached', function (done) {
request(app)
.get('/')
.expect(fs.readFileSync(path.join(FIXTURES_DIR, 'async.html'), 'utf8'))
.end(done)
})

test('async-with-params', function(done) {
request(app)
.get('/async-with-params')
Expand Down

0 comments on commit 891014a

Please sign in to comment.