/
githubmirror
/
hexo
Обзор
Документация
Войти
/
githubmirror
/
hexo
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lib/plugins/helper/partial.ts
46 строк
1 KB
Mimi
perf: avoid copying partial locals on cache hits (#5811)
09 авг 2026, 13:38
Не верифицирован
09 авг 2026, 13:38
b643653
Код
Авторство
О чём код?
import { dirname, join } from 'path'; import type Hexo from '../../hexo'; import type { LocalsType } from '../../types'; interface Options { cache?: boolean | string; only?: boolean; } export = (ctx: Hexo) => function partial(this: LocalsType, name: string, locals?: any, options: 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); if (!view) { throw new Error(`Partial ${name} does not exist. (in ${currentView})`); } // Build locals lazily so a fragment cache hit does not copy the render context. const render = () => { const viewLocals: Record<string, any> = {}; if (options.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, render); } return render(); };