Skip to content

Commit

Permalink
fix: fixed url error catch (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vibes-INS authored Dec 14, 2021
1 parent fc9442f commit f837419
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions libs/mibao-ui/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ export function addParamsToUrl (
if (!url) {
return url
}
const urlObj = new URL(url)
const urlSearchParams = urlObj.searchParams
Object.keys(params).forEach((key) => {
if (!urlSearchParams.has(key) || options?.ignoreDuplicates) {
urlSearchParams.set(key, String(params[key]))
}
})
return decodeURIComponent(urlObj.toString())
try {
const urlObj = new URL(url)
const urlSearchParams = urlObj.searchParams
Object.keys(params).forEach((key) => {
if (!urlSearchParams.has(key) || options?.ignoreDuplicates) {
urlSearchParams.set(key, String(params[key]))
}
})
return decodeURIComponent(urlObj.toString())
} catch {
return url
}
}

export function getImagePreviewUrl<U extends string | undefined> (
Expand All @@ -35,10 +39,17 @@ export function getImagePreviewUrl<U extends string | undefined> (
if (!url) {
return url
}
const urlObj = new URL(url)
try {
const urlObj = new URL(url)
const isSvgOrWebp = /\.(svg|webp)$/i.test(urlObj.pathname)
if (isSvgOrWebp) {
return url
}
} catch {
return url
}
const isOssHost = OSS_IMG_HOSTS.some((host) => url?.startsWith(host))
const isSvgOrWebp = /\.(svg|webp)$/i.test(urlObj.pathname)
if (!isOssHost || isSvgOrWebp) {
if (!isOssHost) {
return url
}
const webpParam = options?.webp ? OSS_IMG_PROCESS_QUERY_KEY_FORMAT_WEBP : ''
Expand Down

1 comment on commit f837419

@vercel
Copy link

@vercel vercel bot commented on f837419 Dec 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.