| @@ -8,11 +8,13 @@ | ||
| 8 | 8 | retryTwice, |
| 9 | 9 | setStatus, |
| 10 | 10 | } from '@auto-launch/functions/helpers'; |
| 11 | 11 | import { getHeadersAndFooters } from '@auto-launch/functions/wp'; |
| 12 | +import { launchStrings } from '@auto-launch/strings'; | |
| 12 | 13 | import { PATTERNS_HOST } from '@constants'; |
| 14 | +import { digest } from '@shared/api/digest'; | |
| 13 | 15 | import { reqDataBasics } from '@shared/lib/data'; |
| 14 | -import { __ } from '@wordpress/i18n'; | |
| 16 | +import { siteImageUrlsByType } from '@shared/lib/site-images'; | |
| 15 | 17 | |
| 16 | 18 | const url = `${PATTERNS_HOST}/api/home`; |
| 17 | 19 | const { wpLanguage, showImprint } = window.extSharedData; |
| 18 | 20 | const method = 'POST'; |
| @@ -25,14 +27,30 @@ | ||
| 25 | 27 | aiHeaders, |
| 26 | 28 | designBuild, |
| 27 | 29 | }) => { |
| 28 | 30 | // translators: this is for a action log UI. Keep it short |
| 29 | - setStatus(__('Preparing your home page', 'extendify-local')); | |
| 31 | + setStatus(launchStrings().statusHome); | |
| 30 | 32 | |
| 33 | + // A full-page design build already carries the whole home; skip the | |
| 34 | + // /api/home fetch and build the page from the built patterns directly. | |
| 35 | + const builtHome = designBuild?.builtPages?.find((p) => p.slug === 'home'); | |
| 36 | + if (builtHome?.fullPage && builtHome.patterns?.length) { | |
| 37 | + // The full page is final — keep the BE's sections verbatim and flag them | |
| 38 | + // so generatePageContent skips the /api/patterns rewrite. | |
| 39 | + const patterns = builtHome.patterns.map((p) => ({ | |
| 40 | + ...p, | |
| 41 | + contentGenerated: true, | |
| 42 | + })); | |
| 43 | + const parts = await resolveTemplateParts({ siteProfile, designBuild }); | |
| 44 | + return getHomeShape.parse({ | |
| 45 | + home: { id: 'home', slug: 'home', patterns, ...parts }, | |
| 46 | + }); | |
| 47 | + } | |
| 48 | + | |
| 31 | 49 | const body = JSON.stringify({ |
| 32 | 50 | ...reqDataBasics, |
| 33 | 51 | siteProfile, |
| 34 | - siteImages, | |
| 52 | + siteImages: siteImageUrlsByType(siteImages), | |
| 35 | 53 | sitePlugins, |
| 36 | 54 | aiHeaders, |
| 37 | 55 | // If pages are passed in they may be used |
| 38 | 56 | pages: designBuild?.pages ?? [], |
| @@ -40,13 +58,43 @@ | ||
| 40 | 58 | }); |
| 41 | 59 | |
| 42 | 60 | const response = await retryTwice(() => |
| 43 | 61 | fetchWithTimeout(url, { method, headers, body }), |
| 62 | + ).catch((error) => { | |
| 63 | + return { ok: false, statusText: error.message, status: 0 }; | |
| 64 | + }); | |
| 65 | + | |
| 66 | + if (!response?.ok) { | |
| 67 | + digest({ | |
| 68 | + error: { | |
| 69 | + message: response.statusText, | |
| 70 | + name: 'FetchError', | |
| 71 | + status: response.status, | |
| 72 | + }, | |
| 73 | + details: { source: 'auto-launch', caller: 'handleHome' }, | |
| 74 | + }); | |
| 75 | + throw new Error(response.statusText); | |
| 76 | + } | |
| 77 | + | |
| 78 | + const template = homeTemplateShape.parse(await response.json()); | |
| 79 | + template.patterns = applyDesignBuildNav( | |
| 80 | + template.patterns, | |
| 81 | + designBuild, | |
| 82 | + siteProfile.structure, | |
| 44 | 83 | ); |
| 45 | - const template = homeTemplateShape.parse(await response.json()); | |
| 46 | 84 | template.patterns = applyDesignBuildHero(template.patterns, designBuild); |
| 47 | - template.patterns = applyDesignBuildNav(template.patterns, designBuild); | |
| 48 | 85 | |
| 86 | + const parts = await resolveTemplateParts({ siteProfile, designBuild }); | |
| 87 | + return getHomeShape.parse({ home: { ...template, ...parts } }); | |
| 88 | +}; | |
| 89 | + | |
| 90 | +const resolveTemplateParts = async ({ siteProfile, designBuild }) => { | |
| 91 | + const headerPart = designBuild?.templateParts?.header; | |
| 92 | + const footerPart = designBuild?.templateParts?.footer; | |
| 93 | + // Both parts came from the design build; skip the template-parts fetch. | |
| 94 | + if (headerPart && footerPart) | |
| 95 | + return { headerCode: headerPart, footerCode: footerPart }; | |
| 96 | + | |
| 49 | 97 | const hasFooterNav = Array.isArray(showImprint) |
| 50 | 98 | ? showImprint.includes(wpLanguage ?? '') && |
| 51 | 99 | siteProfile.category === 'Business' |
| 52 | 100 | : false; |
| @@ -52,11 +100,12 @@ | ||
| 52 | 100 | : false; |
| 53 | 101 | |
| 54 | 102 | const { headers: head, footers: foot } = await getHeadersAndFooters({ |
| 55 | 103 | useNavFooter: hasFooterNav, |
| 56 | - siteProfile, | |
| 57 | 104 | }); |
| 58 | - const headerCode = | |
| 59 | - designBuild?.headerCode ?? head[0]?.content?.raw?.trim() ?? ''; | |
| 60 | - const footerCode = foot[0]?.content?.raw?.trim() ?? ''; | |
| 61 | - return getHomeShape.parse({ home: { ...template, headerCode, footerCode } }); | |
| 105 | + const randomHeader = head[Math.floor(Math.random() * head.length)]; | |
| 106 | + const randomFooter = foot[Math.floor(Math.random() * foot.length)]; | |
| 107 | + return { | |
| 108 | + headerCode: headerPart ?? randomHeader?.content?.raw?.trim() ?? '', | |
| 109 | + footerCode: footerPart ?? randomFooter?.content?.raw?.trim() ?? '', | |
| 110 | + }; | |
| 62 | 111 | }; |