PluginProbe
Extendify / trunk
Extendify vtrunk
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / AutoLaunch / fetchers / get-home.js

get-home.js in Extendify trunk, at src/AutoLaunch/fetchers/get-home.js

112 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {
2 applyDesignBuildHero,
3 applyDesignBuildNav,
4 } from '@auto-launch/fetchers/get-design-build';
5 import { getHomeShape, homeTemplateShape } from '@auto-launch/fetchers/shape';
6 import {
7 fetchWithTimeout,
8 retryTwice,
9 setStatus,
10 } from '@auto-launch/functions/helpers';
11 import { getHeadersAndFooters } from '@auto-launch/functions/wp';
12 import { launchStrings } from '@auto-launch/strings';
13 import { PATTERNS_HOST } from '@constants';
14 import { digest } from '@shared/api/digest';
15 import { reqDataBasics } from '@shared/lib/data';
16 import { siteImageUrlsByType } from '@shared/lib/site-images';
17
18 const url = `${PATTERNS_HOST}/api/home`;
19 const { wpLanguage, showImprint } = window.extSharedData;
20 const method = 'POST';
21 const headers = { 'Content-Type': 'application/json' };
22
23 export const handleHome = async ({
24 siteProfile,
25 sitePlugins,
26 siteImages,
27 aiHeaders,
28 designBuild,
29 }) => {
30 // translators: this is for a action log UI. Keep it short
31 setStatus(launchStrings().statusHome);
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
49 const body = JSON.stringify({
50 ...reqDataBasics,
51 siteProfile,
52 siteImages: siteImageUrlsByType(siteImages),
53 sitePlugins,
54 aiHeaders,
55 // If pages are passed in they may be used
56 pages: designBuild?.pages ?? [],
57 buildId: designBuild?.buildId,
58 });
59
60 const response = await retryTwice(() =>
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,
83 );
84 template.patterns = applyDesignBuildHero(template.patterns, designBuild);
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
97 const hasFooterNav = Array.isArray(showImprint)
98 ? showImprint.includes(wpLanguage ?? '') &&
99 siteProfile.category === 'Business'
100 : false;
101
102 const { headers: head, footers: foot } = await getHeadersAndFooters({
103 useNavFooter: hasFooterNav,
104 });
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 };
111 };
112