Skip to content

Commit dfbb182

Browse files
Handle invariant culture in RedirectTrackingHandler
GetRouteById and RedirectUrlService expects the culture to be null if it's invariant, however, Cultures in IPublished content uses empty string for invariant culture
1 parent 0c7ef06 commit dfbb182

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/Umbraco.Infrastructure/Routing/RedirectTrackingHandler.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,33 @@ private void StoreOldRoute(IContent entity, OldRoutesDictionary oldRoutes)
9999
{
100100
return;
101101
}
102-
var contentCache = publishedSnapshot.Content;
103-
var entityContent = contentCache?.GetById(entity.Id);
102+
103+
IPublishedContentCache contentCache = publishedSnapshot.Content;
104+
IPublishedContent entityContent = contentCache?.GetById(entity.Id);
104105
if (entityContent == null)
106+
{
105107
return;
108+
}
106109

107110
// get the default affected cultures by going up the tree until we find the first culture variant entity (default to no cultures)
108111
var defaultCultures = entityContent.AncestorsOrSelf()?.FirstOrDefault(a => a.Cultures.Any())?.Cultures.Keys.ToArray()
109112
?? new[] { (string)null };
110-
foreach (var x in entityContent.DescendantsOrSelf(_variationContextAccessor))
113+
114+
foreach (IPublishedContent publishedContent in entityContent.DescendantsOrSelf(_variationContextAccessor))
111115
{
112116
// if this entity defines specific cultures, use those instead of the default ones
113-
var cultures = x.Cultures.Any() ? x.Cultures.Keys : defaultCultures;
117+
IEnumerable<string> cultures = publishedContent.Cultures.Any() ? publishedContent.Cultures.Keys : defaultCultures;
114118

115119
foreach (var culture in cultures)
116120
{
117-
var route = contentCache.GetRouteById(x.Id, culture);
121+
var checkedCulture = string.IsNullOrEmpty(culture) ? null : culture;
122+
var route = contentCache.GetRouteById(publishedContent.Id, checkedCulture);
118123
if (IsNotRoute(route))
124+
{
119125
continue;
120-
oldRoutes[new ContentIdAndCulture(x.Id, culture)] = new ContentKeyAndOldRoute(x.Key, route);
126+
}
127+
128+
oldRoutes[new ContentIdAndCulture(publishedContent.Id, culture)] = new ContentKeyAndOldRoute(publishedContent.Key, route);
121129
}
122130
}
123131
}
@@ -135,14 +143,18 @@ private void CreateRedirects(OldRoutesDictionary oldRoutes)
135143
{
136144
_logger.LogWarning("Could not track redirects because there is no current published snapshot available.");
137145
return;
138-
}
146+
}
139147

140-
foreach (var oldRoute in oldRoutes)
148+
foreach (KeyValuePair<ContentIdAndCulture, ContentKeyAndOldRoute> oldRoute in oldRoutes)
141149
{
142-
var newRoute = contentCache.GetRouteById(oldRoute.Key.ContentId, oldRoute.Key.Culture);
150+
var culture = string.IsNullOrWhiteSpace(oldRoute.Key.Culture) ? null : oldRoute.Key.Culture;
151+
var newRoute = contentCache.GetRouteById(oldRoute.Key.ContentId, culture);
143152
if (IsNotRoute(newRoute) || oldRoute.Value.OldRoute == newRoute)
153+
{
144154
continue;
145-
_redirectUrlService.Register(oldRoute.Value.OldRoute, oldRoute.Value.ContentKey, oldRoute.Key.Culture);
155+
}
156+
157+
_redirectUrlService.Register(oldRoute.Value.OldRoute, oldRoute.Value.ContentKey, culture);
146158
}
147159
}
148160

0 commit comments

Comments
 (0)