Skip to content

HTMX: Force a page reload if the version of requested page differs #1438

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Elastic.ApiExplorer/ApiViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public HtmlString RenderMarkdown(string? markdown) =>
public GlobalLayoutViewModel CreateGlobalLayoutModel() =>
new()
{
DocsBuilderVersion = "0.0.0", // TODO: Use a version from the docs-builder
DocSetName = "Api Explorer",
Description = "",
CurrentNavigationItem = CurrentNavigationItem,
Expand Down
12 changes: 12 additions & 0 deletions src/Elastic.Documentation.Site/Assets/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ document.body.addEventListener('htmx:responseError', function (event) {
window.location.assign(event.detail.pathInfo.requestPath)
}
})

// The body now has a data-docs-builder-version attribute, which is the version of the docs-builder
// used to generate this page.
// If there is a new version, we want to refresh the page to prevent broken layouts.
const currentDocsBuilderVersion = $('body').dataset.docsBuilderVersion
document.body.addEventListener('htmx:afterRequest', function (event) {
const targetDocsBuilderVersion =
event.detail.target.dataset.docsBuilderVersion
if (currentDocsBuilderVersion !== targetDocsBuilderVersion) {
window.location = event.detail.pathInfo.finalRequestPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we protect setting the location to itself?

In developer tools setting window.location to the current url causes a refresh.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asking cause I'm worried about a refresh loop.

should we protect against a redirect loop too?

}
})
1 change: 1 addition & 0 deletions src/Elastic.Documentation.Site/_GlobalLayout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<body
class="group/body text-ink has-[#primary-nav-hamburger:checked]:overflow-hidden"
hx-ext="preload, head-support"
data-docs-builder-version="@Model.DocsBuilderVersion"
data-root-path="@Model.Link("/")">
@if (Model.GoogleTagManager.Enabled)
{
Expand Down
1 change: 1 addition & 0 deletions src/Elastic.Documentation.Site/_ViewModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class GlobalSections

public record GlobalLayoutViewModel
{
public required string DocsBuilderVersion { get; init; }
public required string DocSetName { get; init; }
public string Title { get; set; } = "Elastic Documentation";
public required string Description { get; init; }
Expand Down
5 changes: 4 additions & 1 deletion src/Elastic.Markdown/HtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information

using System.IO.Abstractions;
using System.Reflection;
using Elastic.Documentation;
using Elastic.Documentation.Configuration.Builder;
using Elastic.Documentation.Legacy;
Expand Down Expand Up @@ -129,7 +130,9 @@ private async Task<string> RenderLayout(MarkdownFile markdown, MarkdownDocument
AllVersionsUrl = allVersionsUrl,
LegacyPages = legacyPages?.Skip(1).ToArray(),
VersionDropdownItems = VersionDrownDownItemViewModel.FromLegacyPageMappings(legacyPages?.Skip(1).ToArray()),
Products = allProducts
Products = allProducts,
DocsBuilderVersion = Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyInformationalVersionAttribute>()
.FirstOrDefault()?.InformationalVersion ?? "0.0.0"
});
return await slice.RenderAsync(cancellationToken: ctx);
}
Expand Down
1 change: 1 addition & 0 deletions src/Elastic.Markdown/Page/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@functions {
public MarkdownLayoutViewModel LayoutModel => new()
{
DocsBuilderVersion = Model.DocsBuilderVersion,
Layout = Model.CurrentDocument.YamlFrontMatter?.Layout,
RenderHamburgerIcon = Model.CurrentDocument.YamlFrontMatter?.Layout != MarkdownPageLayout.LandingPage,
DocSetName = Model.DocSetName,
Expand Down
7 changes: 1 addition & 6 deletions src/Elastic.Markdown/Page/IndexViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ public class IndexViewModel
public required DocumentationGroup Tree { get; init; }
public required IReadOnlyCollection<PageTocItem> PageTocItems { get; init; }
public required MarkdownFile CurrentDocument { get; init; }

public required INavigationItem CurrentNavigationItem { get; init; }
public required INavigationItem? PreviousDocument { get; init; }
public required INavigationItem? NextDocument { get; init; }
public required INavigationItem[] Parents { get; init; }

public required string NavigationHtml { get; init; }
public required string CurrentVersion { get; init; }

public required string? AllVersionsUrl { get; init; }
public required LegacyPageMapping[]? LegacyPages { get; init; }
public required VersionDrownDownItemViewModel[]? VersionDropdownItems { get; init; }
Expand All @@ -43,13 +40,11 @@ public class IndexViewModel
public required ApplicableTo? AppliesTo { get; init; }
public required bool AllowIndexing { get; init; }
public required Uri? CanonicalBaseUrl { get; init; }

public required GoogleTagManagerConfiguration GoogleTagManager { get; init; }

public required FeatureFlags Features { get; init; }
public required StaticFileContentHashProvider StaticFileContentHashProvider { get; init; }

public required HashSet<Product> Products { get; init; }
public required string DocsBuilderVersion { get; init; }
}

public class VersionDrownDownItemViewModel
Expand Down
Loading