Development

Case Study: Building an E-commerce Store from Scratch

How we built a custom headless e-commerce store using Next.js App Router, Tailwind CSS v4, and the Shopify Storefront API.

Suraj Randhari·Full-Stack Developer & AI Systems Engineer·10th December 2024·12 Min
Case Study: Building an E-commerce Store from Scratch

The Challenge: Speed and Customization

Monolithic e-commerce platforms can sometimes limit customization and impact page speed due to render-blocking scripts and unoptimized asset loading. For this project, the client's store had a mobile page load time of 4.8 seconds, leading to drop-offs. The goal was to build a custom storefront that achieved sub-second load times while maintaining catalog flexibility.

The Solution: Headless Architecture

We implemented a headless architecture to separate the presentation layer from the back-office database. Next.js manages the frontend, while Shopify handles catalog data, inventory, and secure checkouts via the Storefront API. This approach allows us to optimize asset delivery, use modern CSS features, and pre-render pages statically.

  1. Frontend framework: Next.js App Router using React Server Components (RSC) to minimize client-side JavaScript.
  2. Styling: Tailwind CSS v4 for utility-first layouts and fast CSS compilation.
  3. Data Fetching: Shopify Storefront GraphQL API with tag-based revalidation.
  4. Hosting: Vercel for edge network caching and static page regeneration.

Integrating the Shopify Storefront GraphQL API

We set up a helper module to handle Shopify API requests. It supports Next.js fetch cache tag revalidation. When products are updated in Shopify, a webhook triggers a revalidation event to update the static pages without requiring a full build.

typescript
export async function shopifyFetch<T>({ 
  query, 
  variables 
}: { 
  query: string; 
  variables?: Record<string, any> 
}): Promise<T> {
  const endpoint = process.env.SHOPIFY_STOREFRONT_API_URL!;
  const key = process.env.SHOPIFY_STOREFRONT_ACCESS_TOKEN!;

  const res = await fetch(endpoint, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-Shopify-Storefront-Access-Token": key,
    },
    body: JSON.stringify({ query, variables }),
    next: { tags: ["products"] },
  });

  if (!res.ok) {
    throw new Error(`Failed to fetch from Shopify: ${res.statusText}`);
  }

  const { data, errors } = await res.json();
  if (errors) {
    throw new Error(`GraphQL Error: ${JSON.stringify(errors)}`);
  }
  
  return data;
}

Performance Optimization and Core Web Vitals

To achieve fast loading times, we used React Server Components to load product pages on the server and send minimal HTML to the browser. Images are optimized using Next.js Image component to serve modern formats (WebP/AVIF) and use lazy loading. As a result, the Largest Contentful Paint (LCP) decreased to 0.8 seconds, and conversion rates improved by 28% in the first month.

“Separating the frontend from the e-commerce database gives developers complete control over performance and design. Moving to Next.js improved the site performance score to 98 and significantly increased conversions.”

— Suraj Randhari

Conclusion

Headless commerce offers benefits for businesses aiming to optimize speed, design flexibility, and search engine visibility. Using Next.js alongside the Shopify Storefront API enables developers to build custom storefronts that combine performance with reliability.

Related Articles

Step-by-Step Guide to Launching Your First Shopify Store
E-commerce
23rd November 2024·8 Min

Step-by-Step Guide to Launching Your First Shopify Store

Learn how to establish a successful Shopify store from scratch. This comprehensive guide covers catalog setup, payment gateways, theme styling, and checkout optimization.

#Shopify#E-commerce#Guides#Business
Suraj Randhari
Suraj Randhari·Full-Stack Developer & AI Systems Engineer
Newsletter

Subscribe to the developer digest

Stay updated with technical articles, performance deep-dives, design patterns, and case studies. Sent once a month. No spam.