Skip to content

Commit 377e1df

Browse files
committedSep 26, 2024
fix: fix compatibility on Cloudflare
1 parent 5fc6b7d commit 377e1df

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed
 

‎src/runtime/server/api.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { consola } from 'consola'
44
import { createError, getQuery, type H3Event } from 'h3'
55
import type { NuxtIconRuntimeOptions } from '../../schema-types'
66
// @ts-expect-error tsconfig.server has the types
7-
import { useAppConfig, defineCachedEventHandler } from '#imports'
7+
import { useAppConfig, getRequestURL, defineCachedEventHandler } from '#imports'
88
import { collections } from '#nuxt-icon-server-bundle'
99

1010
const warnOnceSet = /* @__PURE__ */ new Set<string>()
1111

1212
const DEFAULT_ENDPOINT = 'https://api.iconify.design'
1313

1414
export default defineCachedEventHandler(async (event: H3Event) => {
15-
const url = event.node.req.url
15+
const url = getRequestURL(event) as URL
1616
if (!url)
17-
return
17+
return createError({ status: 400, message: 'Invalid icon request' })
1818

1919
const options = useAppConfig().icon as NuxtIconRuntimeOptions
2020
const collectionName = event.context.params?.collection?.replace(/\.json$/, '')
@@ -23,8 +23,7 @@ export default defineCachedEventHandler(async (event: H3Event) => {
2323
: null
2424

2525
const apiEndPoint = options.iconifyApiEndpoint || DEFAULT_ENDPOINT
26-
const apiUrl = new URL('./' + basename(url), apiEndPoint)
27-
const icons = apiUrl.searchParams.get('icons')?.split(',')
26+
const icons = url.searchParams.get('icons')?.split(',')
2827

2928
if (collection) {
3029
if (icons?.length) {
@@ -48,6 +47,7 @@ export default defineCachedEventHandler(async (event: H3Event) => {
4847
}
4948

5049
if (options.fallbackToApi === true || options.fallbackToApi === 'server-only') {
50+
const apiUrl = new URL('./' + basename(url.pathname) + url.search, apiEndPoint)
5151
consola.debug(`[Icon] fetching ${(icons || []).map(i => '`' + collectionName + ':' + i + '`').join(',')} from iconify api`)
5252
if (apiUrl.host !== new URL(apiEndPoint).host) {
5353
return createError({ status: 400, message: 'Invalid icon request' })
@@ -56,9 +56,13 @@ export default defineCachedEventHandler(async (event: H3Event) => {
5656
const data = await $fetch(apiUrl.href)
5757
return data
5858
}
59-
catch (e) {
59+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
60+
catch (e: any) {
6061
console.error(e)
61-
return createError({ status: 404 })
62+
if (e.status === 404)
63+
return createError({ status: 404 })
64+
else
65+
return createError({ status: 500, message: 'Failed to fetch fallback icon' })
6266
}
6367
}
6468
return createError({ status: 404 })

0 commit comments

Comments
 (0)
Please sign in to comment.