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

Initially render with a low quality thumbnail for loading speeds, then load a higher res thumbnail when idle #62

Open
FunctionDJ opened this issue Apr 2, 2022 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@FunctionDJ
Copy link
Contributor

Title pretty much says it all.
I was curious so i wrote this custom hook who's return value i pass to the poster prop:

const useKindaLazyYouTubeThumbnail = () => {
  const [imgRes, setImgRes] = useState<"mqdefault" | "maxresdefault">("mqdefault");

  useEffect(() => {
    setTimeout(() => {
      setImgRes("maxresdefault");
    }, 5000);
  }, []);

  return imgRes;
};

What do you guys think? I noticed that changing the poster will show a blank thumbnail for a brief moment because the new thumbnail needs to get downloaded. I've already tried putting a prefetch element into the , but couldn't get it to work yet (it doesn't create a request):

<link rel="prefetch" href={`https://i.ytimg.com/vi_webp/${id}/maxresdefault.webp`}/>

By the way i think it would be cool if the type for the poster prop was importable. You can technically get to it through Parameters<typeof LiteYouTubeEmbed>["0"]["poster"] but yeah i'll let it speak for itself lmao

I'll make a PR for that little export.

@ibrahimcesar
Copy link
Owner

Thanks @FunctionDJ

@LichLord91
Copy link
Contributor

@FunctionDJ used this hook today, clever workaround! I like it.

@FunctionDJ
Copy link
Contributor Author

This hook has a disadvantage: the thumbnail will briefly blink because when the timeout runs, the state update will immediately unload the old image and then start loading the new image, so there's a "downtime". I was playing around with dynamically adding a <link> prefetch element to preload the bigger thumbnail but couldn't get that to work.
That's why i didn't end up using the hook i wrote above. Maybe someone has a better solution :)

@ibrahimcesar ibrahimcesar added help wanted Extra attention is needed triage enhancement New feature or request and removed triage labels Nov 23, 2023
@ibrahimcesar ibrahimcesar pinned this issue Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants