Giter Club home page Giter Club logo

vue-lazytube's Introduction

Vue Lazytube

Embed a YouTube or Vimeo player easily and lazy load the video to save resources and reduces initial load size. Useful when performance and page size is important or for sites with many embedded videos.

For a simple example page with 10 videos, vue-lazytube will reduce loadtime by 7x and memory usage by 2-3x.

Demo online | Tutorial | Sponsor

GitHub release License downloads filesize


Table of Content

Features

  • reduces initial load size by ~1.1 MB per video
  • fully responsive and customizable through props
  • provides methods to control (play, stop, pause and reset) embedded videos
  • provide options to set up custom title and preview of embedded videos
  • platform supported: Youtube and Vimeo

Demo

view vue-lazytube

Installation

With a build systems

$ npm install --save vue-lazytube
$ yarn add vue-lazytube

To make the plugin available globally

In your main.js:

import LazyTube from "vue-lazytube";

Vue.use(LazyTube);

To include only in specific components

import { LazyYoutube, LazyVimeo } from "vue-lazytube";

export default {
    name: 'YourComponent',
    components: {
        LazyYoutube,
        LazyVimeo
    },
}

Usage

<template>
    <!-- Youtube Embed -->
    <LazyYoutube src="https://www.youtube.com/watch?v=TcMBFSGVi1c" />

    <!-- Vimeo Embed -->
    <LazyVimeo src="https://player.vimeo.com/video/64654583" />
</template>

API

Props

Name Type Default Value Description Required
src String `` To load video and iframe, should be Youtube/Vimeo video link. true
aspectRatio String 16:9 Maintaining the aspect ratio of video, perfect for responsively handling video embeds based on the width of the parent, should be in *:* format. e.g, 1:1, 4:3, 16:9 and 21:9. false
maxWidth String 560px Defines maximum width of video container. false
showTitle Boolean true Defines whether to show video title on top. false
autoplay Boolean false Defines whether to load the iframe as the page loads (not recommended) false
thumbnailQuality String standard Defines the quality of thumbnail, should be one of the following default, medium, high, standard or maxres false
iframeClass String ly-iframe Defines the class on iframe element false
customTitle String `` Defines the custom title of the video false
customThumbnail String `` Defines the custom thumbnail image link of the video false

Slots

This Component provides some slots. You can use the named slot to render custom play button and loader:

Slot Description
button Slot gives an ability to provide custom play button
loader Slot gives an ability to provide custom loader

Methods

These methods let you control the player using JavaScript, so you can perform the operations like play, stop, pause and reset.

The user's browser must support the HTML5 postMessage feature. Most modern browsers support postMessage.

Name Type Usage
playVideo function this.$refs["lazyVideo"]['playVideo']()
stopVideo function this.$refs["lazyVideo"]['stopVideo']()
pauseVideo function this.$refs["lazyVideo"]['pauseVideo']()
resetView function this.$refs["lazyVideo"]['resetView']()

Note: Must Replace lazyVideo with your ref.

Example

<template>
    <LazyYoutube
        ref="lazyVideo"
        src="https://www.youtube.com/watch?v=TcMBFSGVi1c"
    />
    
    <button @click="handleClick('playVideo')">Play</button>
    <button @click="handleClick('stopVideo')">Stop</button>
    <button @click="handleClick('pauseVideo')">Pause</button>
    <button @click="handleClick('resetView')">Reset</button>
</template>

<script>
    export default {
      name: "YourComponent",
      components: {},
      methods: {
        handleClick(event) {
          this.$refs["lazyVideo"][event]();
        },
      },
    };
</script>

Open Source License

You may use it under the terms of the MIT Licenses

Buy Me a Coffee

Support me on Patreon

vue-lazytube's People

Contributors

deepsource-autofix[bot] avatar deepsourcebot avatar seeratawan01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vue-lazytube's Issues

Support Vue3

Good library! I'd like to use it, but my project is on vue 3. It would be nice if you can make this library available for Vue 3. Thanks!

Video need double click to play on Chrome

Describe the bug
The video doesnt play when the play button is clicked, it only shows the real iframe, needing an second click to actually play it

Seems to happen only in Chrome

To Reproduce
Steps to reproduce the behavior:

  1. Click on a video
  2. Click again to actually play the video

Expected behavior
Expected to actually play the video in the first click

Desktop (please complete the following information):

  • OS: Ubuntu 20.04
  • Browser Chrome only
  • Version 96.0.4664.110

Additional context
Only happens on Chrome

Custom events, handle unavailable/locked status of video

Is your feature request related to a problem? Please describe.

  • custom events to get info if video is started of stopped
  • events with response if video is:
    AVAILABLE / LOCKED (as private video) / NOT FOUND to conditionally hide the component in Vue

Disable/Enable flag for more videos or recommended videos

Hi, good day!

The current functionality when playing a video using the component. When you played and stop it will show the "More videos" thumbnail which should be possible optional? If possible there's flag showHideThumbnail=true/false. It's just a suggestion. Thanks :)

Init fail

Describe the bug
When start project. The console will show this error
Uncaught (in promise) TypeError: Cannot read property '_c' of undefined

Screenshots
截圖 2021-05-09 上午1 16 35

Desktop (please complete the following information):

  • OS: macOS big sur
  • Browser chrome
  • Version 90.0.4430.93

Additional context
Vue version: 3.0.5

can't install

Hi! I can't install vue-lazytube. I enter it into the terminal: npm install --save vue-lazytube. I get an error:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/vue
npm ERR! vue@"^3.0.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer vue@"^2.6.11" from [email protected]
npm ERR! node_modules/vue-lazytube
npm ERR! vue-lazytube@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

how can I solve this problem?

Embed Info Request

Good library, and this saved me from writing the same code myself. I do recommend adding the option to not load embed information on mount b/c some (such as myself w/ vimeo) just want to 100% lazy load the iframe video with no initial requests. Also it appears you have a generated dist folder in the Repo.

Problems with using v-slot:button

Hello, I have a problem when I use v-slot:button.

this is how i use v-slot:button:

    <LazyYoutube
        ref="youtubeLazyVideo"
        :src="youtubeLink"
        max-width="720px"
        aspect-ratio="16:9"
        thumbnail-quality="standard"
        :loop=true
        :autoplay=false
    >

      <template v-slot:button>
        <button @click="handleClick('playVideo', 'youtubeLazyVideo')">Play</button>
      </template>
    </LazyYoutube>

My button plays a video when clicked, but at the same time two identical iframes are created, this causes some problems.

Adding this condition helped me: this.iframeEl === null
in the LazyYoutube.vue file in the handleClick() method

    handleClick() {
      this.clicked = true;
      if (this.fetchingInfo === false && !this.onceLoaded && this.isVideoFound) {
        if (this.iframeEl === null) {
          this.initiateIframe(this.autoplay, this.loop);
        }
      }
    },

But I don't know how correct this is. And I would not want to change the library code myself.

Not working with Nuxt3

When trying to use this library in my Nuxt 3 app, I can't get it working.
The video (Vimeo) is not loaded and gives me error:
Uncaught (in promise) TypeError: can't access property "_c", t3._self is undefined

To Reproduce
Create a vue component in Nuxt 3.

  <div>
    <LazyVimeo :src="`https://player.vimeo.com/video/${id}`" />
  </div>
</template>

<script setup>
import { LazyVimeo } from "vue-lazytube";

const props = defineProps({
  id: {
    type: String,
    required: true,
  },
});
</script>

Expected behavior
Is that the video loads.

Loading mixed (insecure) display content

Describe the bug
Thumbnails are loaded via http

To Reproduce
Load any video in the player and inspect console via Firefox Browser

Expected behavior
Load thumbnails via https

Screenshots
Screenshot 2021-09-11 at 11 51 59

Desktop (please complete the following information):

  • OS: macos
  • Browser Firefox
  • Version 91

recent update broke server-side rendering

Describe the bug
Upgrade from 1.0.4 to latest 1.1.1 (can bisect tomorrow) broke SSR:

ReferenceError: document is not defined
     at c (node_modules/vue-lazytube/dist/vue-lazytube.umd.min.js:2:83266)
     at v (node_modules/vue-lazytube/dist/vue-lazytube.umd.min.js:2:84526)
     at u (node_modules/vue-lazytube/dist/vue-lazytube.umd.min.js:2:83190)
     at t.exports (node_modules/vue-lazytube/dist/vue-lazytube.umd.min.js:2:84859)
     at assets/js/vendors~page--src--pages--index-vue.9796b736.js:13768:87012

To Reproduce
Steps to reproduce the behavior:

  1. Use vue-lazytube in nodejs rendering
    (Can provide more details tomorrow)

Expected behavior
Working SSR.
Maybe it is out of scope for you and I have to disable it on server side, but would be great if not :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.