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(Directives): Add directive SSR fallback #30

Merged
merged 1 commit into from
Dec 21, 2018
Merged

Conversation

zickat
Copy link
Contributor

@zickat zickat commented Oct 12, 2018

The goal is to match the vue ssr doc and allow the use of directives on server side https://ssr.vuejs.org/api/#directives.
The usage will look like the nuxt one. https://fr.nuxtjs.org/api/configuration-render/

Usage

If you have a directive which adds red color to a div

export default {
    inserted(el) {
        el.style.color = 'red';
    }
}
Vue.directive('red', red);
<div v-red> My red text </div>

This directive can't work with SSR. So we need a fallback.

SSR directive of red : red-server.js

/**
* @param node the VNode where we applied the directive
* @param dir properties of the directive
*/
module.exports = function(node, dir) {
  // get the current style of the node. If it doesn't have one, we create it
  const style = node.data.style || (node.data.style = {})
  // the style can be an array or an object
    if (Array.isArray(style)) {
     //if it's an array, we add a new style in it
      style.push({ color: 'red' })
    } else {
     // else we just set the color to red
      style.color = 'red'
    }
};

Finally, register the directive inside vue.config.js

const red = require('red-server');

module.exports = {
  pluginOptions: {
    ssr: {
      directives: {
        red,
      },
    },
  },
};

@zickat zickat reopened this Nov 14, 2018
@Akryum Akryum merged commit f86f1a4 into Akryum:master Dec 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants