|
13 | 13 | using Nop.Core.Domain.Localization; |
14 | 14 | using Nop.Core.Domain.News; |
15 | 15 | using Nop.Core.Domain.Seo; |
16 | | -using Nop.Core.Domain.Stores; |
17 | 16 | using Nop.Core.Domain.Topics; |
18 | 17 | using Nop.Core.Events; |
19 | | -using Nop.Core.Http; |
20 | 18 | using Nop.Core.Infrastructure; |
21 | 19 | using Nop.Services.Blogs; |
22 | 20 | using Nop.Services.Catalog; |
@@ -524,7 +522,7 @@ protected virtual async Task WriteSitemapUrlAsync(XmlWriter writer, SitemapUrlMo |
524 | 522 | /// <param name="fullPath">The path and name of the sitemap file</param> |
525 | 523 | /// <param name="id">Sitemap identifier</param> |
526 | 524 | /// <returns>A task that represents the asynchronous operation</returns> |
527 | | - protected virtual async Task GenerateAsync(string fullPath, int id = 0) |
| 525 | + protected virtual async Task GenerateAsync(string fullPath, int id = 0) |
528 | 526 | { |
529 | 527 | //generate all URLs for the sitemap |
530 | 528 | var sitemapUrls = await GenerateUrlsAsync(); |
@@ -605,22 +603,6 @@ protected string GetLocalizedUrl(string currentUrl, Language lang) |
605 | 603 | return new Uri(new Uri(scheme), localizedPath).ToString(); |
606 | 604 | } |
607 | 605 |
|
608 | | - /// <summary> |
609 | | - /// Retrieves the list of languages for the given store that are not excluded in the sitemap XML settings, |
610 | | - /// if SEO-friendly URLs for languages are enabled. |
611 | | - /// </summary> |
612 | | - /// <param name="store">The store to retrieve allowed languages for.</param> |
613 | | - /// <returns> |
614 | | - /// A list of <see cref="Language"/> if SEO-friendly URLs are enabled; otherwise, <c>null</c>. |
615 | | - /// </returns> |
616 | | - protected async Task<List<Language>> GetAllowedLanguagesAsync(Store store) |
617 | | - { |
618 | | - return _localizationSettings.SeoFriendlyUrlsForLanguagesEnabled |
619 | | - ? (await _languageService.GetAllLanguagesAsync(storeId: store.Id)) |
620 | | - .Where(lang => !_sitemapXmlSettings.DisallowLanguages.Contains(lang.Id)) |
621 | | - .ToList() |
622 | | - : null; |
623 | | - } |
624 | 606 | #endregion |
625 | 607 |
|
626 | 608 | #region Methods |
@@ -841,43 +823,27 @@ public virtual async Task<SitemapModel> PrepareSitemapModelAsync(SitemapPageMode |
841 | 823 | /// </returns> |
842 | 824 | public virtual async Task<SitemapXmlModel> PrepareSitemapXmlModelAsync(int id = 0) |
843 | 825 | { |
844 | | - var workingLanguage = await _workContext.GetWorkingLanguageAsync(); |
| 826 | + var language = await _workContext.GetWorkingLanguageAsync(); |
845 | 827 | var store = await _storeContext.GetCurrentStoreAsync(); |
846 | | - |
847 | | - // get list of allowed languages (null if multilingual URLs are disabled) |
848 | | - var languages = await GetAllowedLanguagesAsync(store); |
849 | 828 |
|
850 | | - // select current language if allowed, fallback to first allowed if needed |
851 | | - var language = languages?.FirstOrDefault(lang => lang.Id == workingLanguage?.Id) ?? languages?.FirstOrDefault() ?? workingLanguage; |
| 829 | + var fileName = string.Format(NopSeoDefaults.SitemapXmlFilePattern, store.Id, language.Id, id); |
| 830 | + var fullPath = _nopFileProvider.GetAbsolutePath(NopSeoDefaults.SitemapXmlDirectory, fileName); |
852 | 831 |
|
853 | | - if (language.Id != workingLanguage.Id) |
854 | | - _actionContextAccessor.ActionContext.HttpContext.Items[NopHttpDefaults.ForcedSitemapXmlLanguage] = language.UniqueSeoCode.ToLowerInvariant(); |
855 | | - |
856 | | - try |
| 832 | + if (_nopFileProvider.FileExists(fullPath) && _nopFileProvider.GetLastWriteTimeUtc(fullPath) > DateTime.UtcNow.AddHours(-_sitemapXmlSettings.RebuildSitemapXmlAfterHours)) |
857 | 833 | { |
858 | | - var fileName = string.Format(NopSeoDefaults.SitemapXmlFilePattern, store.Id, language.Id, id); |
859 | | - var fullPath = _nopFileProvider.GetAbsolutePath(NopSeoDefaults.SitemapXmlDirectory, fileName); |
860 | | - |
861 | | - if (_nopFileProvider.FileExists(fullPath) && _nopFileProvider.GetLastWriteTimeUtc(fullPath) > DateTime.UtcNow.AddHours(-_sitemapXmlSettings.RebuildSitemapXmlAfterHours)) |
862 | | - { |
863 | | - return new SitemapXmlModel { SitemapXmlPath = fullPath }; |
864 | | - } |
865 | | - |
866 | | - //execute task with lock |
867 | | - if (!await _locker.PerformActionWithLockAsync( |
868 | | - fullPath, |
869 | | - TimeSpan.FromSeconds(_sitemapXmlSettings.SitemapBuildOperationDelay), |
870 | | - async () => await GenerateAsync(fullPath, id))) |
871 | | - { |
872 | | - throw new InvalidOperationException(); |
873 | | - } |
874 | | - |
875 | 834 | return new SitemapXmlModel { SitemapXmlPath = fullPath }; |
876 | 835 | } |
877 | | - finally |
| 836 | + |
| 837 | + //execute task with lock |
| 838 | + if (!await _locker.PerformActionWithLockAsync( |
| 839 | + fullPath, |
| 840 | + TimeSpan.FromSeconds(_sitemapXmlSettings.SitemapBuildOperationDelay), |
| 841 | + async () => await GenerateAsync(fullPath, id))) |
878 | 842 | { |
879 | | - _actionContextAccessor.ActionContext.HttpContext.Items.Remove(NopHttpDefaults.ForcedSitemapXmlLanguage); |
| 843 | + throw new InvalidOperationException(); |
880 | 844 | } |
| 845 | + |
| 846 | + return new SitemapXmlModel { SitemapXmlPath = fullPath }; |
881 | 847 | } |
882 | 848 |
|
883 | 849 | /// <summary> |
@@ -920,8 +886,18 @@ var name when name.Equals(nameof(ProductTag), StringComparison.InvariantCultureI |
920 | 886 | var store = await _storeContext.GetCurrentStoreAsync(); |
921 | 887 |
|
922 | 888 | var updatedOn = dateTimeUpdatedOn ?? DateTime.UtcNow; |
923 | | - var languages = await GetAllowedLanguagesAsync(store); |
| 889 | + var languages = _localizationSettings.SeoFriendlyUrlsForLanguagesEnabled |
| 890 | + ? (await _languageService.GetAllLanguagesAsync(storeId: store.Id)) |
| 891 | + .Where(lang => !_sitemapXmlSettings.DisallowLanguages.Contains(lang.Id)).ToList() |
| 892 | + : null; |
924 | 893 |
|
| 894 | + // select store default language if allowed, fallback to first allowed if needed |
| 895 | + var workingLanguage = await _workContext.GetWorkingLanguageAsync(); |
| 896 | + var language = languages?.FirstOrDefault(lang => lang.Id == store.DefaultLanguageId) ?? languages?.FirstOrDefault() ?? workingLanguage; |
| 897 | + |
| 898 | + if (language.Id != workingLanguage.Id) |
| 899 | + url = GetLocalizedUrl(url, language); |
| 900 | + |
925 | 901 | if (languages == null || languages.Count == 1) |
926 | 902 | return new SitemapUrlModel(url, new List<string>(), updateFreq, updatedOn); |
927 | 903 |
|
|
0 commit comments