@@ -16,21 +16,22 @@ import { replaceRawWithCDN } from './listTemplate';
1616import { getTemplateEnvs } from '@/utils/tools' ;
1717import { getResourceUsage , ResourceUsage } from '@/utils/usage' ;
1818import { generateYamlData , getTemplateDefaultValues } from '@/utils/template' ;
19+ import { readmeCache } from '@/utils/readmeCache' ;
1920
2021export default async function handler ( req : NextApiRequest , res : NextApiResponse ) {
2122 try {
22- const {
23- templateName,
24- locale = 'en' ,
25- includeReadme = 'true'
26- } = req . query as {
23+ const envEnableReadme = process . env . ENABLE_README_FETCH ;
24+ const queryIncludeReadme = req . query . includeReadme !== 'false' ;
25+
26+ const includeReadme =
27+ envEnableReadme === 'false' ? 'false' : queryIncludeReadme ? 'true' : 'true' ;
28+
29+ const { templateName, locale = 'en' } = req . query as {
2730 templateName : string ;
2831 locale : string ;
29- includeReadme : string ;
3032 } ;
3133
3234 let user_namespace = '' ;
33-
3435 try {
3536 const { namespace } = await getK8s ( {
3637 kubeconfig : await authSession ( req . headers )
@@ -109,7 +110,7 @@ export async function GetTemplateByName({
109110 namespace,
110111 templateName,
111112 locale = 'en' ,
112- includeReadme = 'true '
113+ includeReadme = 'false '
113114} : {
114115 namespace : string ;
115116 templateName : string ;
@@ -123,7 +124,7 @@ export async function GetTemplateByName({
123124
124125 const originalPath = process . cwd ( ) ;
125126 const targetPath = path . resolve ( originalPath , 'templates' , targetFolder ) ;
126- // Query by file name in template details
127+
127128 const jsonPath = path . resolve ( originalPath , 'templates.json' ) ;
128129 const jsonData : TemplateType [ ] = JSON . parse ( fs . readFileSync ( jsonPath , 'utf8' ) ) ;
129130 const _tempalte = jsonData . find ( ( item ) => item . metadata . name === templateName ) ;
@@ -133,13 +134,15 @@ export async function GetTemplateByName({
133134 : fs . readFileSync ( `${ targetPath } /${ _tempalteName } ` , 'utf-8' ) ;
134135
135136 let { appYaml, templateYaml } = getYamlTemplate ( yamlString ) ;
137+
136138 if ( ! templateYaml ) {
137139 return {
138140 code : 40000 ,
139141 message : 'Lack of kind template'
140142 } ;
141143 }
142144 templateYaml . spec . deployCount = _tempalte ?. spec ?. deployCount ;
145+
143146 if ( cdnUrl ) {
144147 templateYaml . spec . readme = replaceRawWithCDN ( templateYaml . spec . readme , cdnUrl ) ;
145148 templateYaml . spec . icon = replaceRawWithCDN ( templateYaml . spec . icon , cdnUrl ) ;
@@ -158,7 +161,6 @@ export async function GetTemplateByName({
158161 templateYaml = parseTemplateVariable ( templateYaml , TemplateEnvs ) ;
159162 const dataSource = getTemplateDataSource ( templateYaml ) ;
160163
161- // Convert template to instance
162164 const instanceName = dataSource ?. defaults ?. [ 'app_name' ] ?. value ;
163165 if ( ! instanceName ) {
164166 return {
@@ -172,7 +174,7 @@ export async function GetTemplateByName({
172174 let readmeContent = '' ;
173175 let readUrl = '' ;
174176
175- if ( includeReadme ) {
177+ if ( includeReadme === 'true' ) {
176178 readUrl = templateYaml ?. spec ?. i18n ?. [ locale ] ?. readme || templateYaml ?. spec ?. readme || '' ;
177179 if ( readUrl ) {
178180 try {
@@ -198,6 +200,11 @@ export async function GetTemplateByName({
198200async function fetchReadmeContentWithRetry ( url : string ) : Promise < string > {
199201 if ( ! url ) return '' ;
200202
203+ const cachedContent = readmeCache . get ( url ) ;
204+ if ( cachedContent !== null ) {
205+ return cachedContent ;
206+ }
207+
201208 let retryCount = 0 ;
202209 const maxRetries = 3 ;
203210
@@ -218,7 +225,9 @@ async function fetchReadmeContentWithRetry(url: string): Promise<string> {
218225 throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
219226 }
220227
221- return await response . text ( ) ;
228+ const content = await response . text ( ) ;
229+ readmeCache . set ( url , content ) ;
230+ return content ;
222231 } catch ( err ) {
223232 retryCount ++ ;
224233 if ( retryCount === maxRetries ) {
0 commit comments