Skip to content

Commit ccc00ab

Browse files
committedDec 16, 2024
fix: register custom collections handler, fix #322
1 parent 20ec75c commit ccc00ab

File tree

6 files changed

+48
-6
lines changed

6 files changed

+48
-6
lines changed
 

Diff for: ‎playground/nuxt.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default defineNuxtConfig({
6363
'logos:vitejs',
6464
'ph:acorn-bold',
6565
],
66-
includeCustomCollections: true,
66+
includeCustomCollections: false,
6767
scan: true,
6868
},
6969
},

Diff for: ‎src/module.ts

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ export default defineNuxtModule<ModuleOptions>({
9696
nuxt.options.appConfig.icon = Object.assign(
9797
nuxt.options.appConfig.icon || {},
9898
runtimeOptions,
99+
{
100+
customCollections: options.customCollections?.map(i => i.prefix),
101+
},
99102
)
100103
// Define types for the app.config compatible with Nuxt Studio
101104
nuxt.hook('schema:extend', (schemas) => {

Diff for: ‎src/runtime/plugin.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { addAPIProvider, _api, disableCache } from '@iconify/vue'
1+
import { addAPIProvider, _api, disableCache, setCustomIconsLoader } from '@iconify/vue'
22
import type { Plugin } from 'nuxt/app'
3+
import type { IconifyJSON } from '@iconify/types'
34
import type { NuxtIconRuntimeOptions } from '../types'
45
import { defineNuxtPlugin, useAppConfig, useRuntimeConfig } from '#imports'
56

67
export default defineNuxtPlugin({
78
name: '@nuxt/icon',
89
setup() {
9-
const config = useRuntimeConfig()
10+
const configs = useRuntimeConfig()
1011
const options = useAppConfig().icon as NuxtIconRuntimeOptions
1112

1213
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -17,7 +18,7 @@ export default defineNuxtPlugin({
1718

1819
const resources: string[] = []
1920
if (options.provider === 'server') {
20-
const baseURL = config.app?.baseURL?.replace(/\/$/, '') ?? ''
21+
const baseURL = configs.app?.baseURL?.replace(/\/$/, '') ?? ''
2122

2223
resources.push(baseURL + (options.localApiEndpoint || '/api/_nuxt_icon'))
2324
if (options.fallbackToApi === true || options.fallbackToApi === 'client-only') {
@@ -28,6 +29,30 @@ export default defineNuxtPlugin({
2829
resources.push(options.iconifyApiEndpoint!)
2930
}
3031

32+
async function customIconLoader(icons: string[], prefix: string): Promise<IconifyJSON | null> {
33+
try {
34+
const data = await $fetch(resources[0] + '/' + prefix + '.json', {
35+
query: {
36+
icons: icons.join(','),
37+
},
38+
}) as IconifyJSON
39+
// Simple data validation
40+
if (!data || data.prefix !== prefix || !data.icons)
41+
throw new Error('Invalid data' + JSON.stringify(data))
42+
return data as IconifyJSON
43+
}
44+
catch (e) {
45+
console.error('Failed to load custom icons', e)
46+
return null
47+
}
48+
}
49+
3150
addAPIProvider('', { resources })
51+
52+
// Register custom collections handlers
53+
for (const prefix of options.customCollections || []) {
54+
if (prefix)
55+
setCustomIconsLoader(customIconLoader, prefix)
56+
}
3257
},
3358
}) as Plugin

Diff for: ‎src/schema-types.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ export interface NuxtIconRuntimeOptions {
8686
*/
8787
collections: string[] | null
8888

89+
/**
90+
* Custom Icon Collections
91+
*
92+
*/
93+
customCollections: string[] | null
94+
8995
/**
9096
* Icon Provider
9197
*
@@ -133,7 +139,7 @@ export interface NuxtIconRuntimeOptions {
133139
/**
134140
* Fetch Timeout
135141
*
136-
* Set the timeout for fetching icons on SSR.
142+
* Set the timeout for fetching icons.
137143
*
138144
* @default 1500
139145
*/

Diff for: ‎src/schema.ts

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ export const schema = {
9595
tsType: 'string[] | null',
9696
},
9797
},
98+
customCollections: {
99+
$default: null,
100+
$schema: {
101+
title: 'Custom Icon Collections',
102+
tags: ['@studioIcon material-symbols:format-list-bulleted'],
103+
tsType: 'string[] | null',
104+
},
105+
},
98106
provider: {
99107
$default: undefined,
100108
$schema: {

Diff for: ‎src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface NuxtIconRuntimeServerOptions {
1717
serverKnownCssClasses?: string[]
1818
}
1919

20-
export interface ModuleOptions extends Partial<Omit<NuxtIconRuntimeOptions, 'customize'>> {
20+
export interface ModuleOptions extends Partial<Omit<NuxtIconRuntimeOptions, 'customize' | 'customCollections'>> {
2121
/**
2222
* Name of the component to be registered
2323
* @default 'Icon'

0 commit comments

Comments
 (0)
Please sign in to comment.