Build a robust e-commerce image gallery in NuxtJS

Divine Orji

One of the key factors to high conversion rates on an e-commerce platform is the presence of high-quality product images. When customers can see your product with more precision on its finer details, they are more likely to purchase it.

In this article, you will build a full-featured e-commerce product-image gallery with Nuxtjs and Cloudinary’s Product Gallery widget.

Nuxtjs is an open-source framework built on Vuejs that offers enhancements such as server-side rendering, zero configurations, and an ecosystem of modules that make for a better developer experience.

Sandbox & GitHub Repo

Click the link below to view a complete demo of this article on CodeSanbox

https://codesandbox.io/embed/nuxtjs-product-zoom-rkso64?fontsize=14&hidenavigation=1&theme=dark

To view the source code on GitHub, click here

Pre-requisites

This article requires the following:

  • Knowledge of JavaScript and Vuejs.
  • A Cloudinary account to manage your product images - create a free one here.
  • Yarn package manager to handle the node packages used in this project. You’ll need to install Nodejs first, which comes with npm.
  • Knowing Nuxtjs isn’t a strict requirement, but it’ll be nice to have.

Getting started with create-nuxt-app

In your terminal, navigate to the directory where you want to store your project and initialize it using the command create-nuxt-app:

1npx create-nuxt-app nuxtjs-product-zoom

Read and respond to the prompts with the recommended options below:

With the project’s successful setup, navigate into it and open it with your preferred code editor.

Setting up Cloudinary

On your Cloudinary dashboard, click on ‘Media Library’ and create a new folder named product-images:

Upload your images to the folder you’ve created. You can use the ones provided for this demo:

Working with Cloudinary’s Product Gallery

Product Gallery is an interactive widget that enables you to display images on your website or application. It does this using a galleryWidget() method that takes in an object with three key parameters:

  • Your Cloudinary account cloud name
  • The element that will contain the widget
  • The media assets (your product images) for the widget.

Set tags

To specify which media assets to get from Cloudinary, you need to add tags to your product images so you can reference them by their tags. Click a product image, and on the sidebar that appears to the right, click on ‘Metadata’ and add your tag. Click on ‘Save’ to store the tag. In the same way, you should add all the products you want to display, ensuring they have the same tag name. I recommend naming your tags in camelCase (the same way you name your variables).

Enable client-side asset lists

To get your media assets from Cloudinary, Product Gallery needs access to your client-side asset lists. By default, this delivery type is disabled. To enable it, go to ‘Settings’ and click on the ‘Security’ tab, then scroll down to ‘Restricted media types:’

Ensure that ‘Resource list’ is unchecked, then scroll to the bottom of the page and click ‘Save’.

Create a container for Product Gallery Widget

In your code editor, open pages/index.vue and update <template> with the code below:

1<template>
2 <div class="mx-auto w-full max-w-lg p-5">
3 <div class="my-5">
4 <h2 class="font-bold text-xl">Product Gallery Demo</h2>
5 <p>Click/Tap on each image to zoom</p>
6 </div>
7 <div id="product-gallery"></div>
8 </div>
9</template>

Here, you set up the demo’s UI layout. <div id="product-gallery"></div> is the element that will be referenced by JavaScript to contain our Product Gallery widget.

Initialize Product Gallery widget

In static/, create a js/ folder with a cloudinary.js file and add the following content to it:

1const cloudName = 'dpkreativ'
2const productGallery = cloudinary.galleryWidget({
3 container: '#product-gallery',
4 cloudName: cloudName,
5 mediaAssets: [{ tag: 'handbag' }],
6})
7productGallery.render()

In this code:

  • cloudName takes the cloud name on your Cloudinary account as a variable
  • You created a productGallery variable that instantiates a cloudinary class with a galleryWidget method.
  • cloudinary.galleryWidget takes in at least three required parameters - container, cloudName and mediaAssets.
  • container references your id=``"``product-gallery``" in pages/index.vue
  • cloudName takes in the value of the cloudName variable you created earlier
  • mediaAssets contains an array of assets. { tag: '``handbag``' } creates an object that contains all media with “handbag” as its tag.
  • `productGallery.render()`` renders the product gallery widget you’ve just created

Include JavaScript files in **nuxt.config.js**

Cloudinary provides a JavaScript file that contains all the functionality required for the Product Gallery widget to work; its URL is https://product-gallery.cloudinary.com/all.js. Navigate to the root folder of your project, open nuxt.config.js, and update head with the code below:

1head: {
2 title: 'nuxtjs-product-zoom',
3 htmlAttrs: {
4 lang: 'en',
5 },
6 meta: [
7 { charset: 'utf-8' },
8 { name: 'viewport', content: 'width=device-width, initial-scale=1' },
9 { hid: 'description', name: 'description', content: '' },
10 { name: 'format-detection', content: 'telephone=no' },
11 ],
12 link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
13 script: [
14 {
15 src: 'https://product-gallery.cloudinary.com/all.js',
16 },
17 {
18 body: true,
19 src: '/js/cloudinary.js',
20 },
21 ],
22 },

In this code:

  • You added the script property with src pointing to Cloudinary’s Product Gallery JavaScript file.
  • You added another src pointing to the /js/cloudinary.js file you created in your static/ folder. The object also contains body: true, which tells Nuxt to put the script tag below the body tag in your HTML file.

Run your code

In your terminal, run yarn dev to compile the code and view it on your browser at localhost:3000

Conclusion

In this article, you learned how to build a product image gallery in Nuxtjs using Cloudinary’s Product Gallery widget. For easier customizations on the widget, check out this Product Gallery Demo offered by Cloudinary. You can make customizations using its GUI and copy the code for your needs.

Resources

Divine Orji

Software Engineer and Technical Writer

I am a software engineer passionate about building fast, scalable apps with beautiful user interfaces.