File tree Expand file tree Collapse file tree 1 file changed +14
-8
lines changed
packages/thirdweb/src/insight Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change @@ -338,16 +338,22 @@ function replaceIPFSGateway(url?: string) {
338338 if ( ! url || typeof url !== "string" ) {
339339 return url ;
340340 }
341- if ( url . includes ( "ipfscdn.io" ) ) {
342- const paths = url . split ( "/" ) ;
343- const index = paths . findIndex ( ( path ) => path === "ipfs" ) ;
344- if ( index === - 1 ) {
341+ try {
342+ const parsedUrl = new URL ( url ) ;
343+ if ( parsedUrl . host . endsWith ( "ipfscdn.io" ) ) {
344+ const paths = parsedUrl . pathname . split ( "/" ) ;
345+ const index = paths . findIndex ( ( path ) => path === "ipfs" ) ;
346+ if ( index === - 1 ) {
347+ return url ;
348+ }
349+ const ipfsHash = paths . slice ( index + 1 ) . join ( "/" ) ;
350+ if ( ipfsHash ) {
351+ return `ipfs://${ ipfsHash } ` ;
352+ }
345353 return url ;
346354 }
347- const ipfsHash = paths . slice ( index + 1 ) . join ( "/" ) ;
348- if ( ipfsHash ) {
349- return `ipfs://${ ipfsHash } ` ;
350- }
355+ } catch {
356+ // If the URL is invalid, return it as is
351357 return url ;
352358 }
353359 return url ;
You can’t perform that action at this time.
0 commit comments