Hot Module Replacement (HMR)
Relevant source files
The following files were used as context for generating this wiki page:
Hot Module Replacement (HMR) in nuxt-i18n-micro provides real-time translation updates in the browser without requiring a full page reload. This system targets JSON translation files and injects updated content directly into the Nuxt runtime state during development.
Overview
The HMR system operates through a specialized package, @i18n-micro/hmr, which generates a Nuxt plugin responsible for watching and accepting translation changes. It distinguishes between global (root) translations and page-specific translations to ensure granular updates.
Sources: packages/hmr/package.json:1-5, AGENTS.md:16-16
Architecture and Components
The HMR architecture relies on three primary logic flows: file parsing, root group building, and plugin source generation.
File Parsing
The system categorizes incoming file paths to determine their role in the translation hierarchy. It uses the parsePrimaryFile function to identify if a file belongs to a specific page or the global root.
- Page Files: JSON files located within a
/pages/directory. The parser extracts thepageNameandlocale. - Root Files: JSON files located at the top level of the translation directory. The parser extracts the
locale.
Sources: packages/hmr/src/generate-plugin.ts:16-29
Root Group Building
When multiple translation directories are used (e.g., through Nuxt layers), the system merges them. The buildRootGroups function organizes additional root files and primary root files by locale. It maintains a specific merge order: additional files are processed first, followed by the primary file.
Sources: packages/hmr/src/generate-plugin.ts:40-67
Logic Flow Diagram
The following diagram illustrates how the HMR plugin handles a file change event.
The HMR flow ensures that unchanged layers are not dropped by re-importing the entire group when a root file changes.
Sources: packages/hmr/src/generate-plugin.ts:92-140
Implementation Details
Plugin Generation
The generateHmrPlugin function creates the source code for a Nuxt plugin. This plugin utilizes Vite's import.meta.hot API to intercept module updates.
| Feature | Implementation Detail |
|---|---|
| Page Updates | Accepts a single file and calls $loadPageTranslations with specific locale and pageName. |
| Global Updates | Accepts an array of files (root group). It re-imports all files to ensure deep merge consistency. |
| Merging | Uses deepMergeTranslationsRecursive from @i18n-micro/utils for root files. |
| Logging | Outputs [i18n HMR] Translations reloaded to the browser console upon success. |
Sources: packages/hmr/src/generate-plugin.ts:92-160, packages/hmr/package.json:76-77
Handling Additional Root Files
If additionalRootFiles are provided in the GenerateHmrPluginOptions, the generated code includes logic to merge these files before updating the runtime.
// Example of generated HMR logic for root files
if (import.meta.hot) {
import.meta.hot.accept([/* files */], async () => {
const nuxtApp = useNuxtApp()
let data = {}
for (const path of paths) {
const mod = await import(path)
data = deepMergeTranslationsRecursive(data, mod.default || mod)
}
await nuxtApp.$loadPageTranslations(locale, 'index', data)
})
}
Sources: packages/hmr/src/generate-plugin.ts:123-146, packages/hmr/tests/generate-plugin.test.ts:14-23
Integration with Nuxt
The HMR plugin is integrated as a Nuxt plugin during development. It requires the useNuxtApp composable to access the $loadPageTranslations method, which is the internal trigger for updating the reactive translation state in the application.
Sources: packages/hmr/src/generate-plugin.ts:97-105, scripts/src/commands/docs-generate.ts:250-255
Summary
HMR in nuxt-i18n-micro enables a rapid development cycle by providing granular updates to translation data. By distinguishing between page-level and root-level files and implementing a robust merging strategy for global translations, the system ensures that developers see immediate, accurate changes in the UI without losing state from translation layers.