I have a section called ‘practices’ that i have created and following the default example I have set the preview url to be dynamic using the following logic:
This works but when I want to deploy the site to another domain I need to update the domain name in multiple places. Ideally I would rather update the domain name of the site on the space settings and then reference that value here. Something like:
I am sure this is possible but I need help figuring out the syntax. Please advise.
Please fill out as many of the following questions as possible so we can help you promptly! If you don’t know how to answer or it does not apply, feel free to remove it or leave it blank.
What are you trying to accomplish e.g. I want to integrate builder on a landing page
Screenshots or video link Screenshots of your issue, or even better a link to a video of what is happening or what you are trying to accomplish. We love using loom for that sort of thing
Code stack you are integrating Builder with e.g. NextJS, react, Shopify
Reproducible code example If you are having integration errors, please link to codesandbox or copy and paste your problematic code here. The more detail the better!
In the Builder.io interface, go to the Settings for your space.
Add or Verify Site URL:
Make sure there is a site URL setting, commonly named siteUrl. If it doesn’t exist, you might need to add a custom field or set it in space settings directly.
Step 2: Use the siteUrl in Preview URL Logic
Edit the Preview URL Logic for Your Section:
In the logic for your model’s preview URL, use the space setting:
// The default base URL
const defaultBaseUrl = 'http://localhost:3000';
// Use siteUrl from space settings or default to localhost
const baseUrl = space.siteUrl || defaultBaseUrl;
// Construct the full preview URL
return `${baseUrl}/practices/${content.data.name || '_'}`;
Save and Test:
Save the settings and test your content preview. It should dynamically use the siteUrl defined in your space settings.