Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@storyblok/vue

Package Overview
Dependencies
Maintainers
5
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@storyblok/vue

Storyblok directive for get editable elements.

  • 2.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
increased by3.93%
Maintainers
5
Weekly downloads
 
Created
Source
Storyblok Logo

@storyblok/vue

The Vue plugin you need to interact with Storyblok API and enable the Real-time Visual Editing Experience.


Storyblok Vue npm

Follow @Storyblok Follow @Storyblok

Note: This plugin is for Vue 2. Check out the docs for Vue 3 version.

🚀 Usage

If you are first-time user of the Storyblok, read the Getting Started guide to get a project ready in less than 5 minutes.

Installation

Install @storyblok/vue

npm install --save-dev @storyblok/vue
# yarn add -D @storyblok/vue

Register the plugin on your application (usually in main.js), add the apiPlugin and add the access token of your Storyblok space:

import Vue from "vue";
import { StoryblokVue, apiPlugin } from "@storyblok/vue";
import App from "./App.vue";

Vue.use(StoryblokVue, {
  accessToken: "<your-token>",
  use: [apiPlugin],
});

That's it! All the features are enabled for you: the Api Client for interacting with Storyblok CDN API, and Storyblok Bridge for real-time visual editing experience.

You can enable/disable some of these features if you don't need them, so you save some KB. Please read the "Features and API" section

Composition API

Install @vue/composition-api and register it in the application:

// main.js
import VueCompositionAPI from "@vue/composition-api";
Vue.use(VueCompositionAPI);

To use script setup, install unplugin-vue2-script-setup. Depending on your setup, the configuration is different. For example, in Vite:

// vite.config.js
import { createVuePlugin } from "vite-plugin-vue2";
import ScriptSetup from "unplugin-vue2-script-setup/vite";

export default {
  plugins: [createVuePlugin(), ScriptSetup()],
};
From a CDN

Install the file from the CDN and access the methods via window.storyblokVue:

<script src="https://unpkg.com/@storyblok/vue"></script>

Getting started

@storyblok/vue does three actions when you initialize it:

  • Provides a storyblokApi object in your app, which is an instance of storyblok-js-client
  • Loads Storyblok Bridge for real-time visual updates
  • Provides a v-editable directive to link editable components to the Storyblok Visual Editor
1. Fetching Content

Inject storyblokApi when using Composition API:

<template>
  <div>
    <p v-for="story in stories" :key="story.id">{{ story.name }}</p>
  </div>
</template>

<script setup>
  import { useStoryblokApi } from "@storyblok/vue";

  const storyblokApi = useStoryblokApi();
  const { data } = await storyblokApi.get("cdn/stories", { version: "draft" });
</script>

Note: you can skip using apiPlugin if you prefer your own method or function to fetch your data.

2. Listen to Storyblok Visual Editor events

Use useStoryBridge to get the new story every time is triggered a change event from the Visual Editor. You need to pass the story id as first param, and a callback function as second param to update the new story:

<script setup>
  import { onMounted } from "vue";
  import { useStoryblokBridge, useStoryblokApi } from "@storyblok/vue";

  const storyblokApi = useStoryblokApi();
  const { data } = await storyblokApi.get("cdn/stories", { version: "draft" });
  const state = reactive({ story: data.story });

  onMounted(() => {
    useStoryblokBridge(state.story.id, story => (state.story = story));
  });
</script>

You can pass Bridge options as a third parameter as well:

useStoryblokBridge(state.story.id, (story) => (state.story = story), {
  resolveRelations: ["Article.author"],
});

For every component you've defined in your Storyblok space, add the v-editable directive with the blok content:

<template>
  <div v-editable="blok"><!-- ... --></div>
</template>

Where blok is the actual blok data coming from Storblok's Content Delivery API.

Check out the playground for a full example.

Options API

You can use Options API as well, accessing the api client via this.$storyblokApi:

import { useStoryblokBridge } from "@storyblok/vue";

export default {
  data: () => ({
    story: [],
  }),
  async created() {
    const { data } = this.$storyblokApi.get(/* ... */);
    this.story = data.story;
  },
  mounted() {
    useStoryblokBridge(this.story.id, (evStory) => (this.story = evStory));
  },
};

Features and API

You can choose the features to use when you initialize the plugin. In that way, you can improve Web Performance by optimizing your page load and save some bytes.

Storyblok API

You can use an apiOptions object. This is passed down to the (storyblok-js-client config object](https://github.com/storyblok/storyblok-js-client#class-storyblok):

app.use(StoryblokVue, {
  accessToken: "<your-token>",
  apiOptions: {
    //storyblok-js-client config object
    cache: { type: "memory" },
  },
  use: [apiPlugin],
});

If you prefer to use your own fetch method, just remove the apiPlugin and storyblok-js-client won't be added to your application.

app.use(StoryblokVue);
Storyblok Bridge

You can conditionally load it by using the bridge option. Very useful if you want to disable it in production:

app.use(StoryblokVue, {
  bridge: process.env.NODE_ENV !== "production",
});

Keep in mind you have still access to the raw window.StoryblokBridge:

const sbBridge = new window.StoryblokBridge(options);

sbBridge.on(["input", "published", "change"], (event) => {
  // ...
});

Compatibility

This plugin is for Vue 3. Thus, it supports the same browsers as Vue 3. In short: all modern browsers, dropping IE support.

ℹ️ More Resources

Support

Contributing

Please see our contributing guidelines and our code of conduct. This project use semantic-release for generate new versions by using commit messages and we use the Angular Convention to naming the commits. Check this question about it in semantic-release FAQ.

Keywords

FAQs

Package last updated on 25 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc