Skip to content

Commit 8929b67

Browse files
committed
dont run spacefinder on full width interactives
1 parent 0ad3f79 commit 8929b67

File tree

1 file changed

+62
-25
lines changed

1 file changed

+62
-25
lines changed

bundle/src/insert/spacefinder/article.ts

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ const addDesktopInline1 = (fillSlot: FillAdSlot): Promise<boolean> => {
132132
const addDesktopRightRailAds = (
133133
fillSlot: FillAdSlot,
134134
isConsentless: boolean,
135-
fullWidthGridBody = false,
135+
standardArticleGrid = true,
136136
): Promise<boolean> => {
137137
const insertAds: SpacefinderWriter = async (paras) => {
138138
const stickyContainerHeights = await computeStickyHeights(
@@ -153,7 +153,7 @@ const addDesktopRightRailAds = (
153153
const containerClasses = [
154154
getStickyContainerClassname(i),
155155
'ad-slot-container--right-column', // float the ad to the right and sets max width and transparent background https://github.com/guardian/dotcom-rendering/blob/main/dotcom-rendering/src/lib/adStyles.ts#L161
156-
!fullWidthGridBody && 'ad-slot-container--offset-right', // adds a negative margin to push the ad into the right rail, this isn't needed if the article body is full width
156+
standardArticleGrid && 'ad-slot-container--offset-right', // adds a negative margin to push the ad into the right rail, this isn't needed if the article body is full width
157157
]
158158
.filter(Boolean)
159159
.join(' ');
@@ -253,30 +253,43 @@ const addMobileAndTabletInlineAds = (
253253
});
254254
};
255255

256-
/**
257-
* Checks if the article body is the full width of the article grid, which is the case for most interactive articles.
258-
* Paragraphs are always the same width however, so when it's full width we don't need a negative margin to offset the ad into the right rail, `float: right` alone will suffice. (see addDesktopRightRailAds function)
259-
*/
260-
const hasFullWidthGridBody = async (): Promise<boolean> => {
261-
if (!isInteractive) {
262-
return false;
263-
}
264-
265-
const gridBody = document.querySelector<HTMLElement>(
266-
'[data-gu-name="body"]',
267-
);
268-
const parent = gridBody?.parentElement;
256+
const getGridWidths = () =>
257+
fastdom.measure(() => {
258+
const gridBody = document.querySelector<HTMLElement>(
259+
'[data-gu-name="body"]',
260+
);
261+
const parent = gridBody?.parentElement;
269262

270-
if (!gridBody || !parent) {
271-
return false;
272-
}
263+
const gridBodyWidth = gridBody?.getBoundingClientRect().width;
264+
const parentWidth = parent?.getBoundingClientRect().width;
265+
const viewportWidth = window.innerWidth;
266+
return { gridBodyWidth, parentWidth, viewportWidth };
267+
});
273268

274-
return fastdom.measure(() => {
275-
const articleBodWidth = gridBody.getBoundingClientRect().width;
276-
const parentWidth = parent.getBoundingClientRect().width;
269+
/**
270+
* Determine whether the grid body is the full width of its parent container. If the grid body is full width, then we need to insert the ad without the offset right class, otherwise the ad will be pushed too far into the right hand column and could end up outside of the viewport.
271+
* @param gridBodyWidth
272+
* @param parentWidth
273+
* @returns
274+
*/
275+
const isGridBodyFullWidthOfParent = (
276+
gridBodyWidth: number,
277+
parentWidth: number,
278+
): boolean => {
279+
return gridBodyWidth >= parentWidth;
280+
};
277281

278-
return articleBodWidth >= parentWidth;
279-
});
282+
/**
283+
* Determine whether the grid body is the full width of the viewport. If the grid body is the full width of the viewport, then it's unlikely to have a right hand column, even if it does, it's probably using wacky styles that we can't easily work with, so we won't attempt to insert ads in this case.*
284+
* @param gridBodyWidth
285+
* @param viewportWidth
286+
* @returns
287+
**/
288+
const isGridBodyFullWidthOfViewport = (
289+
gridBodyWidth: number,
290+
viewportWidth: number,
291+
): boolean => {
292+
return gridBodyWidth >= viewportWidth;
280293
};
281294

282295
/**
@@ -297,10 +310,34 @@ const addInlineAds = async (
297310
return addDesktopRightRailAds(fillSlot, isConsentless);
298311
}
299312

300-
const fullWidthGridBody = await hasFullWidthGridBody();
313+
if (isInteractive) {
314+
const { gridBodyWidth, parentWidth, viewportWidth } =
315+
await getGridWidths();
316+
317+
if (gridBodyWidth && parentWidth && viewportWidth) {
318+
// If the grid body is the full width of the viewport, then it's unlikely to have a right hand column, even if it does, it's probably using wacky styles that we can't easily work with, so we won't attempt to insert ads in this case.
319+
if (isGridBodyFullWidthOfViewport(gridBodyWidth, viewportWidth)) {
320+
return false;
321+
}
322+
323+
const fullWidthGridBody = isGridBodyFullWidthOfParent(
324+
gridBodyWidth,
325+
parentWidth,
326+
);
327+
328+
return addDesktopInline1(fillSlot).then(() =>
329+
addDesktopRightRailAds(
330+
fillSlot,
331+
isConsentless,
332+
fullWidthGridBody,
333+
),
334+
);
335+
}
336+
return false;
337+
}
301338

302339
return addDesktopInline1(fillSlot).then(() =>
303-
addDesktopRightRailAds(fillSlot, isConsentless, fullWidthGridBody),
340+
addDesktopRightRailAds(fillSlot, isConsentless),
304341
);
305342
};
306343

0 commit comments

Comments
 (0)