swellstores / horizon

Headless NextJS storefront starter powered by Swell
https://swell-horizon-demo.vercel.app/
MIT License
58 stars 52 forks source link
ecommerce headless jamstack

Description

Horizon is a reference example and starter project for building custom Next v12 storefronts powered by Swell.js.

[!IMPORTANT] The app is provided in an 'as-is' state and will not be receiving updates or bug fixes from March 1, 2024. Swell.js itself is in active development and is recommended SDK for building full-stack JS storefronts on Swell.

Deploy on Vercel

Since Horizon is a standard Next.js app, deploying it on Vercel only takes a few steps. Follow this guide for a walk-through on how to do so.

Features

Technology stack

Getting started

Setting up your environment

The first step will be to set up the environment variables. Create an .env file at the root of the project's directory and add the following entries:

The next step will be setting up the development environment. We recommend installing Node.js through nvm. Afterwards, run the following commands to match the project's Node and NPM versions and install the project's dependencies:

nvm install
yarn install

This will also automatically setup the GraphQL client with end-to-end typings and download the content and settings from your store's editor. Check out the graphql:generate and theme:generate commands on the package.json file for more information.

Lastly, you can start the development server with:

yarn run dev

Horizon should now be running on http://localhost:3000 by default.

Running Storybook

You can take a look at the available components and the props they accept by running the storybook npm script:

yarn run storybook

Storybook should now be running on http://localhost:6006, unless the port is already taken.

Directory structure

Customizing the theme

The config/ folder contains JSON files that dictate the editor's schema and default values. The top-level structure is defined inside of the editor.json file, while the content blocks that make up the dynamic pages are located inside of the content/ folder.

In the prepare step these files are pushed upstream to update the store's editor and afterwards the node script builds-utils/generate-theme.mjs is executed. This script will attempt to fetch the theme data that has been set through the editor, and that can be accessed through the endpoint https://{{your store id}}.swell.store/api/settings. This script can also be ran on its own by using the command npm run theme:generate.

Using this theme data, the script will create a theme.css file that exposes these settings as css variables, so that they can then be used by the Tailwind config and throughout the application.

To modify any of the theme settings, first make the change in the Editor and then re-trigger the script to pull them into the store. Alternatively, you can also modify the theme.css file directly to preview how the changes would look before committing to them in the Editor.

In regards to customizing the editor's schema itself, please see here for more information.

Customize language settings

The language settings represent a set of variables that can be translated into multiple languages in the editor and used throughout Horizon.

The config/editor.json file has a lang key which holds all the text variables used throughout the app. If you want to add a new setting follow these steps:

Add the object containing the configuration of the language setting inside lang under the namespace you want (ex: products)

{
  "lang": {
    //...
    {
      "id": "lang.products.premium_member_label",
      "label": "Premium member label",
      "default": "Premium",
      "type": "short_text",
      "localized": true
    }
  }
}

Add a default value in the config/defaults.json for the new setting (under the same path as the id)

{
  "lang": {
    //...
    "products": {
      "premium_member_label": "Premium"
    }
  }
}

Push the new settings to the store by running the sync npm script. This will add the new language setting to the editor's schema.

yarn run sync

Use the new language setting in the code with the i18n hook

const i18n = useI18n();

const premiumLabel = i18n('products.premium_member_label');

Preview mode

Horizon automatically enters Next.js' preview mode when the NEXT_PUBLIC_SWELL_EDITOR environment variable is set to true. This is done through the preview.ts API route, which sets the cookies necessary to alert Next.js that pages should be rendered on-demand instead of statically. This is important when running alongside the editor so that the changes made within it are immediately reflected in the storefront without requiring a new build.

The calls to the preview.ts API route are done through a useEffect in app.tsx.

Pre-commit hooks

Before every commit, the following checks are made:

This process will automatically attempt to fix the formatting and some of the eslint errors and warnings.

ESLint and TypeScript errors will prevent commits from being completed. See lint-staged.config.js and eslintrc.json for more information on the checks that are made.