Use optimal self-hosted video for user by width#15561
Use optimal self-hosted video for user by width#15561
Conversation
fa4b4f0 to
be2b4d6
Compare
be2b4d6 to
0e973f4
Compare
0e973f4 to
24275c2
Compare
|
Hello 👋! When you're ready to run Chromatic, please apply the You will need to reapply the label each time you want to run Chromatic. |
dac04f1 to
5fc414a
Compare
c8c8f1a to
9313fcc
Compare
9313fcc to
9331026
Compare
| "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": { |
There was a problem hiding this comment.
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.
davidfurey
left a comment
There was a problem hiding this comment.
Great to share this code between articles and fronts. Sorry, I haven't reviewed the whole thing but have made a couple of comments.
dotcom-rendering/src/lib/video.ts
Outdated
| 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; | ||
| }); | ||
|
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
9331026 to
5621360
Compare
dotcom-rendering/src/lib/video.ts
Outdated
|
|
||
| return orderedSources[0]; | ||
| }; |
There was a problem hiding this comment.
Is it easier to reason about if you remove the sort entirely?
| 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 | |
| } | |
| }); | |
| }; |
There was a problem hiding this comment.
Ahh good idea. I agree a reducer over a sort makes a lot more sense
| return DEFAULT_ASPECT_RATIO; | ||
| } | ||
|
|
||
| return firstSource.width / firstSource.height; |
There was a problem hiding this comment.
Why have you opted to calculate the aspect ratio rather than taking it from the video asset?
There was a problem hiding this comment.
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.
b2b3469 to
b735be8
Compare
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.