đ Custom Routing in XM Cloud Starter Kit: Handling /services/* with a different Layout
By default, the Sitecore XM Cloud Starter Kit uses a single dynamic catch-all route ([[...path]].tsx) under src/pages/ to serve all pages. This setup uses Static Site Generation (SSG) and a common layout across the board.
However, in real-world scenarios, you might want specific routes—like /services/*—to:
-
Use a different layout
-
Implement custom data fetching logic
-
Possibly enable Server-Side Rendering (SSR)
In this post, we’ll walk through how to customize routing in the XM Cloud Starter Kit so that URLs like /services/test are handled by a separate catch-all route and layout, completely isolated from the default setup.
❗ This post focuses only on routing and layout customization — not SSR implementation (though this setup will make adding SSR easier later).
đ§Š Use Case
Suppose your homepage has a /services/ folder with a few subpages like:
You want:
-
/services/*to be handled differently than the default route -
A different layout (e.g. without a hero banner or global nav)
-
The ability to fetch content dynamically (e.g. using GraphQL)
Let’s get started.
đ Step 1: Create Folder Structure
First, we’ll mirror the default route inside a custom folder.
đ§ Create a services folder inside src/pages:
đ Duplicate [[...path]].tsx
đ Duplicate Layout.tsx
Copy it into src/pages/services/.
You should now have:
đĄ️ Step 2: Update Middleware
To prevent /services/* routes from being caught by the default [[...path]].tsx, you need to update your middleware.ts.
✏️ Modify middleware.ts:Exxclude services/
This change ensures that requests like /services/test are routed to pages/services/[[...path]].tsx instead of the default handler.
đ§ Step 3: Customize getStaticPaths
Inside pages/services/[[...path]].tsx, update the getStaticPaths method:
Setting fallback: 'blocking' ensures the pages are built when requested — no need to prebuild them during the build process.
⚙️ Step 4: Custom getStaticProps with GraphQL
This is where we dynamically fetch data based on the URL.
What’s happening here?
-
We simulate a dynamic path by modifying
context.params. -
We fetch item data using a custom GraphQL method(GetWildcardPage).
-
If the item exists, we pass its actual URL path to
sitecorePagePropsFactory.
đ Step 5: Create the GraphQL Query
Inside lib ,create a graphql folder .This folder conatines client.ts file having the GetwildCardpage.Refer to the code repo for details
Types
You can define TypeScript interfaces for your response in:
đ¨ Step 6: Apply a Custom Layout
To use a different layout for /services/* pages, wrap your page component accordingly.
Example:
This makes your /services/* pages look and behave differently from the rest of your site.
✅ Final Result
When you navigate to:
You should see:
-
A different layout
-
Custom GraphQL-driven data
-
Dynamically rendered page
-
Logs in the terminal for debugging
đ Summary
| Step | What You Did |
|---|---|
| đ§ Folder Setup | Created pages/services/[[...path]].tsx |
| đĄ️ Middleware | Excluded /services/* from default routing |
| đ§ Static Paths | Disabled pre-building using fallback: 'blocking' |
| đ Data Fetching | Implemented custom GraphQL logic |
Comments
Post a Comment