-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PT-12422: Add spa routes to return correct http status (#650)
* Add spa routes to return correct http status * add two letters language support * fix code smell * feat: add support for JS RegExp patterns * Add content pages support for 200/404 * undo common controller * add workcontext * Fix naming * use workContextAccessor instead workContext --------- Co-authored-by: Artem Makarov <[email protected]> Co-authored-by: artem-dudarev <[email protected]>
- Loading branch information
1 parent
4fc743a
commit 1748185
Showing
8 changed files
with
217 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace VirtoCommerce.Storefront.Model | ||
{ | ||
public interface ISpaRouteService | ||
{ | ||
Task<bool> IsSpaRoute(string route); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using VirtoCommerce.Storefront.AutoRestClients.CoreModuleApi; | ||
using VirtoCommerce.Storefront.Common; | ||
using VirtoCommerce.Storefront.Model; | ||
using VirtoCommerce.Storefront.Model.Common; | ||
using VirtoCommerce.Storefront.Model.StaticContent; | ||
using VirtoCommerce.Storefront.Model.Stores; | ||
|
||
namespace VirtoCommerce.Storefront.Domain | ||
{ | ||
public class SeoInfoService : ISeoInfoService | ||
{ | ||
private readonly ICommerce _coreModuleApi; | ||
|
||
public SeoInfoService(ICommerce coreModuleApi) | ||
{ | ||
_coreModuleApi = coreModuleApi; | ||
} | ||
|
||
public async Task<SeoInfo[]> GetSeoInfosBySlug(string slug) | ||
{ | ||
var result = (await _coreModuleApi.GetSeoInfoBySlugAsync(slug)).Select(x => x.ToSeoInfo()).ToArray(); | ||
|
||
return result; | ||
} | ||
|
||
public async Task<SeoInfo[]> GetBestMatchingSeoInfos(string slug, Store store, string currentCulture) | ||
{ | ||
var result = (await _coreModuleApi.GetSeoInfoBySlugAsync(slug)).GetBestMatchingSeoInfos(store, currentCulture, slug).Select(x => x.ToSeoInfo()).ToArray(); | ||
|
||
return result; | ||
} | ||
|
||
public ContentItem GetContentItem(string slug, WorkContext context) | ||
{ | ||
ContentItem result = null; | ||
var pageUrl = slug == "__index__home__page__" ? "/" : $"/{slug}"; | ||
try | ||
{ | ||
var pages = context.Pages.Where(p => | ||
string.Equals(p.Url, pageUrl, StringComparison.OrdinalIgnoreCase) | ||
|| string.Equals(p.Url, slug, StringComparison.OrdinalIgnoreCase) | ||
); | ||
|
||
var page = pages.FirstOrDefault(x => x.Language.CultureName.EqualsInvariant(context.CurrentLanguage.CultureName)) | ||
?? pages.FirstOrDefault(x => x.Language.IsInvariant) | ||
?? pages.FirstOrDefault(x => x.AliasesUrls.Contains(pageUrl, StringComparer.OrdinalIgnoreCase)); | ||
result = page; | ||
|
||
} | ||
catch | ||
{ | ||
//do nothing | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using VirtoCommerce.Storefront.Model; | ||
using VirtoCommerce.Storefront.Model.Caching; | ||
using VirtoCommerce.Storefront.Model.Common; | ||
using VirtoCommerce.Storefront.Model.Common.Caching; | ||
using VirtoCommerce.Storefront.Model.StaticContent; | ||
|
||
namespace VirtoCommerce.Storefront.Domain | ||
{ | ||
public class SpaRouteService : ISpaRouteService | ||
{ | ||
private readonly IContentBlobProvider _contentBlobProvider; | ||
private readonly IStorefrontMemoryCache _memoryCache; | ||
private readonly IWorkContextAccessor _workContextAccessor; | ||
|
||
public SpaRouteService( | ||
IContentBlobProvider contentBlobProvider, | ||
IStorefrontMemoryCache memoryCache, | ||
IWorkContextAccessor workContextAccessor) | ||
{ | ||
_contentBlobProvider = contentBlobProvider; | ||
_memoryCache = memoryCache; | ||
_workContextAccessor = workContextAccessor; | ||
} | ||
|
||
public virtual async Task<bool> IsSpaRoute(string route) | ||
{ | ||
var workContext = _workContextAccessor.WorkContext; | ||
var cacheKey = CacheKey.With(GetType(), nameof(IsSpaRoute), workContext.CurrentStore.Id, route); | ||
var result = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (_) => | ||
{ | ||
var routes = await GetSpaRoutes(); | ||
|
||
var isSpaRoute = routes.Any(jsPattern => | ||
{ | ||
// Input sample: jsPattern = "/^\\/account\\/profile\\/?$/i" | ||
// Only the char "i" can be an ending. The others chars are not used | ||
// when generating RegExp patterns in the `routes.json` file. | ||
var options = jsPattern.EndsWith("i") ? RegexOptions.IgnoreCase : RegexOptions.None; | ||
var pattern = Regex.Replace(jsPattern, @"^\/|\/i?$", string.Empty); | ||
|
||
return Regex.IsMatch(route, pattern, options); | ||
}); | ||
|
||
return isSpaRoute; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
protected virtual async Task<List<string>> GetSpaRoutes() | ||
{ | ||
var workContext = _workContextAccessor.WorkContext; | ||
var cacheKey = CacheKey.With(GetType(), nameof(GetSpaRoutes), workContext.CurrentStore.Id); | ||
var routes = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (_) => | ||
{ | ||
var result = new List<string>(); | ||
var currentThemeName = !string.IsNullOrEmpty(workContext.CurrentStore.ThemeName) ? workContext.CurrentStore.ThemeName : "default"; | ||
var currentThemePath = Path.Combine("Themes", workContext.CurrentStore.Id, currentThemeName); | ||
var currentThemeSettingPath = Path.Combine(currentThemePath, "config", "routes.json"); | ||
|
||
if (_contentBlobProvider.PathExists(currentThemeSettingPath)) | ||
{ | ||
await using var stream = _contentBlobProvider.OpenRead(currentThemeSettingPath); | ||
result = JsonConvert.DeserializeObject<List<string>>(await stream.ReadToStringAsync()); | ||
} | ||
|
||
return result; | ||
}); | ||
|
||
return routes; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters