Skip to content

Commit 4bf0f50

Browse files
committed
feat: add useI18nPreloadKeys composable
1 parent 3d640d2 commit 4bf0f50

3 files changed

Lines changed: 48 additions & 5 deletions

File tree

src/prepare/auto-imports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function prepareAutoImports({ resolver, userOptions: options, runtimeDir
3131
'useBrowserLocale',
3232
'useCookieLocale',
3333
'useSetI18nParams',
34+
'useI18nPreloadKeys',
3435
NUXT_I18N_COMPOSABLE_DEFINE_ROUTE,
3536
NUXT_I18N_COMPOSABLE_DEFINE_LOCALE,
3637
NUXT_I18N_COMPOSABLE_DEFINE_CONFIG

src/runtime/composables/index.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useNuxtApp, useCookie, useRuntimeConfig } from '#imports'
1+
import { useNuxtApp, useCookie, useRuntimeConfig, useRequestEvent } from '#imports'
22
import { ref } from 'vue'
33
import { _useLocaleHead, _useSetI18nParams } from '../routing/head'
44
import { useComposableContext } from '../utils'
@@ -257,3 +257,38 @@ export function defineI18nRoute(route: I18nRoute | false): void {
257257
warnRuntimeUsage('defineI18nRoute')
258258
}
259259
}
260+
261+
/**
262+
* Register translation keys for preloading
263+
*
264+
* This is used to track keys to include in the preloaded messages which
265+
* otherwise would not be included during SSR.
266+
*
267+
* Examples of keys to register are dynamically or conditionally rendered translations (e.g. inside `v-if` or using computed keys).
268+
*
269+
* @param keys - The translation keys to preload
270+
*
271+
* @example
272+
* ```ts
273+
* useI18nPreloadKeys(['my-dynamic-key', 'nested.dynamic.key'])
274+
* ```
275+
*/
276+
export function useI18nPreloadKeys(keys: string[]): void {
277+
if (import.meta.server) {
278+
const ctx = useRequestEvent()?.context?.nuxtI18n
279+
if (ctx == null) {
280+
console.warn('useI18nPreloadKeys(): `nuxtI18n` server context is accessible.')
281+
return
282+
}
283+
284+
const locale = useNuxtApp()._nuxtI18n.getLocale()
285+
if (locale) {
286+
console.warn('useI18nPreloadKeys(): Could not resolve locale during server-side render.')
287+
return
288+
}
289+
290+
for (const k of keys) {
291+
ctx?.trackKey(k, locale)
292+
}
293+
}
294+
}

src/types.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,20 @@ export interface ExperimentalFeatures {
9898
*/
9999
cacheLifetime?: number
100100
/**
101-
* Strip unused locale messages from the server-side rendered HTML, reducing the size of the HTML payload.
102-
* This might cause issues with conditional rendering of messages in templates.
101+
* Preload locale messages and add them to the server-side rendered HTML.
102+
* This increases the size of the initial HTML payload but prevents an addition client-side request to load the messages.
103+
*
104+
* Since locale messages can be a large collection, it is recommended to use this in combination with `stripMessagesPayload`.
103105
* @default false
104106
*/
105-
stripMessagesPayload?: boolean
106-
107107
preload?: boolean
108+
/**
109+
* Strip unused locale messages from the server-side rendered HTML, reducing the size of the HTML payload.
110+
*
111+
* The `useI18nPreloadKeys` composable is used to prevent keys from being stripped, this is useful for conditionally rendered translations.
112+
* @default false // or `true` if `experimental.preload` is enabled
113+
*/
114+
stripMessagesPayload?: boolean
108115
}
109116

110117
export interface BundleOptions

0 commit comments

Comments
 (0)