Skip to content

Commit b8c885a

Browse files
authored
Merge pull request #15006 from guardian/ih/hosted-content-web-handler
Set up web handler for hosted content
2 parents a81a1ab + 2b02165 commit b8c885a

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

dotcom-rendering/src/server/dev-index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ <h2>Pages</h2>
9797
>🏏 Cricket Match Page</a
9898
>
9999
</li>
100+
<li>
101+
<a
102+
href="/HostedContent/https://www.theguardian.com/advertiser-content/bloomberg-philanthropies/vision-for-a-healthy-future"
103+
>💷 Hosted Article</a
104+
>
105+
</li>
106+
<li>
107+
<a
108+
href="/HostedContent/https://www.theguardian.com/advertiser-content/gsk-oncology/unlocking-the-future-of-cancer-care"
109+
>💰 Hosted Video</a
110+
>
111+
</li>
112+
<li>
113+
<a
114+
href="/HostedContent/https://www.theguardian.com/advertiser-content/canary-islands-rugby/the-spanish-rugby-sevens-team"
115+
>💸 Hosted Gallery</a
116+
>
117+
</li>
100118
</ul>
101119
<form name="url-form">
102120
<input
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { RequestHandler } from 'express';
2+
import { validateAsFEHostedContent } from '../model/validate';
3+
import { enhanceHostedContentType } from '../types/hostedContent';
4+
import { makePrefetchHeader } from './lib/header';
5+
import { recordTypeAndPlatform } from './lib/logging-store';
6+
import { renderHtml } from './render.hostedContent.web';
7+
8+
export const handleHostedContent: RequestHandler = ({ body }, res) => {
9+
recordTypeAndPlatform('article', 'web');
10+
11+
const frontendData = validateAsFEHostedContent(body);
12+
const hostedContent = enhanceHostedContentType(frontendData);
13+
const { html, prefetchScripts } = renderHtml({
14+
hostedContent,
15+
});
16+
17+
res.status(200).set('Link', makePrefetchHeader(prefetchScripts)).send(html);
18+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
};

dotcom-rendering/src/server/server.dev.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { handleAppsAssets } from './handler.assets.apps';
1616
import { handleEditionsCrossword } from './handler.editionsCrossword';
1717
import { handleFront, handleTagPage } from './handler.front.web';
18+
import { handleHostedContent } from './handler.hostedContent.web';
1819
import {
1920
handleCricketMatchPage,
2021
handleFootballMatchListPage,
@@ -106,6 +107,8 @@ renderer.get('/FootballMatchListPage/*url', handleFootballMatchListPage);
106107
renderer.get('/FootballTablesPage/*url', handleFootballTablesPage);
107108
renderer.get('/CricketMatchPage/*url', handleCricketMatchPage);
108109
renderer.get('/FootballMatchSummaryPage/*url', handleFootballMatchPage);
110+
renderer.get('/HostedContent/*url', handleHostedContent);
111+
109112
// POST routes for running frontend locally
110113
renderer.post('/Article', handleArticle);
111114
renderer.post('/Interactive', handleInteractive);
@@ -121,6 +124,7 @@ renderer.post('/FootballMatchListPage', handleFootballMatchListPage);
121124
renderer.post('/FootballTablesPage', handleFootballTablesPage);
122125
renderer.post('/CricketMatchPage', handleCricketMatchPage);
123126
renderer.post('/FootballMatchSummaryPage', handleFootballMatchPage);
127+
renderer.post('/HostedContent', handleHostedContent);
124128

125129
renderer.get('/assets/rendered-items-assets', handleAppsAssets);
126130

dotcom-rendering/src/server/server.prod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import { handleAppsAssets } from './handler.assets.apps';
1818
import { handleEditionsCrossword } from './handler.editionsCrossword';
1919
import { handleFront, handleTagPage } from './handler.front.web';
20+
import { handleHostedContent } from './handler.hostedContent.web';
2021
import {
2122
handleCricketMatchPage,
2223
handleFootballMatchListPage,
@@ -75,6 +76,7 @@ export const prodServer = (): void => {
7576
logRenderTime,
7677
handleFootballMatchPage,
7778
);
79+
app.post('/HostedContent', logRenderTime, handleHostedContent);
7880

7981
app.post(
8082
'/EmailNewsletters',

0 commit comments

Comments
 (0)