Git Product home page Git Product logo

next-snipcart-store's Introduction

React ecommerce tutorial with Snipcart

How to Build a React E-Commerce Web App

This is a demo project using Snipcart to create an e-commerce store with Next.js.

๐Ÿ‘€ Read the step-by-step tutorial here

Getting Started

Environment File

Create a file called .env.local at the root of the project with:

NEXT_PUBLIC_SNIPCART_API_KEY="[Your Snipcart API Key]"

Running the Project

Start your development server:

npm run dev
# or
yarn dev

Open http://localhost:3000 with your browser to see the result.

Learn More

To learn more about Next.js, take a look at the following resources:

You can check out the Next.js GitHub repository - your feedback and contributions are welcome!

Deploy on Vercel

The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.

Check out our Next.js deployment documentation for more details.

next-snipcart-store's People

Contributors

colbyfayock avatar ludovic-armand avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

next-snipcart-store's Issues

Item-count doesn't display after routing with next/link

Im facing an issue where the element with class snipcart-items-count isn't updated after we route from 1 page to another with the Link component from nextjs.

In this video you can see how I route from the homepage to a dynamic product page. During this route the count isn't set again:

Screen.Recording.2021-02-24.at.15.28.14.mov

It tried to see if the snipcart.initialized event was triggered after routing, however this was not the case. Only after a reload this would trigger again and display the correct value.

Most of the other functionality seem to work just fine. If I add a new product to the cart the value actually will update and display correctly:

Screen.Recording.2021-02-24.at.17.09.02.mov

I created a Github repo for this issue which you can find here:

Really curious what is wrong here and if i made a mistake or this is a bug from Snipcart.

Cannot go back to initial landing page using browser back button

Hello!

I've been facing a browser navigation issue with the Snipcart script in a NextJS project. I created a minimal reproduction fork here: https://github.com/talhaguy/next-snipcart-store. The issue is as follows:

Replication Steps

  1. Go to home page
  2. Click on FAQ page link in nav
  3. Once FAQ page is loaded, press browser back button

Expected
Page should go back again.

Actual
Page visually remains on the same one. The URL does change though.

After some debugging, I noticed the history state object is a little different on the landing page with and without Snipcart.

history.state with Snipcart:
with-snipcart

history.state without Snipcart:
wo-snipcart

I created a branch in the reproduction repo with a fix by replacing the landing page history state: https://github.com/talhaguy/next-snipcart-store/tree/fix. I am using the NextJS router to accomplish this:

// _app.js

import { useEffect } from 'react';
import { useRouter } from 'next/router'

function useResetHistory() {
  const router = useRouter()

  useEffect(() => {
    document.addEventListener("snipcart.ready", () => {
      Snipcart.events.on('snipcart.initialized', (snipcartState) => {
        router.replace(router.pathname)
      });
    });
  }, [])
}

function MyApp({ Component, pageProps }) {
  useResetHistory()

  return <Component {...pageProps} />
}

export default MyApp

Although it does seem to be working fine now, it feels hacky. Is there a better way to handle this? Is this truly a conflict between NextJS routing and Snipcart routing?

Thank you,
Muhammad Talha

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.