From 8b3725471270ff22c5f751dd49206a398aa3a7c4 Mon Sep 17 00:00:00 2001 From: venkat Date: Sun, 2 Aug 2020 19:26:09 +0530 Subject: [PATCH] docs: update nuxt guide (#4095) --- website/content/docs/nuxt.md | 195 ++++++++++++++++------------------- 1 file changed, 89 insertions(+), 106 deletions(-) diff --git a/website/content/docs/nuxt.md b/website/content/docs/nuxt.md index 1df1f62c..b48230a4 100644 --- a/website/content/docs/nuxt.md +++ b/website/content/docs/nuxt.md @@ -52,7 +52,7 @@ collections: - name: 'blog' label: 'Blog' format: 'json' - folder: 'assets/content/blog' + folder: 'content/blog' create: true slug: '{{year}}-{{month}}-{{day}}-{{slug}}' editor: @@ -68,12 +68,11 @@ You can build whatever collections and content modeling you want. The important ### Add the `content/` directory to Nuxt -In your `assets/` directory, you can create a new directory `content/`. As you might guess, this is where our content will live. Your filesystem should look about like this, so far: +In your root directory, you can create a new directory `content/`. As you might guess, this is where our content will live. Your filesystem should look about like this, so far: ```sh root/ -├ assets/ -│ └ content/ +├ content/ ├ components/ ├ layouts/ ├ middleware/ @@ -134,128 +133,112 @@ Back in your [Netlify dashboard](https://app.netlify.com/): **Accessing the CMS** Once you've reached this point, you should be able to access the CMS in your browser at `http://localhost:3000/admin`. You'll be prompted to add the URL of your Netlify site. Once you've added that URL, you can log in with an Identity account or with one of the External Providers you enabled in step 3 above. For the sake of this tutorial, you can create a blog post in the CMS, and publish it! Once you `git pull` in your project, the blog post will show up in the project at `assets/content/blog/.json`. -## Integrating content in Nuxt with Vuex - -**Note:** In order to use `nuxtServerInit` your mode must be `universal` in your `nuxt.config.js`. +## Using nuxt/content +Netlify CMS and [nuxt/content](https://content.nuxtjs.org) module just click together and complement each other to give you best authoring experience and developer experience respectively. +Adding nuxt/content dependency ```javascript -mode: 'universal' -``` +yarn add @nuxt/content +or +npm i @nuxt/content -Next, you'll set up the integrated Vuex store to collect blog posts. Create a file `index.js` in the `store/` directory, and add **state**, **mutations**, and **actions** for your blog posts: +```` +Then, add @nuxt/content to the modules section of nuxt.config.js: -```js -export const state = () => ({ - blogPosts: [], -}); +````javascript +{ + modules: [ + '@nuxt/content' + ], + content: { + // Options + } +} +```` +By adding nuxt content module you get `$content` injected into your whole app which you can use to fetch content from your content folder using `simple fetch api` or `nuxt asyncData` option. +
+This also gives a `` component which helps you display markdowm content with ease and also gives option of live editing in dev mode. +### Example Blog Post List +`nuxt/content` module gives us `$content` which we can use to fetch the list of blog posts in `content/blog` directory. +````javascript + -export const mutations = { - setBlogPosts(state, list) { - state.blogPosts = list; + +```` +### Example Blog Post +To generate blog posts create _slug.vue file in the pages folder. by using `$content` you would get a json which you can use to display. But if you are using `markdown` to write your posts you can use `` module which gives you option to edit content on page in dev mode and many more [features](). -export const actions = { - async nuxtServerInit({ commit }) { - let files = await require.context('~/assets/content/blog/', false, /\.json$/); - let blogPosts = files.keys().map(key => { - let res = files(key); - res.slug = key.slice(2, -5); - return res; - }); - await commit('setBlogPosts', blogPosts); - }, -}; -``` - -Now you can use that content in your templates. In your `pages/` directory, create a `blog/` directory. Within the `blog/` directory, create two files `index.vue` and `_blog.vue`. These will respectively be the blog list page and the blog post page. - -**Blog List Page** - -In `pages/blog/index.vue`, you'll add your template and a method to the `computed` property of the Vue instance to return blog posts from the Vuex store. This will make `blogPosts` available in the Vue template for you to iterate over, etc. Feel free to add styling as well. - -```js +````javascript -``` - -**Blog Post Page** -Now open your `pages/blog/_blog.vue` file. Add an `asyncData()` method to the Vue instance that imports the corresponding JSON file. You can add a `payload` as well — this will come in handy during the process of creating a static site. - -```js -export default { - async asyncData({ params, payload }) { - if (payload) return { blogPost: payload }; - else - return { - blogPost: await require(`~/assets/content/blog/${params.blog}.json`), - }; - }, -}; -``` - -Now in your template, you can access whatever properties you need. - -```html - -