Giter Site home page Giter Site logo

cmorrow-rv / storyblok-nuxt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from storyblok/storyblok-nuxt

0.0 0.0 0.0 512 KB

Storyblok Nuxt.js module

Home Page: https://www.storyblok.com/tp/nuxt-js-multilanguage-website-tutorial

License: MIT License

JavaScript 97.93% Vue 2.07%

storyblok-nuxt's Introduction

storyblok-nuxt

npm (scoped with tag) npm Codecov Dependencies js-standard-style

Nuxt module for the Storyblok, Headless CMS.

Getting Started

If you are first-time user of the Storyblok, read Nuxt Getting Started guide at Storyblok website.

Setup

  • Add axios dependency as it's a peer dependecy of the storyblok-js-client used by storyblok-nuxt
  • Add storyblok-nuxt dependency using yarn or npm to your project
  • Add storyblok-nuxt to modules section of nuxt.config.js

Installation

npm install --save-dev nuxt-storyblok axios
// yarn add nuxt-storyblok axios

Hint: You don't have to install Axios if you already installed Axios module of Nuxt.

Add following code to modules section of config.nuxt.js and replace the accessToken with API token from Storyblok space.

{
  modules: [
    ['storyblok-nuxt', {
      accessToken: 'YOUR_PREVIEW_TOKEN',
      cacheProvider: 'memory'
    }],
 ]
}

Usage

This module adds two objects to the the Nuxt.js context.

  1. $storyapi: The Storyblok API client.
  2. $storybridge: A loader for the Storyblok JS bridge that is responsible for adding the editing interface to your website.

Example of fetching data of the homepage and listening to the change events of the JS bridge:

export default {
  data () {
    return {
      story: { content: {} }
    }
  },
  mounted () {
    this.$storybridge(() => {
      const storyblokInstance = new StoryblokBridge()

      storyblokInstance.on(['input', 'published', 'change'], (event) => {
        if (event.action == 'input') {
          if (event.story.id === this.story.id) {
            this.story.content = event.story.content
          }
        } else {
          window.location.reload()
        }
      })
    }, (error) => {
      console.error(error)
    })
  },
  asyncData (context) {
    return context.app.$storyapi.get('cdn/stories/home', {
      version: 'draft'
    }).then((res) => {
      return res.data
    }).catch((res) => {
      if (!res.response) {
        console.error(res)
        context.error({ statusCode: 404, message: 'Failed to receive content form api' })
      } else {
        console.error(res.response.data)
        context.error({ statusCode: res.response.status, message: res.response.data })
      }
    })
  }
}

Hint: Find out more how to use Nuxt together with Storyblok in Nuxt Technology Hub

API

Like described above, this package includes two objects into Nuxt.js context:

$storyapi

This object is a instance of StoryblokClient. You can check the documentation about StoryblokClient in the repository: https://github.com/storyblok/storyblok-js-client

$storybridge(successCallback, errorCallback)

You can use this object to load the Storyblok JS Bridge. In the success callback you will it have available in the window variable StoryblokBridge.

Migrate from 1.x to 2.x

Listening to Visual Editor events in 1.x

Most of our tutorials and recordings still using following deprecated approach for real-time editing and listening to Storyblok's Visual Editor events. This approach can be used only with 1.x version of the storyblok-nuxt.

export default {
  mounted () {
    // Use the input event for instant update of content
    this.$storybridge.on('input', (event) => {
      if (event.story.id === this.story.id) {
        this.story.content = event.story.content
      }
    })
    // Use the bridge to listen the events
    this.$storybridge.on(['published', 'change'], (event) => {
      this.$nuxt.$router.go({
        path: this.$nuxt.$router.currentRoute,
        force: true,
      })
    })
  }
}

Listening to Visual Editor events in 2.x

The recommended approach for 2.x storyblok-nuxt plugin.

export default {
  mounted () {
    this.$storybridge(() => {
      const storyblokInstance = new StoryblokBridge()

      storyblokInstance.on(['input', 'published', 'change'], (event) => {
        if (event.action == 'input') {
          if (event.story.id === this.story.id) {
            this.story.content = event.story.content
          }
        } else {
          this.$nuxt.$router.go({
            path: this.$nuxt.$router.currentRoute,
            force: true,
          })
        }
      })
    }, (error) => {
      console.error(error)
    })
  }
}

Contribution

Fork me on Github.

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.

License

MIT License

Copyright (c) Storyblok [email protected]

storyblok-nuxt's People

Contributors

onefriendaday avatar emanuelgsouza avatar samuells avatar christianzoppi avatar

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.