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

perf(partial): enhance caching #4146

Closed
wants to merge 1 commit 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
49 changes: 32 additions & 17 deletions lib/plugins/helper/partial.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
'use strict';

const { dirname, join } = require('path');
const { Cache } = require('hexo-util');
const viewCache = new Cache();

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

const { cache } = options;
const viewDir = this.view_dir;
const currentView = this.filename.substring(viewDir.length);
const path = join(dirname(currentView), name);
const view = ctx.theme.getView(path) || ctx.theme.getView(name);
const viewLocals = { layout: false };
let view;
if (viewCache.has(name)) {
view = viewCache.get(name);
} else {
const viewDir = this.view_dir;
const currentView = this.filename.substring(viewDir.length);
const path = join(dirname(currentView), name);
view = ctx.theme.getView(path) || ctx.theme.getView(name);

if (!view) {
throw new Error(`Partial ${name} does not exist. (in ${currentView})`);
}
if (!view) {
throw new Error(`Partial ${name} does not exist. (in ${currentView})`);
}

if (options.only) {
Object.assign(viewLocals, locals);
} else {
Object.assign(viewLocals, this, locals);
viewCache.set(name, view);
}

// Partial don't need layout
viewLocals.layout = false;
const { cache, only } = options;

const viewRender = () => {
const viewLocals = { layout: false };

if (only) {
Object.assign(viewLocals, locals);
} else {
Object.assign(viewLocals, this, locals);
}

// Partial don't need layout
viewLocals.layout = false;

return view.renderSync(viewLocals);
};

if (cache) {
const cacheId = typeof cache === 'string' ? cache : view.path;

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

return view.renderSync(viewLocals);
return viewRender();
};
14 changes: 7 additions & 7 deletions test/scripts/helpers/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ describe('partial', () => {
});

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

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

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

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

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

partial('test', {bar: 'bar'}, {only: true}).should.eql('bar');
partial('test2', {bar: 'bar'}, {only: true}).should.eql('bar');
});

it('a partial in another partial', () => {
Expand Down