Implement routing, localization, and multi-sites
This walkthrough builds on the app from Render SitecoreAI content in your app, which hard-codes a single site name and language from environment variables. In this walkthrough, you replace those hard-coded values with request-time resolution, so your app can serve multiple sites and languages, and honor content-managed redirects.
The walkthrough provides code examples for Astro 7 and Go front-end applications, and it describes how to:
- Fetch and cache your site list
- Resolve the site from the request hostname
- Resolve the locale from the request path
- Fetch and evaluate redirects
- Query the site dictionary
- Wire resolution into your routing
- Test your app
- Complete the Render SitecoreAI content in your app and the Enable visual editing walkthroughs. This walkthrough builds directly on the app you built in the previous walkthroughs.
- Review multi-site resolution, locale resolution, and redirects concepts.
- This walkthrough assumes your SitecoreAI environment has at least one additional site or language configured. If you only have a single site and language, you can still follow along. Site and locale resolution will simply always resolve to your one configured value.
Fetch and cache your site list
Add a function that retrieves your environment's site list and caches it in memory, so you don't query it on every request.
-
Add the following to
src/services/sitecoreClient.js:
-
Add the following to
main.go:
Resolve the site from the request hostname
Add a function that matches the request's hostname against your cached site list, falling back to a configured default site.
-
Add the following to
src/services/sitecoreClient.js: -
Update
src/pages/[...slug].astroto resolve the site from the request instead of using a hard-coded environment variable:
-
Add the following to
main.go: -
Update your route handler to resolve the site from the request's
Hostheader instead of a hard-coded environment variable:
Resolve the locale from the request path
Next, extract a locale segment from the request path (for example, /fr-fr/products), falling back to the resolved site's default language when no locale segment is present.
-
Add the following to
src/services/sitecoreClient.js: -
Update
src/pages/[...slug].astroto resolve the locale and strip it from the route path:
-
Add the following to
main.go: -
Update your route handler to resolve the locale and strip it from the route path:
Fetch and evaluate redirects
Add a function that retrieves a site's redirect rules and checks them against the current request before you fetch layout data.
-
Add the following to
src/services/sitecoreClient.js: -
Apply a matched redirect in
src/pages/[...slug].astro, before fetching layout data. TreatSERVER_TRANSFERas an internal rewrite rather than an HTTP redirect, so the visible URL doesn't change:
-
Add the following to
main.go: -
Apply a matched redirect in your route handler, before fetching layout data. Treat
SERVER_TRANSFERas an internal rewrite rather than an HTTP redirect, so the visible URL doesn't change:
These matching functions are simplified for the walkthrough. Refer to redirects for the full evaluation logic, including capture-group substitution in regex targets, the $siteLang token, and query string/locale preservation.
Query the site dictionary
Fetch the site dictionary for the resolved site and locale, so components can look up translated UI strings alongside page content.
-
Add the following to
src/services/sitecoreClient.js: -
Fetch it in
src/pages/[...slug].astro, alongside your layout data:The sample components built in the previous walkthrough don't read any dictionary keys, so this walkthrough doesn't wire
dictionaryfurther. In your own app, pass it to your layout/component rendering so components can look up translated strings by key.
-
Add the following to
main.go: -
Call it in your route handler, alongside fetching layout data. The sample components built in the previous walkthrough don't read any dictionary keys, so this walkthrough doesn't wire the result further. In your own app, pass it through to your component renderer so components can look up translated strings by key.
If your framework supports static site generation, you can enumerate every page for a site and language up front using the routes query, instead of resolving one path at a time as requests come in.
Wire resolution into your routing
With site, locale, and redirect resolution in place, update the call to fetchLayoutData to use the resolved site name and locale instead of the hard-coded environment variables from the previous walkthrough:
Test your app
- This walkthrough's
resolveLocalevalidates the URL's locale segment against your app's configured locale list, so add the new locale to your.envfile's list (for example,SITECORE_SITE_LOCALES=en,fr-fr) as a one-time setup step, matching a language your SitecoreAI site is available in. - Restart your development server.
- Request the root path with a locale segment, such as
/fr-fr, and confirm the layout data returned reflects that language. - If your environment has more than one site configured, test with a hostname that matches a non-default site (for example, by editing your local hosts file), and confirm the correct site's content is returned.
- Ask a content author to add a test redirect rule to your site's redirect map (or add one yourself, using the Content Editor), then request the redirect's source path and confirm your app redirects as expected.
- Confirm the site dictionary is populated by temporarily logging the resolved dictionary object, and check that a known key returns its expected translated value.
Next steps
If you run into issues, see Troubleshooting.