Caching custom templates
Hello I am working on a Nuxt module that uses the addTemplate method to add some content. Problem is that generation of
getContents takes around 10 seconds, is there any way to resolve it?
My current idea is to manually write content to a file in ./.nuxt/cache/my-module, compare hashes of paramaters and either regenerate it or load it. Is there any better way to solve it?Nuxt
Templates · Nuxt Kit v4
Nuxt Kit provides a set of utilities to help you work with templates. These functions allow you to generate extra files during development and build time.
5 Replies
Hi @Norbiros. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Nuxt does not provide a built-in cache mechanism for template generation in the
- Nuxt does not currently offer a built-in cache for
addTemplate utility itself. The getContents function is called whenever Nuxt needs to (re)generate the template, and if this process is slow, it can impact your build times.
Your idea of caching the generated content and comparing hashes is reasonable and aligns with common optimization strategies. However, recent Nuxt improvements have focused on reducing unnecessary template updates and recompilations. For example, there have been changes to only update templates when files actually change, and optimizations to move expensive work to only run when necessary. There is also a recent PR to use hashes for template comparison, which should help avoid unnecessary regeneration if the content hasn't changed, but this is internal to Nuxt's own template system and not exposed as a public API for custom caching logic yet perf(nuxt): use hash for process template comparison, perf(nuxt): reduce unnecessary template updating.
If your getContents is slow due to expensive computation, you can implement your own caching as you described: write the generated content to a file, store a hash of the parameters, and only regenerate if the hash changes. This is not provided out-of-the-box by Nuxt, but is a valid approach.
Additionally, you can use the updateTemplates utility to manually trigger regeneration of templates when you know the underlying data has changed, rather than relying on Nuxt to always call getContents Templates API.
Summary:- Nuxt does not currently offer a built-in cache for
getContents in addTemplate.
- Your manual caching approach is valid and commonly used.
- Recent Nuxt improvements may reduce unnecessary template regeneration, but do not eliminate the need for custom caching if your template generation is inherently slow.
- Use updateTemplates to control when templates are regenerated if you can detect changes yourself.
If you need more granular control, you will need to implement your own caching logic as you described.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@770620808644919307> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai where it is used in real nuxt modules, this caching idea?
Let me check the knowledge base for you again. One moment! :books:
----------------------
I'm sorry, but the knowledge sources do not contain explicit examples or documentation of real Nuxt modules implementing custom caching for template generation (such as writing generated content to disk and comparing hashes before regenerating). While the idea is technically valid, there is no direct reference or case study in the provided sources that shows this caching approach being used in official or community Nuxt modules. If you need more concrete examples, you may need to look into the source code of popular Nuxt modules or community discussions outside the provided documentation and resources.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@770620808644919307> Kapa.ai is still learning and improving, please let me know how I did by reacting below.