Dynamic Preview URLs
By default, Builder adds your targeted URL path to the model preview url you define, but there are cases where you need more to calculate the preview URL, such as:
- Using locale codes as top routes, or even different domains per locale
- Serving a different site—or top level domain—for mobile or desktop
- Section models that are rendered by a custom field; for example, a blog post model with a slug custom field that you’re rendering on
- Previewing live content on your live site and providing a fallback preview on a path, such as
- Using custom fields or targeting for specific subpaths; for example
This document covers:
To get the most out of this document, you should be familiar with:
Example: Setting up a Dynamic Preview URL on a blog post model
- Go to Models.
- Open the Page or Section model you'd like to add a Dynamic Preview URL to.
- Add a
slugcustom field with a type of
- Click on the code icon,
< >, to the right of the Preview URL field. This opens a code editor with comments giving more context on this feature.
- Add your custom code for determining your dynamic URLs.
- Click the X icon to close and click Save on the model.
The video below demonstrates these steps on a blog post Section model.
How the example model and code work together
The Post Section model in this example has a required
slug field so that when a user creates a blog post using the Post model, Builder prompts them to provide a
The code then uses the slug to create the URL if
The example code in the video does two things:
- If the content is live, the example code returns a URL that Builder can use to create the Dynamic Preview URL. The first part, you create by providing your site's URL. The second, dynamic part pulls the
slugfrom the example's Post Section model.
- If the content isn't live, the URL uses a default path, here
blog/__builder_editing__. Your path can be different than the example.
Be sure to replace
https://your-site.com/your-directory/ with your site and directory. In the video example, the blog is hosted on Vercel, but your URL will be different.
For an example app, see Builder's Next.js CMS Blog example on GitHub. To use the example with Builder as in the video:
The Dynamic Preview URLs feature is on by default; however, if the code icon,
< >, isn't available on the model in the Preview URL field, check that it is on. Go to Account Settings > Advanced Settings > Editor. Make sure Advanced Preview URL Logic is toggled to the on position.
Previewing your work with a Dynamic Preview URL
To use a Dynamic Preview URL, you must have already set up the logic on the model to use your site's live URL and the path you want. Use your Dynamic Preview URL by:
- Create a content entry with the model that uses the Dynamic Preview URL.
- In the Visual Editor, add or edit content if desired.
- Click the eyeball icon in the upper right and select View Current Draft.
When the browser loads your content, the URL should include your URL and path.
The video below shows creating and previewing a blog post using the example Post Section model with a Dynamic Preview URL from the previous section in this document.
Available objects and properties
When writing your own custom logic to determine a section or page preview URL, Builder offers helpful objects and properties:
a JSON representation of the current state of content
the current space settings
An object representing the targeting set on content. If a targeting attribute is an
Use for running async code. For more information on
The current content model, such as the Page or Section model you're working with.
The device choice in the editor, it will be either
contentModel object includes several important properties:
truewhen the content is currently live on production and not scheduled for a future or prior date.
startDate: start date of scheduled publish, if scheduled
endDate: end date of scheduled publish, if scheduled
Make sure that your Dynamic Preview URL logic returns a string. Each of the following examples returns a string while using different dynamic features.
Example 1: Returning Blog URLs
A blog might use a check to determine if your content is live and generate a URL that uses your
slug or else use a fallback URL for editing based on that condition. Note that
slug would be a custom field you add to the model, as in Setting up a Dynamic Preview URL on a model.
Example 2: Determining category
This example uses a
category hero model that you can target at a specific category or all categories:
Example 3: Cart modal upsell
This returns a URL for a Section model that is for cart modal upsells:
Example 4: Returning URLs specific to locale
This example creates URLs for localized Pages:
Need Expert help?
Submit a project to our partners, BuildQuick, and be matched with a Builder expert.