Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gallery): HLS -> HLS.Light #1510

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions .changeset/strange-coats-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@alfalab/core-components-gallery': patch
---

- Импорт HLS заменён на light версию, только энкодер без дополнительных обвязок.
- Подгрузка модуля перенесена на время после загрузки основного js, для лучшей клиентской доступности основного приложения.
37 changes: 31 additions & 6 deletions packages/gallery/src/components/image-viewer/video/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import React, { MouseEvent, ReactEventHandler, useContext, useEffect, useRef } from 'react';
import React, {
MouseEvent,
ReactEventHandler,
useContext,
useEffect,
useRef,
useState,
} from 'react';
import cn from 'classnames';
import Hls from 'hls.js';
import type HlsType from 'hls.js/dist/hls.light.mjs';
import type { ErrorData, Events } from 'hls.js/dist/hls.light.mjs';

import { Circle } from '@alfalab/core-components/icon-view/circle';
import PlayCompactMIcon from '@alfalab/icons-glyph/PlayCompactMIcon';
Expand All @@ -20,6 +28,7 @@ type Props = {
export const Video = ({ url, index, className, isActive }: Props) => {
const playerRef = useRef<HTMLVideoElement>(null);
const timer = useRef<ReturnType<typeof setTimeout>>();
const [HLSSupported, setHLSSupported] = useState<boolean>(true);

const { setImageMeta, mutedVideo, view, playingVideo, setPlayingVideo, setHideNavigation } =
useContext(GalleryContext);
Expand All @@ -32,10 +41,22 @@ export const Video = ({ url, index, className, isActive }: Props) => {
}, [index]);

useEffect(() => {
const hls = new Hls();
let hls: HlsType;

if (Hls.isSupported()) {
hls.on(Hls.Events.ERROR, (_, data) => {
async function initHls() {
const { default: Hls } = await import(
/* webpackChunkName: "hls-js-video" */ 'hls.js/dist/hls.light.mjs'
);
Copy link
Contributor Author

@denisx denisx Dec 21, 2024

Choose a reason for hiding this comment

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

300 Kb в отдельный чанк


hls = new Hls();

if (!Hls.isSupported()) {
setHLSSupported(false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

проверка до загрузки


return;
}

hls.on(Hls.Events.ERROR, (_: Events.ERROR, data: ErrorData) => {
if (data.fatal) {
switch (data.type) {
case Hls.ErrorTypes.MEDIA_ERROR:
Expand All @@ -52,11 +73,14 @@ export const Video = ({ url, index, className, isActive }: Props) => {
});

hls.loadSource(url);

if (playerRef.current) {
hls.attachMedia(playerRef.current);
}
}

initHls().catch();

Copy link
Collaborator

Choose a reason for hiding this comment

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

может добавим количество попыток загрузки?

return () => {
if (hls) {
hls.destroy();
Expand All @@ -65,6 +89,7 @@ export const Video = ({ url, index, className, isActive }: Props) => {
clearTimeout(timer.current);
}
};

/* eslint-disable-next-line react-hooks/exhaustive-deps */
}, [url, index]);

Expand Down Expand Up @@ -142,7 +167,7 @@ export const Video = ({ url, index, className, isActive }: Props) => {
playsInline={true}
muted={mutedVideo}
loop={true}
src={Hls.isSupported() ? undefined : url}
src={HLSSupported ? undefined : url}
className={cn(styles.video, { [styles.mobile]: view === 'mobile' }, className)}
>
<track kind='captions' />
Expand Down
6 changes: 6 additions & 0 deletions packages/gallery/src/declaration.d.ts
Copy link
Collaborator

Choose a reason for hiding this comment

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

перенести в hls.d.ts как в module.d.ts

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module 'hls.js/dist/hls.light.mjs' {
import Hls, { ErrorData, Events } from 'hls';

export default Hls;
export { ErrorData, Events };
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
}
},
"exclude": ["node_modules", "dist", "**/*.stories*", "**/*.test*"],
"include": ["./typings"]
"include": ["./typings", "**/declaration.d.ts"]
}
Loading