Shopify section is working fine in local but not working in production| Sabbirz | Blog

Shopify section is working fine in local but not working in production

shopify-section-not-working-on-production

Shopify Sections Missing in Production (Quick Fix Guide)

Ever encountered that head-scratching moment when your code runs flawlessly in your local development environment but acts up in production? 🀯

It's a common developer dilemma, and I recently faced it with a Shopify theme section. The good news? The solution was surprisingly simple, and I'm here to share my experience so you don't have to spend hours debugging! Let's dive in. πŸ‘‡

Understanding the Codebase πŸ’‘

For context, here's a quick rundown of the development setup:

  1. Theme Development: I started developing the theme from scratch.
  2. Styling with Tailwind CSS: I am using TailwindCSS to style the entire website.

πŸš€ Learn How to Integrate TailwindCSS with daisyui in Shopify: Step-by-Step Guide

The Product single Page That Was Working Locally πŸŽ‰

In the local environment, I had a product.json file inside the templates directory:

{
  "sections": {
    "main-product": {
      "type": "main-product",
      "settings": {}
    }
  },
  "order": ["main-product"]
}

As the product.json snippet suggests, the theme was designed to include a section named main-product.liquid file in the sections directory:

<section class="p-8" data-section-id="{{ section.id }}">
  <div class="max-w-4xl mx-auto grid grid-cols-1 md:grid-cols-2 gap-8">
    <div>
      {% if product.featured_image %}
        <img
          width="auto"
          height="auto"
          src="{{ product.featured_image | image_url: width: 800 }}"
          alt="{{ product.featured_image.alt | escape }}"
          loading="lazy"
          class="w-full rounded-lg shadow-lg"
        >
      {% else %}
        {{ 'product-1' | placeholder_svg_tag: 'placeholder-svg w-full h-auto' }}
      {% endif %}
    </div>

    <div>
      <h1 class="text-3xl font-bold mb-2">{{ product.title }}</h1>
      <p class="text-lg text-gray-600 mb-4">{{ product.vendor }}</p>

      <div class="prose mb-4">
        {{ product.description }}
      </div>

      <p class="text-2xl font-semibold text-neutral">
        {{ product.price | money }}
      </p>
    </div>
  </div>
</section>

{% schema %}
{
  "name": "Main product",
  "settings": []
}
{% endschema %}

Key considerations for Shopify sections:

  1. CSS: The HTML above includes Tailwind CSS classes. Feel free to replace these with your own preferred CSS framework or custom styles.
  2. Schema Tag: Every Shopify section file must include a {% schema %} tag, even if it's empty. This is crucial for Shopify to recognize and render your section.

When I ran the code, the product page was working perfectly in the local environment:

shopify section working on local dev mode

πŸš€ Shopify Theme Development Part 1 – Setup, Code & Version Control

Issue in Production

However, after pushing these changes to GitHub and deploying to the Shopify production environment, Shopify couldn't find the section! 😱

shopify section not working in shopify production


The "Silly" Solution πŸ€¦β€β™€οΈ

After debugging for a while, I discovered the issue was quite simple and easy to fix.

Shopify has a specific convention for the main content section of template files. While a custom name like main-product works locally for development purposes, for a template's primary section in a production theme, Shopify often expects the section ID in the order array to simply be main.

Here's the corrected product.json:

{
  "sections": {
    "main": {
      "type": "main-product",
      "settings": {}
    }
  },
  "order": ["main"]
}

The only change was renaming the section key from "main-product" to "main" within the sections object and updating the order array accordingly.


Problem Solved! βœ…

Upon deploying this small but significant change to Shopify, the product section finally rendered correctly! πŸŽ‰

Shopify section is now working after correcting json section name to main

Related posts