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

@pigs/plugin-vue-static

v12.0.6

Published

You might be familar with building a SPA using Vue, Vue Router and Pigs (webpack) already, here we're introducing a Pigs plugin that pre-renders each page of your SPA using vue-server-renderer at build time.

Downloads

3

Readme

@pigs/plugin-vue-static

You might be familar with building a SPA using Vue, Vue Router and Pigs (webpack) already, here we're introducing a Pigs plugin that pre-renders each page of your SPA using vue-server-renderer at build time.

Install

Install this plugin in a Vue project:

yarn add @pigs/plugin-vue-static --dev

You should also have vue and vue-template-compiler installed in your project.

Now you can load this plugin in pigs.config.js:

module.exports = {
  entry: './src/index.js',
  plugins: [
    {
      resolve: '@pigs/plugin-vue-static'
    }
  ]
}

How to use

Entry file

With this plugin, you should export a function which returns an object in the entry file:

// src/index.js
import Router from 'vue-router'

export default () => {
  const router = new Router({
    mode: 'history',
    routes: [
      {
        path: '/',
        component: () => import('./pages/index.vue')
      }
    ]
  })

  // Returned object will be used as the options
  // for root Vue instance
  return {
    router
  }
}

Generate static website

By default it only render HTML for route / because we don't know what pages you want to statically render at build time. Here you can use staticRoutes to let us know:

// pigs.config.js
module.exports = {
  entry: './src/index.js',
  plugins: [
    {
      resolve: '@pigs/plugin-vue-static',
      options: {
        staticRoutes: ['/user/egoist', '/user/cristiano']
      }
    }
  ]
}

Then you will get index.html user/egoist/index.html and user/cristiano/index.html.

Options

staticRoutes

  • Type: string[]
  • Default: ['/']

resourceHints

  • Type: boolean
  • Default: true

Add prefetch and preload resource hints to <head>.

FAQ

How do I modify webpack config for specific build?

The chainWebpack option actually has a second argument:

// pigs.config.js
module.exports = {
  chainWebpack(config, { type }) {
    console.log(type)
  }
}

Without this plugin, type always equals to 'client'. When using vue-static plugin it will be 'server' for server build.

Notably:

  • There's only client build when running pigs --serve since it is still bundled as SPA.
  • Without the --serve flag Pigs will generate two builds, client build and server build.

How do I write code applying to client build only?

Via the environment variable in your app code:

if (process.server) {
  // this is the server build
}

if (process.client) {
  // alias: process.browser
  // this is the client build
  // you can safely access browser API here
  // like `window.document`
}

How do I manipulate head tags?

Via head option in your Vue component:

<script>
export default {
  head: {
    title: 'Page Title'
  }
}
</script>

This feature is powered by vue-meta under the hood, but here we are using head instead of the default metaInfo option.

Showcase

A list of websites built with vue-static:

  • Vue Land: website for Vue Land the official Vue.js Discord Community.