|
| 1 | +import { isString } from '@guardian/libs'; |
| 2 | +import { HostedArticleLayout } from '../layouts/HostedArticleLayout'; |
| 3 | +import { HostedGalleryLayout } from '../layouts/HostedGalleryLayout'; |
| 4 | +import { getModulesBuild, getPathFromManifest } from '../lib/assets'; |
| 5 | +import { renderToStringWithEmotion } from '../lib/emotion'; |
| 6 | +import { polyfillIO } from '../lib/polyfill.io'; |
| 7 | +import type { HostedContent } from '../types/hostedContent'; |
| 8 | +import { htmlPageTemplate } from './htmlPageTemplate'; |
| 9 | + |
| 10 | +type Props = { |
| 11 | + hostedContent: HostedContent; |
| 12 | +}; |
| 13 | + |
| 14 | +export const renderHtml = ({ hostedContent }: Props) => { |
| 15 | + const { type, frontendData } = hostedContent; |
| 16 | + |
| 17 | + const title = `Advertiser content hosted by the Guardian: ${frontendData.title} | The Guardian`; |
| 18 | + |
| 19 | + const HostedLayout = |
| 20 | + type === 'gallery' ? HostedGalleryLayout : HostedArticleLayout; |
| 21 | + const renderingTarget = 'Web'; |
| 22 | + |
| 23 | + const { html, extractedCss } = renderToStringWithEmotion( |
| 24 | + <HostedLayout renderingTarget={renderingTarget} />, |
| 25 | + ); |
| 26 | + |
| 27 | + // We don't send A/B tests or switches from frontend yet- do we need to? |
| 28 | + const build = getModulesBuild({ |
| 29 | + tests: {}, |
| 30 | + switches: {}, |
| 31 | + }); |
| 32 | + |
| 33 | + const prefetchScripts = [ |
| 34 | + polyfillIO, |
| 35 | + getPathFromManifest(build, 'frameworks.js'), |
| 36 | + getPathFromManifest(build, 'index.js'), |
| 37 | + ].filter(isString); |
| 38 | + |
| 39 | + // We currently don't send any of the data required for page config or window.guardian setup from frontend |
| 40 | + const pageHtml = htmlPageTemplate({ |
| 41 | + scriptTags: [], |
| 42 | + css: extractedCss, |
| 43 | + html, |
| 44 | + title, |
| 45 | + description: frontendData.standfirst, |
| 46 | + // @ts-expect-error no config data |
| 47 | + guardian: {}, |
| 48 | + canonicalUrl: '', |
| 49 | + renderingTarget: 'Web', |
| 50 | + // @ts-expect-error no config data |
| 51 | + config: {}, |
| 52 | + weAreHiring: false, |
| 53 | + }); |
| 54 | + |
| 55 | + return { html: pageHtml, prefetchScripts }; |
| 56 | +}; |
0 commit comments