Skip to content

Commit 0c6790e

Browse files
committed
fix(perf): add a simple cache to client bundle
1 parent bf39379 commit 0c6790e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/bundle-client.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import type { NuxtIconModuleContext } from './context'
44
export function registerClientBundle(
55
ctx: NuxtIconModuleContext,
66
): void {
7+
let cacheSize = 0
8+
let cacheData: string | null = null
9+
710
// Client bundle
811
addTemplate({
912
filename: 'nuxt-icon-client-bundle.mjs',
@@ -13,8 +16,13 @@ export function registerClientBundle(
1316
sizeLimitKb = 256,
1417
} = ctx.options.clientBundle || {}
1518

19+
// TODO: find a granular way to cache this
1620
const { collections, count, failed } = await ctx.loadClientBundleCollections()
1721

22+
if (cacheSize === count && cacheData) {
23+
return cacheData
24+
}
25+
1826
if (failed.length) {
1927
const msg = `Nuxt Icon could not fetch the icon data for client bundle:\n${failed.map(f => ' - ' + f).join('\n')}`
2028
if (ctx.nuxt.options._build)
@@ -43,7 +51,7 @@ export function registerClientBundle(
4351

4452
const collectionsRaw = `JSON.parse(${JSON.stringify(valuesCompat)})`
4553

46-
return [
54+
cacheData = [
4755
'let _initialized = false',
4856
'export function init(addIcon) {',
4957
' if (_initialized)',
@@ -57,6 +65,8 @@ export function registerClientBundle(
5765
' _initialized = true',
5866
'}',
5967
].join('\n')
68+
cacheSize = count
69+
return cacheData
6070
},
6171
})
6272
}

0 commit comments

Comments
 (0)