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.
These packages also provide tools for handling the global state management.
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:
# 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:
- Create an
sdk
directory in the root of your project. - Create the SDK Options file —
sdk.options.ts
in thesdk
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.
import { resolveSdkOptions } 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_ALOKAI_MULTISTORE_ENABLED') === 'true';
if (!apiUrl) {
throw new Error('NEXT_PUBLIC_ALOKAI_MIDDLEWARE_API_URL is required to run the app');
}
const options = resolveSdkOptions({
middleware: {
apiUrl,
cdnCacheBustingId,
ssrApiUrl,
},
multistore: {
enabled: isMultiStoreEnabled,
},
});
return options;
}
- Create SDK Config file -
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 thecreateSdk
function from the Next.js SDK and using themiddlewareModule
it provides. You should also import other modules you want to use.
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 theUnifiedEndpoints
type as a generic parameter. TheUnifiedEndpoints
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 usegetRequestHeaders
to access and proxy the initial cookie headers to SDK requests during SSR. Initial headers could be provided by thegetSdk
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 increateSdk
. This factory function receives context, which is useful for creating the SDK configuration.
- Create SDK instance for server components. Let's do it in the
sdk.server.ts
in thesdk
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.
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 thegetSdk
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();
createAlokaiContext
For client-side rendering, you can use createAlokaiContext
. This function serves for two purposes:
- providing the SDK context
- providing the global state management context and hooks for handling the state of the application
To use it, you'll need to create a new file in your application, for example sdk/alokai-context.tsx
:
'use client';
import { createAlokaiContext } from '@vue-storefront/next/client';
import type { Sdk } from './sdk.server';
export const { AlokaiProvider, useSdk } = createAlokaiContext<Sdk>();
This is also a place where you can import hooks for handling the state management. You can read more about the state management in the State Management page.
Once you have created the Alokai context, you can create client-side SDK instance and register it in your application.
You can do it in two steps:
- Retrieve the SDK config in the server component in
app/[locale]/layout.tsx
:
// app/[locale]/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>
);
}
- Pass the SDK options to the
Providers
client component and initialize the SDK instance along with theAlokaiProvider
:
// components/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 (
<AlokaiProvider sdk={getSdk()}>
{children}
</AlokaiProvider>
)
}
Don't be alarmed if you see a use client
directive in the components/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:
import { getSdk } from "@/sdk";
export async 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 ✨