You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/guides/6.multistore/3.patterns/5.subpath/3.subpath-middleware.md
+327Lines changed: 327 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -6,3 +6,330 @@ navigation:
6
6
---
7
7
8
8
# How to make sub-path routing on Middleware
9
+
10
+
## Understanding Sub-Path Routing Architecture
11
+
12
+
Sub-path routing in Alokai consists of two essential components: the storefront (frontend) and the middleware (backend). This architectural pattern allows you to serve different content or product catalogs through distinct URL paths within the same application. **This guide focuses exclusively on implementing the middleware aspect of sub-path routing** using the `ConfigSwitcher` extension, which dynamically changes integration configurations based on URL paths. For comprehensive information about implementing sub-path routing in storefront applications ([Next.js](./1.subpath-next.md) and [Nuxt](./2.subpath-nuxt.md)), please refer to the dedicated documentation for those frameworks.
13
+
14
+
The `ConfigSwitcher` extension in middleware enables dynamic configuration changes per request, allowing you to adapt your integrations like eCommerce or CMS settings based on specific URL paths. This powerful feature lets you serve different product catalogs or content collections through the same store instance but on different URL paths.
15
+
16
+
## Why Use Sub-Path Routing?
17
+
18
+
Sub-path routing allows you to create a more dynamic and flexible application that can adapt to different contexts based on the URL path. This approach can simplify your architecture by consolidating multiple configurations within a single store instance.
19
+
20
+
### Common Use Cases
21
+
22
+
1.**Multi-catalog stores**: Show different product catalogs based on URL paths
23
+
2.**Regional content variations**: Serve different content based on region-specific paths
24
+
3.**Brand-specific experiences**: Configure different brand experiences within the same store
25
+
26
+
**What You'll Learn**
27
+
28
+
::list{type="success"}
29
+
- Understanding how `ConfigSwitcher` extension works within the middleware
30
+
- Setting up multiple configurations within a single store
The `ConfigSwitcher` dynamically changes your middleware integration configuration based on the current request configuration ID sent with the `x-alokai-middleware-config-id` header. This means:
39
+
40
+
1.**Path-Based Configuration**
41
+
- Different configuration ID set by different paths allow to fetch data from different sources
- Multiple configurations can exist within a single store
48
+
49
+
3.**Same-Store Operation**
50
+
-`ConfigSwitcher` works within a single store instance
51
+
- It extracts the configuration ID from the request
52
+
- It executes queries using the appropriate configuration
53
+
54
+
::tip
55
+
The `ConfigSwitcher` only affects middleware configuration. The frontend remains the same, but it receives data from different sources based on the URL path.
56
+
::
57
+
58
+
### Key Distinctions To File-Based Inheritance
59
+
60
+
`ConfigSwitcher` operates differently from file-based inheritance:
Let's use SAPCC eCommerce integration as an example, by default the configuration file is located at: `/apps/storefront-middleware/integrations/sapcc/config.ts`
96
+
97
+
::warning
98
+
If you've overridden this file for a specific store (e.g., `store-c`), your configuration would be at:
By looking at this config file you can see that you can modify anything inside of the `api` or `OAuth` objects.
141
+
Other objects outside of the `configuration` object like the `extensions` and `location` cannot be modified using this tool.
142
+
143
+
## Implementation Guide
144
+
145
+
### Step 1: Create a configuration file
146
+
147
+
The first step is to create a dedicated configuration file for your integration, in which all possible configurations will be defined.
148
+
149
+
To keep things organized, you should create this file in the same directory as your integration's configuration file. The recommended name for this file is `switcher.config.ts`.
150
+
151
+
For the above SAPCC example, this file path would look like this: `/apps/storefront-middleware/integrations/sapcc/`.
152
+
153
+
::tip
154
+
If you've overridden the configuration file for some store, perform this step in its directory. If, for example, the store is `store-c`, the correct file path becomes:
Note that two keys have been placed here: `default` and `electronics`. Config object **must contain all** configuration keys that are used by a given store, even those you won't be changing. If you don't want to override some configuration, leave an empty object. Only make the changes you need, everything else will be inherited from the main configuration.
175
+
176
+
### Step 3: Apply overrides
177
+
178
+
Let's assume you want to change the product catalog for the configuration with ID `electronics`. To do it extend the given example with this change:
Note that the overrides are made here within the `api` object. This is important because configurations are merged with the main configuration, so their structure must be the same. In the base configuration, these keys are also nested inside of the `api` object.
198
+
199
+
### Step 4: Create `ConfigSwitcher` extension
200
+
201
+
Now let's create a `ConfigSwitcher` extension file.
202
+
Create a new file and name it `configSwitcher.ts`. Place it where the other extensions for the given integration are located. Continuing example with SAPCC, place it in the directory `/apps/storefront-middleware/integrations/sapcc/extensions`.
203
+
204
+
Fill the newly created file with the content from the snippet below:
Now you need to supply the configuration for the `ConfigSwitcher` extension with the configuration file you created in the previous step. Import it from the file and use it inside the extension:
Each integration requires its own `ConfigSwitcher` setup. If your project uses multiple integrations (e.g., SAPCC for eCommerce and Contentful for CMS), you'll need to configure the `ConfigSwitcher` separately for each one.
279
+
280
+
Every `ConfigSwitcher` extensions must use the same configuration IDs, which means that you cannot use different configuration IDs in different extensions.
281
+
282
+
## Typescript
283
+
284
+
To enable proper typing, create a new type that will define the configuration of the `ConfigSwitcher` extension. For this purpose, use the `Config` type from the base configuration of the integration whose configuration you're changing.
Since each configuration option can be overridden, and the type may have some keys marked as required that you won't be overriding, you must override the type so that none of the keys are required. Use the `Partial` type for this. If you want to override only selected values for the `api` key in the configuration, you can do it in the way shown below:
With sub-path routing properly configured, your Alokai application can now dynamically adapt to different contexts while maintaining a single codebase and deployment. This approach simplifies management while providing the flexibility needed for complex multi-catalog or multi-region scenarios.
0 commit comments