DEV Community

Cover image for 3. Create homepage of categories and products - Create a Commerce.js store with Svelte
Jamie Barton for Commerce.js

Posted on

1 2

3. Create homepage of categories and products - Create a Commerce.js store with Svelte

Let's first create an index page of all the data we need to build our store with Svelte.



Inside /src/routes/index.svelte we'll doing the following:

  • Fetch all merchant information using commerce.merchants.about()
  • Fetch all categories using commerce.categories.list()
  • Fetch all products using commerce.products.list()
  • List our merchant business name
  • List all categories, and products, with dynamic links to both
<script context="module">
import commerce from "../lib/commerce.js";

export async function preload() {
  const merchant = await commerce.merchants.about();
  const { data: categories } = await commerce.categories.list();
  const { data: products } = await commerce.products.list();

  return {
    merchant,
    categories,
    products,
  };
}
</script>

<script>
export let merchant;
export let categories;
export let products;
</script>

<h1>{merchant.business_name}</h1>

<ul>
  {#each categories as category}
    <li>
      <a rel="prefetch" href="categories/{category.slug}">{category.name}</a>
    </li>
  {/each}
</ul>

<ul>
  {#each products as product}
    <li>
      <a rel="prefetch" href="products/{product.permalink}">{product.name}</a>
    </li>
  {/each}
</ul>
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay