Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed url redirect creation issue #3837

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 13 additions & 19 deletions src/Umbraco.Web/Routing/RedirectTrackingEventHandler.cs
Expand Up @@ -94,14 +94,14 @@ protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplica
/// <summary>
/// Tracks a documents URLs during publishing in the current request
/// </summary>
private static Dictionary<int, Tuple<Guid, string>> OldRoutes
private static Dictionary<int, Tuple<Guid, string>> OldUrls
{
get
{
var oldRoutes = RequestCache.GetCacheItem<Dictionary<int, Tuple<Guid, string>>>(
var oldUrls = RequestCache.GetCacheItem<Dictionary<int, Tuple<Guid, string>>>(
ContextKey3,
() => new Dictionary<int, Tuple<Guid, string>>());
return oldRoutes;
return oldUrls;
}
}

Expand Down Expand Up @@ -190,14 +190,13 @@ private static void ContentService_Publishing(IPublishingStrategy sender, Publis
// else save routes for all descendants
var entityContent = contentCache.GetById(entity.Id);
if (entityContent == null) continue;

foreach (var x in entityContent.DescendantsOrSelf())
{
var route = contentCache.GetRouteById(x.Id);
if (IsNotRoute(route)) continue;
var wk = UnwrapToKey(x);
if (wk == null) continue;

OldRoutes[x.Id] = Tuple.Create(wk.Key, route);
OldUrls[x.Id] = Tuple.Create(wk.Key, x.Url);
}
}

Expand Down Expand Up @@ -244,21 +243,21 @@ private void PageCacheRefresher_CacheUpdated(PageCacheRefresher sender, CacheRef
// only on master / single, not on replicas!
if (IsReplicaServer) return;

// simply getting OldRoutes will register it in the request cache,
// simply getting oldUrls will register it in the request cache,
// so whatever we do with it, try/finally it to ensure it's cleared

try
{
foreach (var oldRoute in OldRoutes)
foreach (var oldUrl in OldUrls)
{
// assuming we cannot have 'CacheUpdated' for only part of the infos else we'd need
// to set a flag in 'Published' to indicate which entities have been refreshed ok
CreateRedirect(oldRoute.Key, oldRoute.Value.Item1, oldRoute.Value.Item2);
CreateRedirect(oldUrl.Key, oldUrl.Value.Item1, oldUrl.Value.Item2);
}
}
finally
{
OldRoutes.Clear();
OldUrls.Clear();
RequestCache.ClearCacheItem(ContextKey3);
}
}
Expand All @@ -281,23 +280,18 @@ private static void ContentService_Moved(IContentService sender, MoveEventArgs<I
LockedEvents = false;
}

private static void CreateRedirect(int contentId, Guid contentKey, string oldRoute)
private static void CreateRedirect(int contentId, Guid contentKey, string oldUrl)
{

var contentCache = GetPublishedCache();
if (contentCache == null) return;

var newRoute = contentCache.GetRouteById(contentId);
if (IsNotRoute(newRoute) || oldRoute == newRoute) return;
var newUrl = contentCache.GetById(contentId)?.Url;
if (newUrl == null || oldUrl == newUrl) return;
var redirectUrlService = ApplicationContext.Current.Services.RedirectUrlService;
redirectUrlService.Register(oldRoute, contentKey);
redirectUrlService.Register(oldUrl, contentKey);
}

private static bool IsNotRoute(string route)
{
// null if content not found
return route == null;
}

// gets a value indicating whether server is 'replica'
private static bool IsReplicaServer
Expand Down