Skip to content

Commit f51edd8

Browse files
authored
Merge pull request #7478 from umbraco/v17/feature/additiona-preview-environments
Rerwite the "Additional preview environments support" article and correct URL provider references
2 parents e5d7e95 + 1fd4004 commit f51edd8

File tree

3 files changed

+117
-9
lines changed

3 files changed

+117
-9
lines changed

17/umbraco-cms/reference/content-delivery-api/additional-preview-environments-support.md

Lines changed: 108 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,120 @@ description: >-
55

66
# Additional preview environments support
77

8-
{% hint style="warning" %}
9-
The original contents of this article, explaining how to change the backoffice UI, does not apply anymore to Umbraco 15 and above and has been removed.
10-
The way to achieve this beyond Umbraco 14 is to create and register an [Extension](../../customizing/extending-overview) to modify the backoffice UI preview button.
11-
{% endhint %}
12-
13-
With Umbraco, you can save and preview draft content before going live. The preview feature allows you to visualize how a page will look like once it is published, directly from within the backoffice. This is also possible for the Content Delivery API data. You can extend the preview functionality in the backoffice by configuring external preview URLs for client libraries consuming the Content Delivery API.
8+
With Umbraco, you can save and preview draft content before going live. The preview feature allows you to visualize how a page will look once it is published, directly from within the backoffice. This is also possible for the Content Delivery API data. You can extend the preview functionality in the backoffice by configuring external preview URLs for client libraries consuming the Content Delivery API.
149

1510
{% hint style="info" %}
16-
To get introduced to the preview functionality in the Content Delivery API, please refer to the [Preview concept](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#preview) section.
11+
To learn more about the preview functionality in the Content Delivery API, see the [Preview concept](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#preview) section.
1712
{% endhint %}
1813

14+
Generating external preview URLs for the backoffice requires implementation on both the client and server sides.
15+
16+
## Server-side implementation
17+
18+
On the server-side, you'll need a custom `IUrlProvider` implementation. This is responsible for generating the preview URL for a given piece of content.
19+
20+
An external preview environment likely requires some kind of authentication to allow preview. The URL provider should perform this as well.
21+
22+
Here's an example of what a URL provider could look like:
23+
24+
{% code title="MyUrlProvider.cs" lineNumbers="true" %}
25+
```csharp
26+
using Umbraco.Cms.Core.Models;
27+
using Umbraco.Cms.Core.Models.PublishedContent;
28+
using Umbraco.Cms.Core.Routing;
29+
30+
namespace My.Site;
31+
32+
public class MyUrlProvider : IUrlProvider
33+
{
34+
public string Alias => "MyUrlProvider";
35+
36+
public UrlInfo? GetUrl(IPublishedContent content, UrlMode mode, string? culture, Uri current)
37+
=> null;
38+
39+
public IEnumerable<UrlInfo> GetOtherUrls(int id, Uri current)
40+
=> [];
41+
42+
public async Task<UrlInfo?> GetPreviewUrlAsync(IContent content, string? culture, string? segment)
43+
{
44+
var token = await GetPreviewTokenAsync();
45+
return new UrlInfo(
46+
url: new Uri($"https://my.preview.environment/?id={content.Key}&culture={culture}&token={token}"),
47+
provider: Alias,
48+
culture: culture,
49+
message: null,
50+
isExternal: true);
51+
}
52+
53+
private async Task<string> GetPreviewTokenAsync()
54+
{
55+
// this is where you'll perform auth against the external preview environment and return an auth token
56+
}
57+
}
58+
```
59+
{% endcode %}
60+
61+
The URL provider must be registered with Umbraco using a composer:
62+
63+
{% code title="MyUrlProvider.cs" lineNumbers="true" %}
64+
```csharp
65+
using Umbraco.Cms.Core.Composing;
66+
67+
namespace My.Site;
68+
69+
public class MyUrlProviderComposer : IComposer
70+
{
71+
public void Compose(IUmbracoBuilder builder)
72+
=> builder.AddUrlProvider<MyUrlProvider>();
73+
}
74+
```
75+
{% endcode %}
76+
77+
## Client-side implementation
78+
79+
On the client side, you'll need an extension of:
80+
81+
- Type: `workspaceActionMenuItem`.
82+
- Kind: `previewOption`.
83+
1984
{% hint style="info" %}
20-
Support for configuring additional preview environments in the Content Delivery API was introduced in version 12.3.
85+
You can read more about extensions in the [Extending the Umbraco Backoffice](https://docs.umbraco.com/welcome/getting-started/developing-websites-with-umbraco/extending-the-umbraco-backoffice) article.
2186
{% endhint %}
2287

88+
Here's a sample extension:
89+
90+
```json
91+
{
92+
"name": "My UrlProvider",
93+
"alias": "My.UrlProvider",
94+
"extensions": [
95+
{
96+
"type": "workspaceActionMenuItem",
97+
"kind": "previewOption",
98+
"alias": "My.PreviewOption",
99+
"name": "My Preview Option",
100+
"forWorkspaceActions": "Umb.WorkspaceAction.Document.SaveAndPreview",
101+
"weight": 99,
102+
"meta": {
103+
"icon": "icon-umbraco",
104+
"label": "My preview option",
105+
"urlProviderAlias": "MyUrlProvider"
106+
}
107+
}
108+
]
109+
}
110+
```
111+
112+
This extension works with the URL provider above because the `urlProviderAlias` of the extension matches the `Alias` of the URL provider.
113+
114+
Save the extension as `umbraco-package.json` in a folder under `App_Plugins` and (re)start the site. The result is a new preview option in a pop-up over the "Save and preview" button:
115+
116+
![External preview options in a pop-up over the "Save and preview" button](images/external-preview-option.png)
117+
23118
{% hint style="info" %}
24-
An upcoming release of Umbraco will allow users to configure custom preview URLs directly in the backoffice.
119+
The default "Save and preview" button is also an extension of this type.
120+
121+
In other words, multiple preview options can co-exist. If you have multiple external environments, you can create preview options for all of them.
122+
123+
The extension `weight` determines the order of appearance. A `weight` above 100 will swap the default preview option with the custom one.
25124
{% endhint %}
13.5 KB
Loading

17/umbraco-cms/reference/routing/request-pipeline/outbound-pipeline.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,16 @@ Umbraco ships with a `DefaultUrlProvider`, which provides the implementation for
159159
// This one is initialized by default
160160
public class NewDefaultUrlProvider : IUrlProvider
161161
{
162+
public string Alias => …;
163+
162164
public virtual UrlInfo GetUrl(IPublishedContent content, UrlMode mode, string? culture, Uri current)
163165
{…}
164166

165167
public virtual IEnumerable<UrlInfo> GetOtherUrls(int id, Uri current)
166168
{…}
169+
170+
public virtual Task<UrlInfo?> GetPreviewUrlAsync(IContent content, string? culture, string? segment)
171+
{…}
167172
}
168173
```
169174

@@ -199,6 +204,10 @@ public interface IUrlProvider
199204
UrlInfo? GetUrl(IPublishedContent content, UrlMode mode, string? culture, Uri current);
200205

201206
IEnumerable<UrlInfo> GetOtherUrls(int id, Uri current);
207+
208+
Task<UrlInfo?> GetPreviewUrlAsync(IContent content, string? culture, string? segment);
209+
210+
public string Alias { get; }
202211
}
203212
```
204213

0 commit comments

Comments
 (0)