DEV Community

Roberto Dote
Roberto Dote

Posted on

Simple svelte demo deployed in Netlify

I was learning Svelte, and i created a simple project to explain some functionalities.

Before to begin, always you can found the source code in gitlab and the deployed project in Netlify

Create a Svelte project.

You have two options:
1- Build you own environment with rollup and create your configuration files and scrips.

2- Get a clean/simple project from svelte repo.

npx degit sveltejs/template my-svelte-project
cd my-svelte-project
npm install
npm run dev

Folder Structure

For this example i created the follow structure:


i came from react world and, i tried to maintain a similar folder structure with containers(views) and components.
Also i created a products.js for get static products data.

the main file put the application into document.body

import App from './App.svelte';

const app = new App({
    target: document.body

export default app;

And in our App.svelte we can import our (in this case) containers components (views).

    import HomeLayout from './Views/Home.svelte';

<HomeLayout />

First, we've the javascript code and below the component.

Now, let's to see the Views/Home.svelte component.

  import Header from '../Components/Header.svelte';
  import ContentBox from '../Components/ContentBox.svelte';
  import productsJson from '../products.js'
  import { onMount } from 'svelte';

  let products

  onMount(() => {
    products = productsJson

<div class="container-home">
  <Header />
  <ContentBox products={products} />

First, we can import the components that we going to use, also we import the productsJson. and also we going to use a function from svelte life cycle onMount

let products is our state for store the data from productsJson (or returned data from any service).
so, in onMount function we can to do this assignament.

Note that in <ContentBox products={products} /> we are passing the product list to the component. we going to see this later.

now, let's see the Header component.
here we can found some more fun.

This component is more large, so if you want to see the full source code, please go to

In top on the code, we have the styles rules.

  .header {
    height: auto;
    width: 100%;
    background: skyblue;
    box-sizing: border-box;
    padding: 7px;
  .header .menu {
    display: flex;
    align-items: center;
    list-style-type: none;
    justify-content: flex-end;

in the middle we have the javscript content.

  const logoUrl = '';
  let menuOptions = [
      title: 'Option 1',
      url: '#',
      active: false
      title: 'Option 2',
      url: '#',
      active: false
      title: 'Option 3',
      url: '#',
      active: false

  const setActivate = ({ title }) => {
    menuOptions = => ({
      active: item.title === title

const logoUrl is a simple constant for store the logo url, we can use this directly in the code.

let menuOptions is a state (in vue or react), and here we manage the menu options.

const setActivate is a function that modify the state 'menuOption'. we pass a title, and set the item from array when the title be equal to select item in the menu.

after this, we have the html code.

<li class="logo"><img src={logoUrl} alt="logo" /></li>
{#each menuOptions as option}
    class="menuItem{ ? ' active' : ''}"
    on:click={() => setActivate(option)}>

1- we user the logoUrl const for get the logo.
2- we use #each for iterate the menuOptions array. this is a function from svelte.
3- we use a condition class for active items in the menu using ternary operator.
4- we use on:click for override event click and call to the function setActivate for pass the current option

ContentBox component

So, in this component we've some styles in the begin.

after the styles, we've the script content. let's see this:

  import Product from './Product.svelte'
  import { cart } from '../Store/store.js'

  export let products = []

  let total = 0

  cart.subscribe(c => {
    total = c

  const removeItem = () =>
    cart.update(c => c - 1)

1- Import the component Product
2- Import the cart store.

---- Let's see how the store works ----

  • In the folders of our project, we have Store/store.js.
import { writable } from 'svelte/store'

export const cart = writable(0)
  • Here we are importing the writable function from svelte/store, and we create our 'global' state calling cart. in this case we only have a writable with a int value. you could have and object, array, string, etc.

--- back to ContenBox component ---

3- this export let products = [] is the data received by props. and also we are assign it an empty array as default value.

4- let total = 0 is our local state for store the data from the Store (cart state)

5- cart.subscribe is a native function from cart object (store) and it allow to cart state listen the changes and use this new data.
in this case, when the cart state is changed we set out local variable total with the new cart value.

6- removeItem is a function that is called when the user press the link for remove items from the cart. and this call a cart.update for update the state value with a new value. in this case we are taking the current value and decrease it in 1

Now, from the html side we've the follow code:

<div class="content-box">
  <h1>Product list</h1>
  <div class="cart">
    <h3>You have {total} products in the cart</h3>
    {#if total > 0}
      <a href="javascript:void(0)" on:click={removeItem}>Click to remove an item from cart</a>
  <div class="product-cotainer">
    {#each products as product}
      <Product product={product} />

1- <h3>..{total}..</h3> we are showing our local state total, this value changes when the 'global' state cart was changing.

2- We've an if condition for show/hide a link for remove items from the cart.

3- We've an each for create a Product component dynamically and pass to it a product object as a prop.

Product Component

Here we have no new functionality.
So, let's to review the code quickly.

  import { cart } from '../Store/store.js'
  export let product = {}

  const setCart = () => cart.update(total => total + 1)

1- Import the cart store object
2- Receive the product object from the parent (as a prop)
3- setCart update the 'global' state cart increase it in 1.

the html side:

<div class="product">
    <img src={product.image} alt="" />
  <div class="product-info">
  <div class="product-price">
    {#if product.stock < 5}
      <strong>¡Hurry up!</strong>
  <div class="addtocart">
    <button on:click={setCart}>Add to cart</button>

Here we can use the product object for get the product data.
We have an if conditional for show/hide a message for the product stock quantity.
And finally we've a button with the click event for update the cart state.

In general this are all the files.
Remember that you can check here the live website:

____ Deploying on netlify ____

!! First upload you code to Gitlab (in this case, but can use bitbucket or github).

git clone
cd my-fancy-repo
git add
git commit -m "add README"
git push -u origin master

Ok, now go to
and create a free account.

After, go to
And select the source of you code (gitlab, github, bitbucket)
And finally Select you repo (project)

Now, we need to set our deploy settings.

You need to set the branch (in this case is master)
the build command in this case is: npm run build (this command is in the package.json file)
and the publish directory is: public (this is the default folder used by svelte for store the build files).

Finally click on 'Deploy Site' :D

Now that you have the site with gitlab and netlify, when you merge code to master branch (in this case), netlify going to re deploy your app with you new changes, fantastic!!

For more svelte documentation please visit and check the tutorial and the examples sections.

And for routes and server-side features, svelte use Sapper (

This is all! See u ;)

Top comments (0)