Skip to content

Use optimal self-hosted video for user by width#15561

Open
domlander wants to merge 10 commits intomainfrom
doml/sh-optimise-video-source
Open

Use optimal self-hosted video for user by width#15561
domlander wants to merge 10 commits intomainfrom
doml/sh-optimise-video-source

Conversation

@domlander
Copy link
Contributor

@domlander domlander commented Mar 19, 2026

What does this change?

Selects the optimal video from the list of video sources. Selects the best set of dimensions for the users screen size.

Use the same logic when extracting sources from assets for both fronts and articles.

Why?

To serve smaller videos to users without any reduction in video quality.

Logic for extracting sources is likely to be the same for both fronts and articles. Combining the logic into one function ensures that changes to one will affect the other.

@domlander domlander self-assigned this Mar 19, 2026
@domlander domlander added run_chromatic Runs chromatic when label is applied fronts + curation maintenance Departmental tracking: maintenance work, not a fix or a feature labels Mar 19, 2026
@github-actions github-actions bot removed the run_chromatic Runs chromatic when label is applied label Mar 19, 2026
@github-actions
Copy link

github-actions bot commented Mar 19, 2026

@domlander domlander force-pushed the doml/sh-optimise-video-source branch from fa4b4f0 to be2b4d6 Compare March 23, 2026 15:22
@github-actions
Copy link

github-actions bot commented Mar 23, 2026

@domlander domlander force-pushed the doml/sh-optimise-video-source branch from be2b4d6 to 0e973f4 Compare March 23, 2026 15:48
@domlander domlander changed the title WIP Pick best self-hosted video for user by dimensions Use optimal self-hosted video for user by width Mar 23, 2026
@domlander domlander added the run_chromatic Runs chromatic when label is applied label Mar 23, 2026
@github-actions github-actions bot removed the run_chromatic Runs chromatic when label is applied label Mar 23, 2026
@domlander domlander force-pushed the doml/sh-optimise-video-source branch from 0e973f4 to 24275c2 Compare March 25, 2026 09:29
@domlander domlander added the run_chromatic Runs chromatic when label is applied label Mar 25, 2026
@github-actions github-actions bot removed the run_chromatic Runs chromatic when label is applied label Mar 25, 2026
@domlander domlander marked this pull request as ready for review March 25, 2026 09:37
@github-actions
Copy link

Hello 👋! When you're ready to run Chromatic, please apply the run_chromatic label to this PR.

You will need to reapply the label each time you want to run Chromatic.

Click here to see the Chromatic project.

@domlander domlander force-pushed the doml/sh-optimise-video-source branch from dac04f1 to 5fc414a Compare March 25, 2026 12:41
@domlander domlander added the run_chromatic Runs chromatic when label is applied label Mar 25, 2026
@github-actions github-actions bot removed the run_chromatic Runs chromatic when label is applied label Mar 25, 2026
@domlander domlander added the run_chromatic Runs chromatic when label is applied label Mar 25, 2026
@github-actions github-actions bot removed the run_chromatic Runs chromatic when label is applied label Mar 25, 2026
@domlander domlander force-pushed the doml/sh-optimise-video-source branch from c8c8f1a to 9313fcc Compare March 25, 2026 13:08
@domlander domlander added the run_chromatic Runs chromatic when label is applied label Mar 25, 2026
@github-actions github-actions bot removed the run_chromatic Runs chromatic when label is applied label Mar 25, 2026
@domlander domlander force-pushed the doml/sh-optimise-video-source branch from 9313fcc to 9331026 Compare March 25, 2026 16:00
@domlander domlander added the run_chromatic Runs chromatic when label is applied label Mar 25, 2026
@github-actions github-actions bot removed the run_chromatic Runs chromatic when label is applied label Mar 25, 2026
@domlander domlander requested a review from abeddow91 March 25, 2026 16:27
"footballMatch": {
"$ref": "#/definitions/{id:string;homeTeam:{name:string;id:string;scorers:string[];codename:string;players:{name:string;id:string;position:string;lastName:string;substitute:boolean;timeOnPitch:string;shirtNumber:string;events:{eventTime:string;eventType:string;}[];}[];possession:number;shotsOn:number;shotsOff:number;corners:number;fouls:number;colours:string;crest:string;score?:number;};awayTeam:{name:string;id:string;scorers:string[];codename:string;players:{name:string;id:string;position:string;lastName:string;substitute:boolean;timeOnPitch:string;shirtNumber:string;events:{eventTime:string;eventType:string;}[];}[];possession:number;shotsOn:number;shotsOff:number;corners:number;fouls:number;colours:string;crest:string;score?:number;};status:string;comments?:string;}"
},
"matchInfo": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore these changes - these are temporary to allow CI to run. It is not intended that these will be a part of this PR when it gets merged.

Copy link
Member

@davidfurey davidfurey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to share this code between articles and fronts. Sorry, I haven't reviewed the whole thing but have made a couple of comments.

Comment on lines +102 to +111
const orderedSources = sources.sort((a, b) => {
if (a.width < screenWidth && b.width < screenWidth) {
return a.width < b.width ? 1 : -1;
}
if (a.width > screenWidth && b.width > screenWidth) {
return a.width > b.width ? 1 : -1;
}
return a.width < screenWidth ? 1 : -1;
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has taken quite a while for me to convince myself this works. If it is obvious to others then I'll take it as a learning. If not, can you express this in a more obvious way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a good point. I have tried to make it a bit clearer to what is going on, but I find that sorting algorithms are inherently complicated in Typescript, with their a's and b's and -1 meaning "put that one first", instead of 1. I have included plenty of tests to assert that this algorithm is working correctly, so hopefully that helps.

@domlander domlander force-pushed the doml/sh-optimise-video-source branch from 9331026 to 5621360 Compare March 25, 2026 17:47
Comment on lines +120 to +122

return orderedSources[0];
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it easier to reason about if you remove the sort entirely?

Suggested change
return orderedSources[0];
};
const findOptimalSource = (
sources: Source[],
screenWidth: number,
): Source | undefined => {
if (sources.length === 0) return undefined;
return sources.reduce((a, b) => {
if (a.width < screenWidth || b.width < screenWidth) {
return a.width > b.width ? a : b; // take the larger source
} else {
return a.width < b.width ? a : b; // take the smaller source
}
});
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh good idea. I agree a reducer over a sort makes a lot more sense

@domlander domlander added the run_chromatic Runs chromatic when label is applied label Mar 26, 2026
@github-actions github-actions bot removed the run_chromatic Runs chromatic when label is applied label Mar 26, 2026
return DEFAULT_ASPECT_RATIO;
}

return firstSource.width / firstSource.height;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have you opted to calculate the aspect ratio rather than taking it from the video asset?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll convert the string into a number, so that the aspect ratio defined on the video source is the source of truth for the aspect ratio passed into the component.

@domlander domlander force-pushed the doml/sh-optimise-video-source branch from b2b3469 to b735be8 Compare March 26, 2026 10:51
@domlander domlander added the run_chromatic Runs chromatic when label is applied label Mar 26, 2026
@github-actions github-actions bot removed the run_chromatic Runs chromatic when label is applied label Mar 26, 2026
@domlander domlander added the run_chromatic Runs chromatic when label is applied label Mar 26, 2026
@github-actions github-actions bot removed the run_chromatic Runs chromatic when label is applied label Mar 26, 2026
@domlander domlander requested a review from abeddow91 March 26, 2026 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fronts + curation maintenance Departmental tracking: maintenance work, not a fix or a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants