shopware/frontends - composables-next

Set of Vue.js composition functions that can be used in any Vue.js project. They provide state management, UI logic and data fetching and are the base for all guides in our building section.
Features
createShopwareContext
method to create a Vue 3 plugin to install- State management
- Logic for UI
- Communication with Store-API via api-client package
Setup
Install npm packages (composables & api-client):
pnpm add @shopware-pwa/composables-next @shopware/api-client @shopware/api-gen
yarn add @shopware-pwa/composables-next @shopware/api-client @shopware/api-gen
npm i @shopware-pwa/composables-next @shopware/api-client @shopware/api-gen
Now generate your types ysing the CLI:
pnpm shopware-api-gen generate --apiType=store
Initialize the api-client instance:
import { createAPIClient } from "@shopware/api-client";
import type { operations } from "#shopware";
export const apiClient = createAPIClient<operations>({
baseURL: "https://your-api-instance.com",
accessToken: "your-sales-channel-access-token",
});
app.provide("apiClient", apiClient);
Now, we can create a Vue 3 plugin to install a Shopware context in an app:
import { createShopwareContext } from "@shopware-pwa/composables-next";
const shopwareContext = createShopwareContext(app, {
devStorefrontUrl: "https://your-sales-channel-configured-domain.com",
});
app.use(shopwareContext);
Exclude @shopware-pwa/composables-next
package from pre-building process:
...
optimizeDeps: {
exclude: ["@shopware-pwa/composables-next"],
},
...
The example does not provide the session handling and that means you need to do few additional steps if you need to keep your session after the page reload (see the chapter below with 🍪)
Basic usage
Now you can use any composable function in your setup function:
<script setup>
import { useUser, useSessionContext } from "@shopware-pwa/composables-next/dist";
const { login } = useUser();
const { refreshSessionContext, sessionContext } = useSessionContext();
refreshSessionContext();
</script>
<template>
<pre>{{ sessionContext }}</pre>
<button @click="login({
username: "some-user",
password: "secret-passwd"
})">
Try to login!
</button>
</template>
Session persistence with 🍪
By default, the API-Client is stateless, but accepts an optional context token as a parameter while initializing an instance. In order to keep a session, install some cookie parser to work with cookies easier:
pnpm add js-cookie
yarn add js-cookie
npm i js-cookie
Let's get back to the step where the api-client
was initialized:
import { createAPIClient } from "@shopware/api-client";
import type { operations } from "#shopware";
import Cookies from "js-cookie";
const shopwareEndpoint = "https://demo-frontends.shopware.store/store-api";
export const apiClient = createAPIClient<operations>({
baseURL: shopwareEndpoint,
accessToken: "SWSCBHFSNTVMAWNZDNFKSHLAYW",
contextToken: Cookies.get("sw-context-token"),
});
apiClient.hook("onContextChanged", (newContextToken) => {
Cookies.set("sw-context-token", newContextToken, {
expires: 365,
path: "/",
sameSite: "lax",
secure: shopwareEndpoint.startsWith("https://"),
});
});
Thanks to this, the session will be kept to the corresponding sw-context-token
saved in the cookie, so it can be reachable also in the SSR. Check the example to see it in action:

TypeScript support
All composable functions are fully typed with TypeScript and they are registed globally in Nuxt.js application, so the type hinting will help you to work with all of them.
Links
Changelog
Full changelog for stable version is available here
Latest changes: 0.0.0-canary-20241204154203
Minor Changes
-
#1489 2c337b5
Thanks @mdanilowicz! - Changed registration
method in the useUser
composable. Because of changes in the double opt-in on registration flow in the Shopware backend we are adjusting this method on our side. In new approach we are checking active
and doubleOptInRegistration
properties that represents current status of the user.
-
#1369 13c83be
Thanks @mdanilowicz! - Added buildDynamicBreadcrumbs
method for building breadcrumbs structure
Added pushBreadcrumb
method to push single breadcrumb at the top of the breadcrumbs list
Added associations
option to the search
method in useProductSearch
composable
Patch Changes
-
#1449 8ba9702
Thanks @mkucmus! - Add configuration step to README.md
-
#1492 a03a492
Thanks @mkucmus! - useCustomerOrders
- added checkPromotions flag for loading orders
-
Updated dependencies [a87bbcf
, 13c83be
]:
- @shopware/api-client@0.0.0-canary-20241204154203
- @shopware-pwa/helpers-next@0.0.0-canary-20241204154203