Skip to content

Commit 9da252b

Browse files
committed
Merge branch 'main' of https://github.com/vuestorefront/vue-storefront into docs/multibrand
2 parents 53fe252 + a8e2980 commit 9da252b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1323
-13
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
* @vuestorefront/integrations-team
1+
* @vuestorefront/application-team
22
actions/check-licenses/* @filrak @rsurowieckivsf
33
docs/**/*.md

docs/content/guides/1.index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ Alokai is not a cookie-cutter solution, it is meant to be able to handle even th
3434
Learn how to create efficient and unified multigeo/multibrand/multivendor setups with Alokai.
3535
::
3636

37+
#section-4
38+
:card{to="/guides/kubernetes-probe" title="Kubernetes Probes" description="Alokai Cloud customers' middleware and frontend apps are deployed in Kubernetes. Check how Kubernetes mechanisms are used by Alokai Cloud" icon="tabler:heart-rate-monitor"}
39+
40+
#section-5
41+
:card{to="/guides/best-practices" title="Best Practices" description="Check what to follow to keep your storefront in the best possible shape" icon="tabler:rosette-discount-check"}
42+
43+
3744
::
3845

3946

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Best Practices
2+
3+
In this guide, we have compiled a collection of best practices to help you optimize your development workflow, improve code quality, and ensure maintainability.
4+
5+
::card{title="Data Fetching" icon="tabler:mobiledata" to="/guides/best-practices/data-fetching" }
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Data Fetching best practices
2+
3+
Data fetching is a critical aspect of web application performance. While Alokai leverages established best practices from frameworks like Next.js and Nuxt, this guide explains how to effectively implement these strategies in the context of Alokai's architecture.
4+
5+
Read about data fetching in general in Next.js / Nuxt official documentation:
6+
7+
- [Data fetching in Next.js](https://nextjs.org/docs/app/building-your-application/data-fetching)
8+
- [Data fetching in Nuxt](https://nuxt.com/docs/getting-started/data-fetching)
9+
10+
## Data fetching strategies
11+
12+
There are three data-fetching strategies:
13+
14+
- **Client-Side Fetching:** Data is fetched by the client (usually a browser) directly from an API.
15+
- **Server-Side Fetching / Server-Side Rendering (SSR):** Data is fetched on the server during the request and delivered to the client as pre-rendered HTML.
16+
- **Static Site Generation (SSG):** Pages are pre-rendered at build time, suitable for content that doesn't change frequently. E-commerce applications are highly dynamic, so we’re not covering this strategy as it’s rarely used in this context. Cached server-side rendered pages are a good replacement for SSG.
17+
18+
The diagram helps to visualize the two first strategies:
19+
20+
<img src="/images/data-fetching-strategies.svg" alt="data fetching strategies" class="mx-auto">
21+
22+
The `Storefront` is a Next/Nuxt application. Api is any 3rd party system.
23+
24+
Animation below visualizes the difference between server and client side fetching. Product data is fetched on the server and rendered to HTML, so the product data is visible immediately after page load. Cart data is fetched client side, so we see a spinner that is replaced by content when cart data is loaded.
25+
26+
<img src="/images/ssr-csr.gif" alt="data fetching animation" class="mx-auto">
27+
28+
## How to pick data fetching strategy
29+
30+
The rule of thumb is: _always use server-side fetching except for_:
31+
32+
- **lazy loaded data** - data that is not visible immediately on page load and can be removed from server-side rendered html to reduce its size. For example:
33+
- product reviews should appear only when user clicks expand on the “reviews accordion”
34+
- content below the fold is fetched on scroll
35+
36+
- **personalized data** - data that is different for each user or group. Fetching such data server side would not only cause flickering but also might cause problem with caching ([read more about caching](/storefront/features/cdn/making-ssr-cacheable)). For example:
37+
- pricing data might be different for each customer group
38+
- product recommendations are based on individual user’s journey
39+
- cart content is specific for each user
40+
41+
## Data fetching in Alokai
42+
43+
Alokai architecture introduces the Middleware in between the front-end application and the APIs. This adds two additional possible routes to our diagram, but most of the traffic should go through the middleware, because:
44+
45+
- it is cached on the CDN
46+
- it keeps your architecture simple
47+
- you can easily monitor middleware traffic in the console
48+
- enables [data federation](/middleware/guides/federation)
49+
- ensures middleware reusability and omnichannel capabilities
50+
51+
<img src="/images/data-fetching-alokai-strategies.svg" alt="data fetching strategies with alokai" class="mx-auto">
52+
53+
Fortunately, we can reject the route between the server and the API, because:
54+
55+
- effectively it is similar to reaching through the middleware, but adds unnecessary complexity to the architecture,
56+
- tightly couples the storefront with the API.
57+
58+
The direct route between the browser and API should be avoided, because it:
59+
60+
- tightly couples storefront with the API,
61+
- reduces performance by bypasses CDN cache.
62+
63+
Use direct direct browser to API calls only when communicating via middleware is not possible or requires unnecessary effort. Sample scenarios when it might be valid to communicate directly between client and API:
64+
65+
- the communication requires extremely low latency - e.g. voice or video stream
66+
- communication with API is done via some SDK that requires direct client (browser) interaction - e.g. analytics or authentication services
67+
68+
## CDN Caching
69+
70+
To further improve your application performance we encourage you to enable the CDN. The CDN is capable of caching responses from the server and the middleware to the browser.
71+
72+
<img src="/images/data-fetching-cdn.svg" alt="data fetching and CDN" class="mx-auto">
73+
74+
::info
75+
[Read more about caching here.](/storefront/features/cdn/making-ssr-cacheable)
76+
::
77+
78+
## Examples
79+
80+
### Server-Side fetching in Next.js
81+
82+
In this example we fetch product data on the server and display it's name.
83+
84+
```tsx
85+
import { getSdk } from '@/sdk';
86+
87+
export default async function ServerSideDataFetching() {
88+
const sdk = getSdk(); // get the SDK instance on the server side.
89+
90+
const response = await sdk.unified.getProductDetails({ id: '300013358' }); // fetch product data
91+
const product = response?.product ?? null;
92+
93+
return <h1>Product name: {product.name}</h1>; // display product name
94+
}
95+
```
96+
97+
### Client-side fetching in Next.js
98+
99+
In this example we fetch product data on the client. We display loader while data is being fetched. Tanstack query is used to fetch data and manage component state.
100+
101+
```tsx
102+
'use client'; // tell nextjs to render this component on the client side
103+
104+
import { SfLoaderCircular } from '@storefront-ui/react';
105+
import { useQuery } from '@tanstack/react-query';
106+
107+
import { useSdk } from '@/sdk/alokai-context';
108+
109+
export default function ClientSideDataFetching() {
110+
const sdk = useSdk(); // get the SDK instance on the client side.
111+
112+
// use tanstack query to fetch product data and manage the loading state
113+
const { data: productData, isLoading } = useQuery({
114+
queryFn: () => sdk.unified.getProductDetails({ id: '300013358' }),
115+
queryKey: ['product'],
116+
});
117+
118+
// display a loader while product data is loading
119+
if (isLoading || !productData) {
120+
return <SfLoaderCircular />;
121+
}
122+
123+
return <h1>Product name: {productData.product.name}</h1>; // display product name
124+
}
125+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
title: Best Practices
2+
sidebarRoot: true
3+
navigation:
4+
icon: tabler:rosette-discount-check
5+

0 commit comments

Comments
 (0)