Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmoradi committed Oct 17, 2024
1 parent a418116 commit 7173f11
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public partial class AppComponentBase : ComponentBase, IAsyncDisposable

[AutoInject] protected JsonSerializerOptions JsonSerializerOptions = default!;

[Parameter] public string? culture { get; set; }

/// <summary>
/// <inheritdoc cref="IPrerenderStateService"/>
/// </summary>
Expand Down Expand Up @@ -97,6 +99,16 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
{
try
{
if (string.IsNullOrEmpty(culture) is false)
{
if (CultureInfoManager.MultilingualEnabled is false || CultureInfoManager.SupportedCultures.Any(sc => string.Equals(sc.Culture.Name, culture, StringComparison.InvariantCultureIgnoreCase)) is false)
{
// Because Blazor router doesn't support regex, the '/{culture?}/' captures some irrelevant routes
// such as non existing routes like /some-invalid-url, we need to make sure that the first segment of the route is a valid culture name.
NavigationManager.NavigateTo($"{Urls.NotFoundPage}?url={NavigationManager.ToBaseRelativePath(NavigationManager.Uri)}");
}
}

await OnAfterFirstRenderAsync();
}
catch (Exception exp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

PubSubService.Publish(PubSubMessages.PAGE_TITLE_CHANGED, (Title, Subtitle));
if (firstRender)
{
PubSubService.Publish(PubSubMessages.PAGE_TITLE_CHANGED, (Title, Subtitle));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@attribute [Route(Urls.HomePage)]
@attribute [Route("{culture?}" + Urls.HomePage)]
@attribute [Route("{culture:nonfile?}" + Urls.HomePage)]
@inherits AppPageBase

<PageTitle>@Localizer[nameof(AppStrings.HomeTitle)]</PageTitle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,5 @@ public partial class HomePage
protected override string? Title => Localizer[nameof(AppStrings.Home)];
protected override string? Subtitle => string.Empty;

[Parameter] public string? culture { get; set; }

[CascadingParameter] private BitDir? currentDir { get; set; }

protected async override Task OnAfterFirstRenderAsync()
{
if (string.IsNullOrEmpty(culture) is false)
{
if (CultureInfoManager.MultilingualEnabled is false || CultureInfoManager.SupportedCultures.Any(sc => string.Equals(sc.Culture.Name, culture, StringComparison.InvariantCultureIgnoreCase)) is false)
{
// Because Blazor router doesn't support regex, the HomePage.razor's route '/{culture?}/' captures some irrelevant routes
// such as non existing routes like /some-invalid-url, we need to make sure that the first segment of the route is a valid culture name.
NavigationManager.NavigateTo($"{Urls.NotFoundPage}?url={NavigationManager.ToBaseRelativePath(NavigationManager.Uri)}");
}

await base.OnAfterFirstRenderAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@*+:cnd:noEmit*@
@attribute [Route(Urls.ConfirmPage)]
@attribute [Route("{culture?}" + Urls.ConfirmPage)]
@inherits AppComponentBase
@inherits AppPageBase

<PageTitle>@Localizer[nameof(AppStrings.ConfirmTitle)]</PageTitle>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@*+:cnd:noEmit*@
@attribute [Route(Urls.ForgotPasswordPage)]
@attribute [Route("{culture?}" + Urls.ForgotPasswordPage)]
@inherits AppComponentBase
@inherits AppPageBase

<PageTitle>@Localizer[nameof(AppStrings.ForgotPassword)]</PageTitle>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@attribute [Route(Urls.ResetPasswordPage)]
@attribute [Route("{culture?}" + Urls.ResetPasswordPage)]
@inherits AppComponentBase
@inherits AppPageBase

<PageTitle>@Localizer[nameof(AppStrings.ResetPassword)]</PageTitle>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@*+:cnd:noEmit*@
@attribute [Route(Urls.SignInPage)]
@attribute [Route("{culture?}" + Urls.SignInPage)]
@inherits AppComponentBase
@inherits AppPageBase

<PageTitle>@Localizer[nameof(AppStrings.SignInTitle)]</PageTitle>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@*+:cnd:noEmit*@
@attribute [Route(Urls.SignUpPage)]
@attribute [Route("{culture?}" + Urls.SignUpPage)]
@inherits AppComponentBase
@inherits AppPageBase

<PageTitle>@Localizer[nameof(AppStrings.SingUpTitle)]</PageTitle>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,6 @@ public static void ConfiureMiddlewares(this WebApplication app)
app.UseRequestLocalization(options);
}

app.Use(async (context, next) =>
{
// Because Blazor router doesn't support regex, the HomePage.razor's route '/{culture?}/' captures some irrelevant routes
// such as /service-worker.js (root files of wwwroot) and some non existing routes like /some-invalid-url as well.
// We need to make sure that the first segment of the route is a valid culture name.

if (context.GetEndpoint() is RouteEndpoint routeEndpoint && routeEndpoint.RoutePattern?.RawText is "{culture?}/")
{
var culture = context.Request.RouteValues["culture"]?.ToString();

if (CultureInfoManager.SupportedCultures.Any(sc => sc.Culture.Name == culture) is false)
{
context.SetEndpoint(null);
}
}

await next.Invoke();
});

app.UseExceptionHandler("/", createScopeForErrors: true);

if (env.IsDevelopment())
Expand Down

0 comments on commit 7173f11

Please sign in to comment.