When working with Sitecore XM Cloud, you have the flexibility to host your head application on various hosting providers. Among these, Vercel stands out with its array of developer-friendly features. One notable feature is its automatic generation of preview deployments for each pull request. This means your entire team can quickly assess changes and ensure everything is functioning correctly without the need to maintain multiple environments separately, thus saving time and resources.
Moreover, a significant advantage of this setup is the ability to seamlessly switch between different sites within Vercel using just one URL. By simply appending "?sc_site=[enter site name]" to the URL, the head application automatically switches. Behind the scenes, the Sitecore multisite plugin checks the provided site name and orchestrates the switch while setting the necessary "sc_site" cookie to inform the head application which site to load.
Now armed with this knowledge, you can effortlessly navigate between any available sites within your Sitecore instance. However, one drawback is the requirement to know which sites are accessible. To address this, I devised a straightforward yet effective solution.
Introducing the "SiteSelection" component—a simple yet powerful tool I developed. This component fetches the list of available site names from an environment variable and presents them in a dropdown menu. With this intuitive interface, anyone can easily select the desired site, eliminating the need for manual URL adjustments and streamlining the process of switching between sites. This enhancement not only saves time but also reduces the potential for errors, ensuring a smoother development experience for all involved.
Here is an example of that code:
- NEXT_PUBLIC_FEATURE_SITE_SELECTION_ENABLED - this is the feature flags that enables this component. We don't want to render this component on a production URL. By default the component will not render itself.
- NEXT_PUBLIC_SITES_LIST - this is an array of sites (names) that are available on the XM Cloud instance.