@@ -89,16 +89,43 @@ function normalizeCatalogImageBaseUrl(rawBaseUrl: string): string {
8989 return trimmedValue . replace ( / \/ + $ / u, '' ) ;
9090}
9191
92- function getCatalogImageBaseUrl ( ) : string {
92+ function getCatalogImageBaseUrlFromEnv ( ) : string | undefined {
9393 const viteEnvCandidate = ( import . meta as ImportMeta & { env ?: Record < string , string | undefined > } ) . env ;
9494 const processEnvCandidate = typeof process !== 'undefined' ? process . env : undefined ;
95- const configuredBaseUrl = viteEnvCandidate ?. VITE_INGREDIENT_IMAGE_BASE_URL ?? processEnvCandidate ?. VITE_INGREDIENT_IMAGE_BASE_URL ;
95+ return viteEnvCandidate ?. VITE_INGREDIENT_IMAGE_BASE_URL ?? processEnvCandidate ?. VITE_INGREDIENT_IMAGE_BASE_URL ;
96+ }
97+
98+ let cachedCatalogImageBaseUrlConfig : string | undefined ;
99+ let cachedCatalogImageBaseUrlValue : string | null | undefined ;
100+
101+ function getCatalogImageBaseUrl ( ) : string | null {
102+ const configuredBaseUrl = getCatalogImageBaseUrlFromEnv ( ) ;
103+ if ( cachedCatalogImageBaseUrlValue !== undefined && cachedCatalogImageBaseUrlConfig === configuredBaseUrl ) {
104+ return cachedCatalogImageBaseUrlValue ;
105+ }
96106
97107 if ( configuredBaseUrl === undefined ) {
98- throw new Error ( 'VITE_INGREDIENT_IMAGE_BASE_URL is required for ingredient catalog images.' ) ;
108+ cachedCatalogImageBaseUrlConfig = configuredBaseUrl ;
109+ cachedCatalogImageBaseUrlValue = null ;
110+ return cachedCatalogImageBaseUrlValue ;
99111 }
100112
101- return normalizeCatalogImageBaseUrl ( configuredBaseUrl ) ;
113+ try {
114+ cachedCatalogImageBaseUrlConfig = configuredBaseUrl ;
115+ cachedCatalogImageBaseUrlValue = normalizeCatalogImageBaseUrl ( configuredBaseUrl ) ;
116+ return cachedCatalogImageBaseUrlValue ;
117+ } catch ( error ) {
118+ console . warn (
119+ 'Ingredient catalog images are disabled because VITE_INGREDIENT_IMAGE_BASE_URL is invalid.' ,
120+ {
121+ configuredBaseUrl,
122+ error : error instanceof Error ? error . message : String ( error ) ,
123+ } ,
124+ ) ;
125+ cachedCatalogImageBaseUrlConfig = configuredBaseUrl ;
126+ cachedCatalogImageBaseUrlValue = null ;
127+ return cachedCatalogImageBaseUrlValue ;
128+ }
102129}
103130
104131function createIngredientCatalogEntry < TKey extends string > (
@@ -640,8 +667,9 @@ export function resolveIngredientVisual(input: IngredientVisualInput): Ingredien
640667 const catalogMatch = findIngredientCatalogMatch ( searchText ) ;
641668 if ( catalogMatch !== null ) {
642669 const imageBaseUrl = getCatalogImageBaseUrl ( ) ;
670+ const imageUrl = imageBaseUrl === null ? null : buildCatalogImageUrl ( imageBaseUrl , catalogMatch . entry . imageObjectKey ) ;
643671 return {
644- imageUrl : buildCatalogImageUrl ( imageBaseUrl , catalogMatch . entry . imageObjectKey ) ,
672+ imageUrl,
645673 fallbackIcon : catalogMatch . entry . fallbackIcon ,
646674 altText : catalogMatch . entry . altText ,
647675 source : 'catalog-match' ,
0 commit comments