Giter VIP home page Giter VIP logo

gridsome-starter-strapi's Introduction

gridsome-starter-strapi

This is a Gridsome Starter using the headless CMS Strapi. It is suppose to kick-start development with Strapi.

A Demo is hosted on Netlify.

Under the hood, the starter is using the source plugin @gridsome/source-strapi to pull data from Strapi into Gridsome's data store.

Images provided by Strapi are downloaded and saved locally via onCreateNode. Thus, the build version includes all images in /static.

Rich text which is provided as markdown from Strapi is prepared via onCreateNode and transformed to html using @gridsome/transformer-remark.

Gridsome-Starter-Strapi Preview

Strapi Setup

Note: Steps that are marked with a check are already done and are just necessary if you set up a completely new project.

  1. ✅ Install the @gridsome/source-strapi Plugin: npm install @gridsome/source-strapi --save
  2. ✅ Add it to the config file:
export default {
  plugins: [
    {
      use: '@gridsome/source-strapi',
      options: {
        apiURL: 'http://localhost:1337',
        queryLimit: 1000, // Defaults to 100
        contentTypes: ['post'],
        // Possibility to login with a Strapi user,
        // when content types are not publicly available (optional).
        loginData: {
          identifier: '',
          password: ''
        }
      }
    }
  ]
}
  1. Install Strapi by following the Get Started Guide.
  2. Install Strapi and create a new project: npx create-strapi-app gridsome-starter-strapi-cms --quickstart.
  3. Complete the form to create the first Administrator user.
  4. Since we are using GraphQL, install the GraphQL plugin: npm run strapi install graphql. Open the GraphQL Playground (http://localhost:1337/graphql) to test your queries.
  5. Restart your server: npm run develop.

Create Posts

  1. In the Strapi Admin Interface you can now add Content Types (Plugins > Content-Types Builder > Create new collection type).
  2. Name you new collection type Post.
  3. Now, add 4 fields, namely title (text), subtitle (text), article (rich text) and image (single media).
  4. Click on Save & Publish.
  5. Add blog entries by clicking on Collection Types > Posts > Add New Post.
  6. Go to Settings > Roles under USERS & PERMISSIONS PLUGIN. Click on the column saying Public. Put a check on find and findOne under Post. Click on Save.
  7. ✅ In gridsome.config.js add post (API ID) under contentTypes.

Create About

  1. In the Strapi Admin Interface you can now add Content Types (Plugins > Content-Types Builder > Create new single type).
  2. Name you new collection type About.
  3. Now, add 2 fields, namely title (text) and article (rich text).
  4. Click on Save & Publish.
  5. Add content to the about page by clicking on Single Types > About.
  6. Go to Settings > Roles under USERS & PERMISSIONS PLUGIN. Click on the column saying Public. Put a check on find under About. Click on Save.
  7. ✅ In gridsome.config.js add about (API ID) under singleTypes.

Gridsome Setup

1. Install Gridsome CLI tool if you don't have

npm install --global @gridsome/cli

2. Create a Gridsome project

  1. gridsome create gridsome-starter-strapi to install default starter
  2. cd gridsome-starter-strapi to open the folder
  3. gridsome develop to start a local dev server at http://localhost:8080
  4. Happy coding 🎉🙌

🐛 Debugging

  • Since we are working with GraphQL, the GraphQL explorer can be very helpful. You can open it through http://localhost:8080/___explore.

Troubleshooting

  • If you get TypeError: gridsome.server.js: Cannot read property 'allStrapiPost' of undefined (31:10) as an error after starting the gridsome development server, you likely forgot to create and/or publish your post in Strapi under Collection Types > Posts.

💫 Deploy

Deploy to Netlify

gridsome-starter-strapi's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

doc22940

gridsome-starter-strapi's Issues

Q: Save new images to Gridsome when server is running

Hi,

I have implemented your function to move all medias from Strapi into Gridsome's asset directory, so that all feature of the g-image can be used. And that works very well. I have encountered one problem though. If my server is running and I upload a new image in Strapi, then there is no chance that this image will be moved to assets directory. The server must be restarted.

Is there any way to do that while the server is running? I'm looking for some solution, which would work on production as well. Right know it seems that to make it working, the whole g-image functionality needs to be implemented in some custom component, which would then use responsive image variants that Strapi creates for every image.

Converting rich text to html

Could the rich text section be converted to HTML instead of with white-space: pre-line to allow for more customisation around paragraphs?

Missing Information

This is just to document what issues I ran into and how I resolved them.

They may not affect everyone who uses this repo, but it may be something that could be quickly fixed to avoid anyone else having the same problems.

First Issue

I was unable to build a GraphQL query using the provided statement in the gridsome.server.js file. I looked at the troubleshooting portion in the readme but that didn't solve the problem.

Solution

I found the problem was in the options supplied to the source-strapi plugin. I added fieldName: "strapi" and typeName: "strapi" and the query started to work.

  siteName: "Gridsome-Starter-Strapi",
  plugins: [
    {
      use: "@gridsome/source-strapi",
      options: {
        apiURL: "http://localhost:1337",
        fieldName: "strapi",
        typeName: "strapi",
        queryLimit: 1000, // Defaults to 100
        contentTypes: ["post"],
        singleTypes: ["about"],
        // Possibility to login with a Strapi user,
        // when content types are not publicly available (optional).
        loginData: {
          identifier: "",
          password: "",
        },
      },
    },
    {
      use: "@gridsome/transformer-remark",
    },
  ],
};

Second Issue

During the build step I kept getting hung up in gridsome.server.js again because of the process of saving the images from Strapi in the src/assets/images folder.

Solution

When I cloned the repo there was no assets/images folder in the project and the function was not creating the necessary directories to save the image file in. I manually created the directory structure and started the process again with no issue.

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.