npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-document

v0.1.2

Published

A 1kb document manager for Vue.js

Downloads

42

Readme

vue-document npm

A document manager for Vue.js. Think react-helmet but for Vue.js.

Features:

Installation

Get it directly from NPM:

npm i --save-dev vue-document

or through unpkg:

<script src="https://unpkg.com/vue-document></script>
(in this case vue-document will be available as window.VueDocument).

Usage

NOTE: Any values specified in children components get automatically merged in
(overriding existing entries (in a "deep merge" fashion)).

Client-Side

<template>
  <div id="app">{{ message }}</div>
</template>

<script>
  import Vue from 'vue'
  import VueDocument from 'vue-document'
  
  Vue.use(VueDocument, {
    injector (document) {
      // "this" is referencing Vue component which caused "document" metadata to change
      const metadata = this.$root.$document
      document.title = metadata.head.title
      document.querySelector('meta[name="description"]').content = 
        metadata.meta.find((meta) => meta.name === 'description').content
    }
  })
  
  export default {
    document: {
      head: {
        title: 'Custom Title',
        meta: [
          {name: 'description', content: 'Custom Description'}
        ]
      }
    },
    el: '#app',
    data: {
      message: 'Hello Vue!'
    }
  }
</script>

In the example above, instead of writing injector ourselves we could use vue-document-injector-title.js and vue-document-injector-meta.js (later is used for custom <meta/> injection, not just description). injector handler could then be simplified to

  import VueDocument, {titleInjector, metaInjector} from 'vue-document'
  
  Vue.use(VueDocument, {
    injector: [titleInjector, metaInjector]
  })

Live Demo @ JS Bin

Server-Side

The example below is not using bundleRenderer just to keep things simple.
There is nothing stopping you from doing that.

const Vue = require('vue')
const VueDocument = require('vue-document')

Vue.use(VueDocument)

const vm = new Vue({
  document: {
    head: {
      title: 'Custom Title',
      meta: [
        {name: 'description', content: 'Custom Description'}
      ]
    }
  },
  render (h) {
    return h('div', {attrs: {id: 'app'}}, this.message)
  }, 
  el: '#app',
  data: {
    message: 'Hello Vue!'
  }
})

const renderer = require('vue-server-renderer').createRenderer()
renderer.renderToString(vm, (err, html) => {
  if (err) {
    throw err
  }
  const metadata = vm.$document
  console.log(
    `
      <!doctype html>
      <title>${metadata.head.title}</title>
      <meta name="description" content="${
        metadata.meta.find((meta) => meta.name === 'description').content
      }">
      ${html}
    `
  )   
})

Another option is to inject vm.$document into the existing html (which can be helpful if page is generated by html-webpack-plugin or something similar):

const Vue = require('vue')
const VueDocument = require('vue-document')
const {titleInjector, metaInjector} = VueDocument

Vue.use(VueDocument, {
  injector: [titleInjector, metaInjector]
})

const window = require('domino')
  .createWindow('<!doctype html><title>*</title>')
const document = window.document

const vm = ...
vm.$documentForceUpdate(document)
 
console.log(document.documentElement.outerHTML)

License

MIT