diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 7ce48af6..97ac9c24 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -38,7 +38,7 @@ export default defineNuxtPlugin({ resources.push(options.iconifyApiEndpoint!) } - async function customIconLoader(icons: string[], prefix: string): Promise { + async function loadCustomIconsChunk(icons: string[], prefix: string): Promise { try { const data = await requestFetch(resources[0] + '/' + prefix + '.json', { query: { @@ -56,6 +56,32 @@ export default defineNuxtPlugin({ } } + async function customIconLoader(icons: string[], prefix: string): Promise { + // Split large lists into several requests to stay under URL length limits (431 / 414) + const chunks: string[][] = [[]] + let length = 0 + for (const name of icons) { + const nameLength = encodeURIComponent(name).length + if (length + nameLength > 2000 && chunks.at(-1)!.length) { + chunks.push([]) + length = 0 + } + chunks.at(-1)!.push(name) + length += nameLength + 1 + } + const results = (await Promise.all(chunks.map(chunk => loadCustomIconsChunk(chunk, prefix)))) + .filter((data): data is IconifyJSON => !!data) + if (!results.length) + return null + // Keep collection-level properties (width, height, ...) from the responses + return results.reduce((acc, data) => ({ + ...acc, + ...data, + icons: { ...acc.icons, ...data.icons }, + aliases: { ...acc.aliases, ...data.aliases }, + })) + } + addAPIProvider('', { resources }) // Register custom collections handlers