Vue Storefront is now Alokai! Learn More
Getting Started

Getting Started

If you're setting your Alokai application from scratch, you'll need to configure a type-safe SDK that communicates with your Server Middleware.

In the examples below, we assume that you have an Alokai app with the Unified Data Model. However, the approach for non-unified Alokai applications is similar.

There are various ways to configure the SDK, depending on your chosen framework. For Next.js and Nuxt, you can use the @vue-storefront/next and @vue-storefront/nuxt packages respectively. If you're looking for framework agnostic experience, you can use the @vue-storefront/sdk package.

Installation

To get started with the SDK within Next.js, first you have to install the @vue-storefront/next package. In the root of your Storefront project run:

Next.js
# Using yarn
yarn add --dev @vue-storefront/next

# Using pnpm
pnpm add -D @vue-storefront/next

# Using npm
npm install --save-dev @vue-storefront/next

Initializing the SDK

To use SDK in our application, we need to initialize it first. To do so, follow these steps:

  1. Create an sdk directory in the root of your project.
  2. Create the SDK Options file — sdk.options.ts in the sdk directory. In this file, we create a configuration object used to pass information to the SDK Factory about the address of the middleware, both in client and SSR mode, the identifier for cache busting, etc.

If you want to keep your Storefront more configurable, we highly recommend using the next-runtime-env library to read the environment variables from the runtime rather than hard-coding them during the build process.

sdk/options.ts
import type { CreateSdkOptions } from '@vue-storefront/next';
import { env } from 'next-runtime-env';

export function getSdkOptions() {
  const apiUrl = env('NEXT_PUBLIC_ALOKAI_MIDDLEWARE_API_URL') ?? '';
  const ssrApiUrl = env('NEXT_PUBLIC_ALOKAI_MIDDLEWARE_SSR_API_URL');
  const cdnCacheBustingId = env('NEXT_PUBLIC_ALOKAI_MIDDLEWARE_CDN_CACHE_BUSTING_ID') ?? 'no-cache-busting-id-set';
  const isMultiStoreEnabled = env('NEXT_PUBLIC_MULTISTORE_ENABLED') === 'true';
  if (!apiUrl) {
    throw new Error('NEXT_PUBLIC_ALOKAI_MIDDLEWARE_API_URL is required to run the app');
  }

  const options: CreateSdkOptions = {
    middleware: {
      apiUrl,
      cdnCacheBustingId,
      ssrApiUrl,
    },
    multistore: {
      enabled: isMultiStoreEnabled,
    },
  };

  return options;
}
  1. Create SDK Config file - sdk.config.ts. In this file, we define the configuration of different modules. We are making it a separate file to easily import it both on the server and the client. Create the SDK configuration by importing the createSdk function from the Next.js SDK and using the middlewareModule it provides. You should also import other modules you want to use.
sdk/config.ts
import { contentfulModule } from "@vsf-enterprise/contentful-sdk";
import { defineSdkConfig } from "@vue-storefront/next";
import type { CommerceEndpoints, UnifiedEndpoints } from "storefront-middleware/types";

export function getSdkConfig() {
  return defineSdkConfig(({ buildModule, config, middlewareModule, getRequestHeaders }) => ({
    commerce: buildModule(middlewareModule<UnifiedEndpoints>, {
      apiUrl: config.middlewareUrl + "/commerce",
      defaultRequestConfig: {
        headers: getRequestHeaders(),
      },
    }),
    cms: buildModule(contentfulModule, {
      apiUrl: config.middlewareUrl + "/cms",
    }),
  }));
}
  • The buildModule function is used to build the module. It expects the module and the module configuration as arguments.
  • The config is object containg data that is needed in module configuration such as:
    • the URL of the middleware instance
    • or the busting ID for CDN cache
  • The middlewareModule is an SDK module that ensures communication with the Server Middleware. It takes the UnifiedEndpoints type as a generic parameter. The UnifiedEndpoints type is a type that represents the endpoints of the Server Middleware.
  • The getRequestHeaders function is used to provide the incoming headers within your requests. You can use getRequestHeaders to access and proxy the initial cookie headers to SDK requests during SSR. Initial headers could be provided by the getSdk method. Check out examples there:

In the browser, getRequestHeaders will return an empty object.

  • The defineSdkConfig function returns the factory function for the SDK configuration as a second argument in createSdk. This factory function receives context, which is useful for creating the SDK configuration.
  1. Create SDK instance for server components. Let's do it in the sdk.server.ts in the sdk directory.

It is not necessary to name the file sdk.server.ts specifically or to keep it in the sdk directory, but it is recommended to keep it consistent with the rest of the Alokai project.

sdk/sdk.server.ts
import { createSdk } from "@vue-storefront/next";
import { getSdkOptions } from "./options";
import { getSdkConfig } from "./config";

export const { getSdk } = createSdk(
  getSdkOptions(),
  getSdkConfig()
);

export type Sdk = ReturnType<typeof getSdk>;

Let's break down the code above:

  • The createSdk function expects
    • base SDK options including the middleware and (optionally) the multistore configuration as a first argument,
    • and a factory function for the SDK configuration as a second argument. Those factory function receives a context, useful for creating the SDK configuration.
  • The createSdk function returns the getSdk function, which is used to retreive the new SDK instance.

Registering the SDK

Once you have initialized the SDK, you can register it in your application.

Alokai SDK can be used in two ways:

  • getSdk - returns the SDK instance, which can be used to call the SDK methods directly. This is useful for server-side rendering, as it allows you to call the SDK methods directly in your application.
  • createSdkContext - returns the SDK context, which can be used to share the same SDK instance on the Client side. This is useful for client-side rendering, as it allows you to share the same SDK instance across your application.

getSdk

getSdk is used to create the new SDK instance. This is especially useful for server-side fetching, as it returns a new SDK instance that can be used to call the SDK methods directly in your application.

Below is an example of how you can use getSdk in your application:

import { getSdk } from "@/sdk/sdk.server";

const sdk = getSdk();

createSdkContext

For client-side rendering, you can use createSdkContext. To use it, you'll need to create a new file in your application, for example sdk/sdk-provider.tsx:

'use client';

import { createSdkContext } from '@vue-storefront/next/client';

import type { Sdk } from './sdk.server';

export const [SdkProvider, useSdk] = createSdkContext<Sdk>();

Once you have created the SDK context, you can create client-side SDK instance and register it in your application. For example, if you're using the Pages Router, you can register it in pages/_app.tsx:

import type { AppProps } from "next/app";
import { SdkProvider } from "@/sdk/sdk-provider";
import { getSdkOptions } from "@/sdk/options";
import { getSdkConfig } from "@/sdk/config";

export default function App({ Component, pageProps }: AppProps) {
  const { getSdk } = createSdk(
    getSdkOptions(),
    getSdkConfig()
  );

  return (
    <SdkProvider sdk={getSdk()}>
      <Component {...pageProps} />
    </SdkProvider>
  );
}

If you're using the App Router, you can register it in app/layout.tsx:

app/layout.tsx
import { ReactNode } from "react";
import { PublicEnvProvider } from "next-runtime-env";
import { Providers } from "./providers";
import { getSdkOptions } from "@/sdk/options";

export default function RootLayout({ children }: { children: ReactNode }) {
  const sdkOptions = getSdkOptions();

  return (
    <html lang="en">
      <body>
        <PublicEnvProvider>
          <Providers sdkOptions={sdkOptions}>{children}</Providers>
        </PublicEnvProvider>
      </body>
    </html>
  );
}
app/providers.tsx
"use client";

import { ReactNode } from "react";
import type { CreateSdkOptions } from '@vue-storefront/next';
import { SdkProvider } from "@/sdk";
import { getSdkOptions } from "@/sdk/options";
import { getSdkConfig } from "@/sdk/config";

export function Providers({ children, sdkOptions }: { children: ReactNode, sdkOptions: CreateSdkOptions }) {
  const { getSdk } = createSdk(
    sdkOptions,
    getSdkConfig()
  );

  return <SdkProvider sdk={getSdk()}>{children}</SdkProvider>;
}

Don't be alarmed if you see a use client directive in the app/providers.tsx file. This will not turn your application into a client-side rendered application. All children inside the provider will be still rendered on the server-side by default. You can read more about use client directive in React Documentation.

Usage

Once you have registered the SDK in your application, you can start using it. Here's an example of how you can use the SAP Commerce Cloud SDK module in your application:

Pages Router
import { getSdk } from "@/sdk";

export function getServersideProps() {
  const sdk = getSdk();
  const { products } = await sdk.commerce.searchProduct();

  return {
    props: {
      products,
    },
  };
}

Code above is just an example of how you can use the SDK in your application. For more information about the available methods, please refer to the respective Integration's documentation.

That's it! You can now use VueStorefront SDK Module in your Next.js app ✨

Next Steps