Usage
This transformer is automatically enabled and used once it is installed in your project. Extra configuration options can be set globally under transformers field of gridsome.config.js.
/* gridsome.config.js */ export default { plugins: [ { use: '@gridsome/source-filesystem', options: { path: 'path-to-local-images', } }, ] }
Query for local images
-
Config
@gridsome/source-filesystemplugin to fetch local images and map them in GraphQL Data layer undertypeName: ImageNode./* gridsome.config.js */ export default { plugins: [ { use: '@gridsome/source-filesystem', options: { path: 'content/images', typeName: 'ImageNode' } }, ] }
Warning Don't use
ImagefotypeNamebecause it is reserved by Gridsome. -
Then you can query the image details on page component:
<page-query> query { images: allImageNode { edges { node { path name extension } } } } </page-query>
Manage images with Cloudinary
-
Add
.envfile contains the environment variables defined aboveCLOUDNAME,API_KEY,API_SECRET. Make sure you have this file added to.gitignorefile to avoid committing and push it to the remote repo by accident.CLOUDNAME = your_cloud_name API_KEY = your_api_key API_SECRET = your_api_secret_key
Learn how to get these keys here
-
Config
@gridsome/source-filesystemplugin to fetch local images and map them in GraphQL Data layer undertypeName: ImageNode./* gridsome.config.js */ export default { plugins: [ { use: '@gridsome/source-filesystem', options: { path: 'content/images', typeName: 'ImageNode' } }, ] }
Warning Don't use
ImagefotypeNamebecause it is reserved by Gridsome. -
Add Cloudinary as the loader using
transformers.img.loaderingridsome.config.jswith required configurations:transformers: { img: { uploadOptions: { folder: 'examples', }, loader: { type: 'cloudinary', cloudName: process.env.CLOUDNAME, apiKey: process.env.API_KEY, apiSecret: process.env.API_SECRET, } } }
- Each of the
ImageNodenode will now have additional fieldimage. The queried image's info will be fetched directly from Cloudinary if it was uploaded previously. Otherwise it will be uploaded to Cloudinary and returns the info accordingly.
<page-query> query { images: allImageNode { edges { node { image { secureUrl publicId } } } } } </page-query>
- Then you can use
g-imageto display the queried images according to theirsecuredUrl.
<template> <Layout> <div> <ul> <li v-for="image in images" :key="image.publicId"> <g-image :src="image.secureUrl" loading="lazy"/> </li> </ul> </div> </Layout> </template> <script> export default { computed: { images() { return this.$page.images.edges.map(({ node }) => node) } } } </script>