Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(enhance_cache): bring up [locals] #3902

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ module.exports = {
ignore: [],

// Category & Tag
meta_generator: true
meta_generator: true,

// Enhance Cache
enhance_cache: false
};

18 changes: 18 additions & 0 deletions lib/plugins/helper/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

const { dirname, join } = require('path');

const { createHash } = require('crypto');
const md5 = (str) => createHash('md5').update(str).digest('hex');

module.exports = ctx => function partial(name, locals, options = {}) {
if (typeof name !== 'string') throw new TypeError('name must be a string!');

Expand All @@ -26,10 +29,25 @@ module.exports = ctx => function partial(name, locals, options = {}) {
viewLocals.layout = false;

if (cache) {
// fragment_cache for the partial is enabled no matter if enhance_cache is enabled
/*
* The specific { cache: true } or { cache: cacheId } should have higher priority.
* It means theme developer want to cache this partial even the locals has changed.
*/
const cacheId = typeof cache === 'string' ? cache : view.path;

return this.fragment_cache(cacheId, () => view.renderSync(viewLocals));
} else if (ctx.config.enhance_cache && cache !== false) {
// enhance_cache is enabled while fragment_cache for the partial is not disabled
/*
* If a theme developer doesn't want caching, they might use { cache: false }.
* We should stick to current behavior even if user has enabled enhance_cache.
*/
const cacheId = typeof cache === 'string' ? cache : [view.path, md5(JSON.stringify(locals))].join('-');

return this.fragment_cache(cacheId, () => view.renderSync(viewLocals));
}

// render directly since neither enhance_cache & fragment_cache is enabled.
return view.renderSync(viewLocals);
};
30 changes: 23 additions & 7 deletions test/scripts/helpers/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ describe('partial', () => {
partial('test', {foo: 'bar'}).should.eql('bar');
});

it('cache', () => {
hexo.theme.setView('test.swig', '{{ foo }}');

partial('test', {foo: 'bar'}, {cache: true}).should.eql('bar');
partial('test', {}, {cache: true}).should.eql('bar');
});

it('only', () => {
hexo.theme.setView('test.swig', '{{ foo }}{{ bar }}');

Expand Down Expand Up @@ -94,4 +87,27 @@ describe('partial', () => {

errorCallback.calledOnce.should.be.true;
});

it('cache - default', () => {
hexo.theme.setView('test.swig', '{{ foo }}');

partial('test', {foo: 'bar'}, { cache: true }).should.eql('bar');
partial('test', {}, { cache: true }).should.eql('bar');
partial('test', {foo: 'bar'}, { cache: 'test-cache-id' }).should.eql('bar');
partial('test', {}, { cache: 'test-cache-id' }).should.eql('bar');
});

it('cache - enhance cache', () => {
hexo.config.enhance_cache = true;
hexo.theme.setView('test.swig', '{{ foo }}');

// enhance cache should only cache partial when the locals unchanged
partial('test', {foo: 'bar'}).should.eql('bar');
partial('test', {foo: 'baz'}).should.eql('baz');
// when {cache: true} is set, enhance cache should cache the partial even the locals changed
partial('test', {foo: 'bar'}, { cache: true }).should.eql('bar');
partial('test', {foo: 'baz'}, { cache: true }).should.eql('bar');
partial('test', {foo: 'bar'}, { cache: 'test-cache-id' }).should.eql('bar');
partial('test', {foo: 'baz'}, { cache: 'test-cache-id' }).should.eql('bar');
});
});