Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dotcom-rendering/fixtures/generated/fe-articles/Audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const Audio: FEArticle = {
source: 'The Guardian',
},
url: 'https://audio.guim.co.uk/2026/02/27-42462-gdn.alr.060326.NA_GAVIN_FRANCIS_MENTALILLNESS.mp3',
urlWithAds:
'https://flex.acast.com/audio.guim.co.uk/2026/02/27-42462-gdn.alr.060326.NA_GAVIN_FRANCIS_MENTALILLNESS.mp3',
mimeType: 'audio/mpeg',
},
],
Expand Down
23 changes: 3 additions & 20 deletions dotcom-rendering/src/components/AudioAtom/AudioAtom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,11 @@ const PlaySVG = () => (
</svg>
);

const buildUrl = (basicUrl: string, shouldUseAcast?: boolean) => {
return shouldUseAcast
? basicUrl.replace('https://', 'https://flex.acast.com/')
: basicUrl;
};

type Props = {
id: string;
trackUrl: string;
kicker: string;
title?: string;
shouldUseAcast?: boolean;
duration: number;
};

Expand All @@ -205,7 +198,6 @@ export const AudioAtom = ({
trackUrl,
kicker,
title,
shouldUseAcast,
duration,
}: Props): JSX.Element => {
const audioEl = useRef<HTMLAudioElement>(null);
Expand All @@ -214,10 +206,6 @@ export const AudioAtom = ({
// update current time and progress bar position
const [currentTime, setCurrentTime] = useState<number>(0);
const [percentPlayed, setPercentPlayed] = useState<number>(0);
// url
const [urlToUse, setUrlToUse] = useState<string>(
buildUrl(trackUrl, shouldUseAcast),
);

useEffect(() => {
const audioElCurrent = audioEl.current;
Expand Down Expand Up @@ -245,7 +233,7 @@ export const AudioAtom = ({
updateCurrentTimeAndPosition,
)
: undefined;
}, [audioEl, setCurrentTime, shouldUseAcast]);
}, [audioEl, setCurrentTime]);

// update duration time
const [durationTime, setDurationTime] = useState<number>(duration);
Expand Down Expand Up @@ -301,11 +289,6 @@ export const AudioAtom = ({
return () => document.removeEventListener('keydown', keyListener);
}, [audioEl, progressBarEl]);

// If Acast is enabled, replace the default url with an ad enabled one
useEffect(() => {
setUrlToUse(buildUrl(trackUrl, shouldUseAcast));
}, [shouldUseAcast, trackUrl]);

const playAudio = () => {
setIsPlaying(true);
void audioEl.current?.play();
Expand All @@ -330,7 +313,7 @@ export const AudioAtom = ({
{/* eslint-disable-next-line jsx-a11y/media-has-caption -- TODO */}
<audio
css={audioElementStyle}
src={urlToUse}
src={trackUrl}
ref={audioEl}
preload="none"
data-component="inarticle audio"
Expand All @@ -340,7 +323,7 @@ export const AudioAtom = ({
>
<p>
Sorry your browser does not support audio - but you can
download here and listen ${urlToUse}
download here and listen ${trackUrl}
</p>
</audio>
<div css={audioControlsStyle}>
Expand Down
6 changes: 4 additions & 2 deletions dotcom-rendering/src/components/AudioAtomWrapper.island.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AudioAtom } from './AudioAtom/AudioAtom';
type Props = {
id: string;
trackUrl: string;
trackUrlWithAds: string;
kicker: string;
title?: string | undefined;
duration: number;
Expand All @@ -26,6 +27,7 @@ type Props = {
export const AudioAtomWrapper = ({
id,
trackUrl,
trackUrlWithAds,
kicker,
title,
duration,
Expand All @@ -37,6 +39,7 @@ export const AudioAtomWrapper = ({
// * ACast *
// *****************
const [shouldUseAcast, setShouldUseAcast] = useState<boolean>(false);

useEffect(() => {
onConsentChange((state) => {
// Should we use ad enabled audio? If so, then set the shouldUseAcast
Expand All @@ -57,11 +60,10 @@ export const AudioAtomWrapper = ({
return (
<AudioAtom
id={id}
trackUrl={trackUrl}
trackUrl={shouldUseAcast ? trackUrlWithAds : trackUrl}
kicker={kicker}
title={title}
duration={duration}
shouldUseAcast={shouldUseAcast}
/>
);
};
8 changes: 4 additions & 4 deletions dotcom-rendering/src/components/AudioPlayerWrapper.island.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Props = {
mediaId: string;
duration?: number;
src: string;
srcWithAds: string;
contentIsNotSensitive: boolean;
isAcastEnabled: boolean;
};
Expand All @@ -23,6 +24,7 @@ type Props = {
export const AudioPlayerWrapper = ({
duration,
src,
srcWithAds,
mediaId,
contentIsNotSensitive,
isAcastEnabled,
Expand All @@ -41,13 +43,11 @@ export const AudioPlayerWrapper = ({
const consentForAcast = getConsentFor('acast', consentState);

if (consentForAcast) {
setFinalSrc(
src.replace('https://', 'https://flex.acast.com/'),
);
setFinalSrc(srcWithAds);
}
});
}
}, [src, contentIsNotSensitive, isAcastEnabled]);
}, [src, srcWithAds, contentIsNotSensitive, isAcastEnabled]);

return <AudioPlayer src={finalSrc} mediaId={mediaId} duration={duration} />;
};
12 changes: 10 additions & 2 deletions dotcom-rendering/src/frontend/schemas/feArticle.json
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,9 @@
"trackUrl": {
"type": "string"
},
"trackUrlWithAds": {
"type": "string"
},
"duration": {
"type": "number"
},
Expand All @@ -955,7 +958,8 @@
"elementId",
"id",
"kicker",
"trackUrl"
"trackUrl",
"trackUrlWithAds"
]
},
"RoleType": {
Expand Down Expand Up @@ -1002,6 +1006,9 @@
"url": {
"type": "string"
},
"urlWithAds": {
"type": "string"
},
"mimeType": {
"type": "string"
},
Expand All @@ -1024,7 +1031,8 @@
}
},
"required": [
"url"
"url",
"urlWithAds"
]
},
"BlockquoteBlockElement": {
Expand Down
3 changes: 3 additions & 0 deletions dotcom-rendering/src/layouts/AudioLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ export const AudioLayout = (props: WebProps | AppProps) => {
!!article.config.switches.acast
}
src={audioData.audioDownloadUrl}
srcWithAds={
audioData.audioDownloadUrlWithAds
}
mediaId={audioData.mediaId}
/>
</Island>
Expand Down
8 changes: 7 additions & 1 deletion dotcom-rendering/src/lib/audio-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type { FEElement } from '../types/content';
export const getAudioData = (
mainMediaElements: FEElement[] | undefined,
):
| { audioDownloadUrl: string; mediaId: string; durationSeconds?: number }
| {
audioDownloadUrl: string;
audioDownloadUrlWithAds: string;
mediaId: string;
durationSeconds?: number;
}
| undefined => {
const audioBlockElement = mainMediaElements?.find(
(element) =>
Expand All @@ -17,6 +22,7 @@ export const getAudioData = (
const total = (isNaN(mins) ? 0 : mins) * 60 + (isNaN(secs) ? 0 : secs);
return {
audioDownloadUrl: audioBlockElement.assets[0].url,
audioDownloadUrlWithAds: audioBlockElement.assets[0].urlWithAds,
mediaId: audioBlockElement.id,
durationSeconds: total > 0 ? total : undefined,
};
Expand Down
1 change: 1 addition & 0 deletions dotcom-rendering/src/lib/renderElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export const renderElement = ({
<AudioAtomWrapper
id={element.id}
trackUrl={element.trackUrl}
trackUrlWithAds={element.trackUrlWithAds}
kicker={element.kicker}
title={element.title}
duration={element.duration}
Expand Down
12 changes: 10 additions & 2 deletions dotcom-rendering/src/model/block-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@
"trackUrl": {
"type": "string"
},
"trackUrlWithAds": {
"type": "string"
},
"duration": {
"type": "number"
},
Expand All @@ -428,7 +431,8 @@
"elementId",
"id",
"kicker",
"trackUrl"
"trackUrl",
"trackUrlWithAds"
]
},
"RoleType": {
Expand Down Expand Up @@ -475,6 +479,9 @@
"url": {
"type": "string"
},
"urlWithAds": {
"type": "string"
},
"mimeType": {
"type": "string"
},
Expand All @@ -497,7 +504,8 @@
}
},
"required": [
"url"
"url",
"urlWithAds"
]
},
"BlockquoteBlockElement": {
Expand Down
2 changes: 2 additions & 0 deletions dotcom-rendering/src/types/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface AudioAtomBlockElement {
kicker: string;
title?: string;
trackUrl: string;
trackUrlWithAds: string;
duration: number;
coverUrl: string;
role?: RoleType;
Expand Down Expand Up @@ -1004,6 +1005,7 @@ export interface VideoAssets {

interface AudioAsset {
url: string;
urlWithAds: string;
mimeType?: string;
fields?: {
durationMinutes?: string;
Expand Down
Loading