Skip to content

Commit 3cfaa30

Browse files
authored
docs(tf-415): different configurations for same file (#7410)
1 parent d3b7ca0 commit 3cfaa30

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed

docs/content/guides/6.multistore/3.patterns/3.data/1.different-config-same-integration.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,212 @@ navigation:
66
---
77

88
# Different configurations for the same integration
9+
10+
When building a multistore solution, you'll often need to use the same integration with different configurations across your stores. This guide explains how to efficiently manage multiple configurations for the same integration while maintaining a clean and maintainable codebase.
11+
12+
**What You'll Learn**
13+
14+
::list{type="success"}
15+
- Understanding when you need different configurations for the same integration
16+
- How to implement store-specific integration configurations
17+
- Best practices for managing integration configurations across multiple stores
18+
- Using environment variables to reduce code duplication
19+
::
20+
21+
## When Do You Need Multiple Configurations?
22+
23+
Let's explore some common scenarios where you might need different configurations for the same integration:
24+
25+
### Use Case 1: Different Content in Stores Using a CMS Integration
26+
27+
Imagine running two stores where the same CMS integration is used, but each store needs distinct content. For instance, a fashion store and a sports store have entirely different marketing copy and visuals.
28+
29+
### Use Case 2: Multiple Product Catalogs for an eCommerce Integration
30+
31+
Suppose your solution uses an integration with SAP Commerce Cloud (SAPCC). You want to build two stores—one for B2C and another for B2B. Each store requires a different product catalog.
32+
33+
## Solution: Store-Specific Integration Configurations
34+
35+
You can create and customize the **middleware** configuration for each store using **file-based inheritance**. Here's how it works:
36+
37+
1. Create a configuration file for each store
38+
2. Override specific values (like product catalogs) in each file
39+
3. Each store will have its own configuration with values specific to its needs
40+
41+
### Implementation Example
42+
43+
For our SAP Commerce Cloud example with B2C and B2B stores:
44+
45+
```ts
46+
// B2C store
47+
// /apps/stores/b2c/storefront-middleware/integrations/sapcc/config.ts
48+
49+
export const config = {
50+
configuration: {
51+
api: {
52+
baseSiteId: 'b2c-site', // store specific value
53+
catalogId: 'b2cProductsCatalog', // store specific value
54+
// ...
55+
},
56+
}
57+
}
58+
```
59+
60+
```ts
61+
// B2B store
62+
// /apps/stores/b2b/storefront-middleware/integrations/sapcc/config.ts
63+
64+
export const config = {
65+
configuration: {
66+
api: {
67+
baseSiteId: 'b2b-site', // store specific value
68+
catalogId: 'b2bProductsCatalog', // store specific value
69+
// ...
70+
},
71+
}
72+
}
73+
```
74+
75+
After this operation, the file structure will look like this:
76+
77+
```bash
78+
apps/
79+
├── storefront-middleware
80+
| ├── integrations
81+
| | └── sapcc
82+
| | └── config.ts # base config will be overridden by stores config
83+
└── stores
84+
├── b2c
85+
| ├── storefront-middleware
86+
| | ├── integrations
87+
| | | └── sapcc
88+
| | | └── config.ts # new file for b2c store
89+
└── b2b
90+
└── storefront-middleware
91+
└── integrations
92+
└── sapcc
93+
└── config.ts # new file for b2b store
94+
```
95+
96+
:::tip
97+
This approach leverages the [file-based inheritance](/guides/multistore/tooling-and-concepts/file-based-inheritance) system in Alokai, allowing you to override only what's different while inheriting everything else.
98+
:::
99+
100+
## Best Practices
101+
102+
### 1. Use Environment Variables
103+
104+
Instead of creating separate configuration files for each store, leverage **environment variables** where possible. Different environment settings can dynamically configure the middleware or storefront application for each store.
105+
106+
This approach helps avoid duplicated files and makes your solution more modular, clean, and easy to maintain.
107+
108+
Let's improve our previous configuration example to use environment variables:
109+
110+
```ts
111+
// base source code, not nested in stores
112+
// /apps/storefront-middleware/integrations/sapcc/config.ts
113+
114+
const { BASE_SITE_ID, PRODUCT_CATALOG_ID } = process.env;
115+
116+
export const config = {
117+
configuration: {
118+
api: {
119+
baseSiteId: BASE_SITE_ID, // dynamic value
120+
catalogId: PRODUCT_CATALOG_ID, // dynamic value
121+
// ...
122+
},
123+
}
124+
}
125+
```
126+
127+
For local development, use `.env` files specific to each store:
128+
129+
```plaintext
130+
# b2c store
131+
# /apps/stores/b2c/storefront-middleware/.env
132+
BASE_SITE_ID="b2c-site"
133+
PRODUCT_CATALOG_ID="b2cProductsCatalog"
134+
135+
# b2b store
136+
# /apps/stores/b2b/storefront-middleware/.env
137+
BASE_SITE_ID="b2b-site"
138+
PRODUCT_CATALOG_ID="b2bProductsCatalog"
139+
```
140+
141+
:::warning
142+
`.env` files are git ignored out of the box, but it's important to verify this and ensure they are not
143+
accidentally committed. Use `.env.example` to provide a template on what variables are used.
144+
:::
145+
146+
### 2. Use small files for overrides
147+
148+
Where environment variables aren't suitable (like in cases requiring a lot of customization), follow these steps:
149+
150+
- Separate the **overridable parts** of your configurations into distinct files
151+
- Use **file-based inheritance** to share the majority of the configuration
152+
- Override only what needs to change for individual stores
153+
154+
This approach ensures your codebase remains clean and maintainable without introducing unnecessary code duplication.
155+
156+
See the example below to learn how to implement it:
157+
158+
First, define the usage of the new override file in the base code:
159+
160+
```ts
161+
// base source code, not nested in stores
162+
// /apps/storefront-middleware/integrations/sapcc/config.ts
163+
164+
// new file with part of the store specific configuration
165+
import { configPart } from './storeSpecificConfig';
166+
167+
export const config = {
168+
configuration: {
169+
api: {
170+
...configPart,
171+
// ...
172+
},
173+
}
174+
}
175+
```
176+
177+
Next, create a small file containing the store-specific portion of the configuration:
178+
179+
```ts
180+
// B2C store
181+
// /apps/stores/b2c/storefront-middleware/integrations/sapcc/storeSpecificConfig.ts
182+
183+
export const configPart = {
184+
baseSiteId: 'b2c-site', // store specific value
185+
catalogId: 'b2cProductsCatalog', // store specific value
186+
};
187+
```
188+
189+
With this setup, the store-specific configuration will seamlessly integrate with the base configuration
190+
191+
## Setting Up Environment Variables for instances in Alokai Console
192+
193+
Every instance can have its own environment variables configured for middleware and storefront application. To find out how to provide environment variables, please check the [Console docs](/console/instance/settings/environment-variables).
194+
195+
For developers, working with `.env` files is straightforward:
196+
1. Copy the `.env.example` file
197+
2. Rename it to `.env` and customize its values for the specific store
198+
3. The environment variables will be automatically loaded when you run the store locally
199+
200+
## Final Recommendations
201+
202+
1. **Modular Setup**: Use environment variables and file-based inheritance to avoid duplication and ensure shared code remains central.
203+
2. **Clean Configuration Files**: Keep files minimal with overrides only for values that differ between stores.
204+
3. **Versioned Examples**: Always provide `.env.example` templates and ensure sensitive data isn't pushed to the repository.
205+
206+
By following these practices, your setup will remain clean, scalable, and easy to manage, no matter how many stores or integrations you add in the future.
207+
208+
::card{title="Next: Different integrations in each store" icon="tabler:number-2-small" }
209+
210+
#description
211+
Learn how to implement and manage different integrations across your stores.
212+
213+
#cta
214+
:::docs-button{to="/guides/multistore/patterns/data/different-integrations-per-store"}
215+
Next
216+
:::
217+
::

0 commit comments

Comments
 (0)