Skip to content

Commit 41dc0f2

Browse files
committed
cache url
1 parent c2700a0 commit 41dc0f2

File tree

7 files changed

+23
-22
lines changed

7 files changed

+23
-22
lines changed

src/Elastic.Documentation.Navigation/Assembler/AssembledNavigation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ void IAssignableChildrenNavigation.SetNavigationItems(IReadOnlyCollection<INavig
209209
// Set the navigation index
210210
node.Parent = parent;
211211
node.NavigationIndex = index;
212-
homeAccessor.HomeProvider = new NavigationHomeProvider(pathPrefix, root, true);
212+
homeAccessor.HomeProvider = new NavigationHomeProvider(pathPrefix, root);
213213

214214
//var wrapped = new SiteTableOfContentsNavigation<IDocumentationFile>(node, homeAccessor.HomeProvider, parent, root);
215215
//parent = wrapped;

src/Elastic.Documentation.Navigation/Isolated/DocumentationSetNavigation.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ public DocumentationSetNavigation(
9696
/// </summary>
9797
public string PathPrefix => HomeProvider == this ? _pathPrefix : HomeProvider.PathPrefix;
9898

99-
bool INavigationHomeProvider.RelativeToTableOfContents => false;
100-
10199
public INavigationHomeProvider HomeProvider { get; set; }
102100

103101
public GitCheckoutInformation Git { get; }
@@ -379,7 +377,7 @@ int depth
379377
context.ReadFileSystem.Path.Combine(context.DocumentationSourceDirectory.FullName, fullTocPath)
380378
);
381379

382-
var isolatedHomeProvider = new NavigationHomeProvider(homeAccessor.HomeProvider.PathPrefix, homeAccessor.HomeProvider.NavigationRoot, homeAccessor.HomeProvider.RelativeToTableOfContents);
380+
var isolatedHomeProvider = new NavigationHomeProvider(homeAccessor.HomeProvider.PathPrefix, homeAccessor.HomeProvider.NavigationRoot);
383381

384382
// Create the TOC navigation with empty children initially
385383
// We use null parent temporarily - we'll set it properly at the end using the public setter

src/Elastic.Documentation.Navigation/Isolated/FileNavigationLeaf.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ public class FileNavigationLeaf<TModel>(TModel model, IFileInfo fileInfo, FileNa
2424
public IFileInfo FileInfo { get; } = fileInfo;
2525

2626
/// <inheritdoc />
27-
public TModel Model { get; init; } = model;
27+
public TModel Model { get; } = model;
28+
29+
private string? _homeProviderCache;
30+
private string? _urlCache;
2831

2932
/// <inheritdoc />
3033
public string Url
3134
{
3235
get
3336
{
37+
if (_homeProviderCache is not null && _homeProviderCache == args.HomeAccessor.HomeProvider.Id && _urlCache is not null)
38+
return _urlCache;
39+
3440
var rootUrl = args.HomeAccessor.HomeProvider.PathPrefix.TrimEnd('/');
3541
var relativeToContainer = args.HomeAccessor.HomeProvider.NavigationRoot.Parent is SiteNavigation;
3642

@@ -49,12 +55,15 @@ public string Url
4955
if (string.IsNullOrEmpty(path))
5056
return string.IsNullOrEmpty(rootUrl) ? "/" : rootUrl;
5157

52-
return $"{rootUrl}/{path}";
58+
_homeProviderCache = args.HomeAccessor.HomeProvider.Id;
59+
_urlCache = $"{rootUrl}/{path}";
60+
61+
return _urlCache;
5362
}
5463
}
5564

5665
/// <inheritdoc />
57-
public bool Hidden { get; init; } = args.Hidden;
66+
public bool Hidden { get; } = args.Hidden;
5867

5968
/// <inheritdoc />
6069
public IRootNavigationItem<INavigationModel, INavigationItem> NavigationRoot => args.HomeAccessor.HomeProvider.NavigationRoot;

src/Elastic.Documentation.Navigation/Isolated/IPathPrefixProvider.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface INavigationHomeProvider
1010
{
1111
string PathPrefix { get; }
1212
IRootNavigationItem<INavigationModel, INavigationItem> NavigationRoot { get; }
13-
bool RelativeToTableOfContents { get; }
13+
string Id { get; }
1414
}
1515

1616
public interface INavigationHomeAccessor
@@ -19,20 +19,16 @@ public interface INavigationHomeAccessor
1919
}
2020

2121
[DebuggerDisplay("{PathPrefix} => {NavigationRoot.Url}")]
22-
public class NavigationHomeProvider(
23-
string pathPrefix,
24-
IRootNavigationItem<INavigationModel, INavigationItem> navigationRoot,
25-
bool relativeToTableOfContents
26-
) : INavigationHomeProvider
22+
public class NavigationHomeProvider(string pathPrefix, IRootNavigationItem<INavigationModel, INavigationItem> navigationRoot) : INavigationHomeProvider
2723
{
2824
/// <inheritdoc />
2925
public string PathPrefix { get; } = pathPrefix;
3026

31-
public bool RelativeToTableOfContents { get; } = relativeToTableOfContents;
32-
3327
/// <inheritdoc />
3428
public IRootNavigationItem<INavigationModel, INavigationItem> NavigationRoot { get; } = navigationRoot;
3529

30+
public string Id { get; } = Guid.NewGuid().ToString("N");
31+
3632
public override string ToString() => $"{PathPrefix} => {NavigationRoot.Url}";
3733
}
3834

src/Elastic.Documentation.Navigation/Isolated/TableOfContentsNavigation.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ INavigationHomeProvider homeProvider
6060
/// </summary>
6161
public string PathPrefix { get; }
6262

63-
bool INavigationHomeProvider.RelativeToTableOfContents => false;
64-
6563
/// <inheritdoc />
6664
public string Url => Index.Url;
6765

tests/Navigation.Tests/Isolation/DynamicUrlTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public void DynamicUrlUpdatesWhenRootUrlChanges()
3737
file.Url.Should().Be("/setup/install");
3838

3939
// Change root URL
40-
navigation.HomeProvider = new NavigationHomeProvider("/v8.0", navigation.NavigationRoot, false);
40+
navigation.HomeProvider = new NavigationHomeProvider("/v8.0", navigation.NavigationRoot);
4141

4242
// URLs should update dynamically
4343
// Since folder has no index child, its URL is the first child's URL
4444
folder.Url.Should().Be("/v8.0/setup/install");
4545
file.Url.Should().Be("/v8.0/setup/install");
4646

4747
// Change root URL
48-
navigation.HomeProvider = new NavigationHomeProvider("/v9.0", navigation.NavigationRoot, false);
48+
navigation.HomeProvider = new NavigationHomeProvider("/v9.0", navigation.NavigationRoot);
4949

5050
// URLs should update dynamically
5151
// Since folder has no index child, its URL is the first child's URL
@@ -80,7 +80,7 @@ public void UrlRootPropagatesCorrectlyThroughFolders()
8080
file.Url.Should().Be("/outer/inner/deep");
8181

8282
// Change root URL
83-
navigation.HomeProvider = new NavigationHomeProvider("/base", navigation.NavigationRoot, false);
83+
navigation.HomeProvider = new NavigationHomeProvider("/base", navigation.NavigationRoot);
8484

8585
file.Url.Should().Be("/base/outer/inner/deep");
8686
}
@@ -277,7 +277,7 @@ public void UrlRootChangesForTableOfContentsNavigation()
277277
file.Url.Should().Be("/guides/api/reference");
278278

279279
// Change root URL
280-
navigation.HomeProvider = new NavigationHomeProvider("/v2", navigation.NavigationRoot, false);
280+
navigation.HomeProvider = new NavigationHomeProvider("/v2", navigation.NavigationRoot);
281281

282282
// Both TOC and file URLs should update
283283
toc.Url.Should().Be("/v2/guides/api/reference");

tests/Navigation.Tests/Isolation/FileNavigationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void FileNavigationUrlUpdatesWhenRootChanges()
164164
child.Url.Should().Be("/section1");
165165

166166
// Change root URL
167-
navigation.HomeProvider = new NavigationHomeProvider("/v2", navigation.NavigationRoot, false);
167+
navigation.HomeProvider = new NavigationHomeProvider("/v2", navigation.NavigationRoot);
168168

169169
// URLs should update dynamically
170170
fileNav.Url.Should().Be("/v2/guide");

0 commit comments

Comments
 (0)