perf(nunjucks): reuse compiled view templates - #5810
Merged
Conversation
How to testgit clone -b fix/nunjucks-compile-cache https://github.com/hexojs/hexo.git
cd hexo
npm install
npm test |
SukkaW
approved these changes
Aug 8, 2026
D-Sketon
approved these changes
Aug 9, 2026
stevenjoezhang
force-pushed
the
fix/nunjucks-compile-cache
branch
from
August 9, 2026 11:53
e7d7f76 to
0548582
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does it do?
Reuses the compiled top-level Nunjucks template for each Hexo View instead of creating a new Nunjucks environment and recompiling the same source on every render.
Background
The current behavior dates back to #4356, which introduced the dedicated Nunjucks renderer in 2020.
Although the
compile()hook returns a closure with a comment saying that the closure keeps the compiled template,njkCompile(data)is invoked inside that closure. Consequently, every render creates another Nunjucks environment and recompiles the current View.Simply moving the compilation outside the closure would make
{% extends %},{% include %}, and{% import %}dependencies stale duringhexo server, because Nunjucks caches loader-resolved templates.This PR therefore:
Environment#invalidateCache()before every render.extends,include, andimport.hexo serverhot-reload behavior.This optimization was identified while profiling #5705. It does not remove the
Buffer.from()encoding cost reported by that issue; it addresses a larger Nunjucks rendering hot path found during the same profiling work.Performance
Measured with an isolated snapshot of a real-world Hexo blog. Baseline and patched runs used the same site state and were executed in alternating order.
hexo generatemedianThe unchanged dependency-read count is intentional: loader-resolved dependencies are still reloaded on each render to preserve server-side hot updates.
Tests
Added coverage to verify that:
includetemplates are reloaded.extendstemplates are reloaded.importtemplates are reloaded.A real
hexo serverintegration check was also performed by changing an included theme partial and confirming that the updated content appeared in the next HTTP response.Validation completed:
Related to #5705.
Screenshots
N/A
Pull request tasks