fix(js): tweak how the vjs hook is fired and url is created

This commit is contained in:
Gabe Kangas 2024-11-15 13:25:06 -08:00
parent d8abe74e5a
commit 70bbcb97b8

View file

@ -29,23 +29,31 @@ export const VideoJS: FC<VideoJSProps> = ({ options, onReady }) => {
player.autoplay(options.autoplay); player.autoplay(options.autoplay);
player.src(options.sources); player.src(options.sources);
} }
}, [options, videoRef]);
React.useEffect(() => {
videojs.getPlayer(videoRef.current).on('xhr-hooks-ready', () => { videojs.getPlayer(videoRef.current).on('xhr-hooks-ready', () => {
const cachebusterRequestHook = o => { const cachebusterRequestHook = o => {
let { uri } = o; const { uri } = o;
let updatedURI = uri;
if (o.uri.match('m3u8')) { if (o.uri.match('m3u8')) {
const u = uri.startsWith('http')
? new URL(uri)
: new URL(uri, window.location.protocol + window.location.host);
const cachebuster = Math.random().toString(16).slice(2, 8); const cachebuster = Math.random().toString(16).slice(2, 8);
uri = `${o.uri}?cachebust=${cachebuster}`; u.searchParams.append('cachebust', cachebuster);
updatedURI = u.toString();
} }
return { return {
uri, ...o,
uri: updatedURI,
}; };
}; };
( (
videojs.getPlayer(videoRef.current).tech({ IWillNotUseThisInPlugins: true }) as any videojs.getPlayer(videoRef.current).tech({ IWillNotUseThisInPlugins: true }) as any
)?.vhs.xhr.onRequest(cachebusterRequestHook); )?.vhs.xhr.onRequest(cachebusterRequestHook);
}); });
}, [options, videoRef]); }, []);
return ( return (
<div data-vjs-player> <div data-vjs-player>