mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 16:11:10 +00:00
Falls back to the application layout when the active theme cannot be loaded
This commit is contained in:
1 parent
a09876525b
commit
170b3a0917
2 files changed
+57
-27
No files matched your search
@@ -2,6 +2,7 @@
|
|||||||
using Lumina.Presentation.Web.Common.DTO.Themes;
|
using Lumina.Presentation.Web.Common.DTO.Themes;
|
||||||
using Lumina.Presentation.Web.Common.Primitives;
|
using Lumina.Presentation.Web.Common.Primitives;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
#endregion
|
#endregion
|
||||||
@@ -49,31 +50,45 @@ public class ThemeLayoutRenderer
|
|||||||
/// <returns>An <see cref="Result{TValue}"/> containing either the full page HTML, or an error.</returns>
|
/// <returns>An <see cref="Result{TValue}"/> containing either the full page HTML, or an error.</returns>
|
||||||
public virtual async Task<Result<string>> RenderAsync(ThemeLayoutPageDto page, string? requestedThemeId = null, CancellationToken cancellationToken = default)
|
public virtual async Task<Result<string>> RenderAsync(ThemeLayoutPageDto page, string? requestedThemeId = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
ThemeRenderDocumentDto layoutDocument = await _themeService.GetRenderDocumentAsync(LAYOUT_PAGE_KEY, requestedThemeId, cancellationToken);
|
try
|
||||||
string themeId = layoutDocument.Theme.Id;
|
{
|
||||||
bool isAuthenticated = _httpContextAccessor.HttpContext?.User.Identity?.IsAuthenticated == true;
|
ThemeRenderDocumentDto layoutDocument = await _themeService.GetRenderDocumentAsync(LAYOUT_PAGE_KEY, requestedThemeId, cancellationToken);
|
||||||
|
string themeId = layoutDocument.Theme.Id;
|
||||||
|
bool isAuthenticated = _httpContextAccessor.HttpContext?.User.Identity?.IsAuthenticated == true;
|
||||||
|
|
||||||
string appHead = await _viewRenderer.RenderPartialAsync("_ThemeLayoutHead", cancellationToken);
|
string appHead = await _viewRenderer.RenderPartialAsync("_ThemeLayoutHead", cancellationToken);
|
||||||
string nav = await RenderNavAsync(themeId, cancellationToken);
|
string nav = await RenderNavAsync(themeId, cancellationToken);
|
||||||
string audioPlayer = isAuthenticated ? await _viewRenderer.RenderPartialAsync("_AudioPlayer", cancellationToken) : string.Empty;
|
string audioPlayer = isAuthenticated ? await _viewRenderer.RenderPartialAsync("_AudioPlayer", cancellationToken) : string.Empty;
|
||||||
string appScripts = await _viewRenderer.RenderPartialAsync("_ThemeLayoutScripts", cancellationToken);
|
string appScripts = await _viewRenderer.RenderPartialAsync("_ThemeLayoutScripts", cancellationToken);
|
||||||
|
|
||||||
ThemeLayoutModelDto layoutModel = new(
|
ThemeLayoutModelDto layoutModel = new(
|
||||||
page.Title,
|
page.Title,
|
||||||
$"/theme-assets/{themeId}/assets",
|
$"/theme-assets/{themeId}/assets",
|
||||||
appHead,
|
appHead,
|
||||||
nav,
|
nav,
|
||||||
page.Content,
|
page.Content,
|
||||||
audioPlayer,
|
audioPlayer,
|
||||||
appScripts,
|
appScripts,
|
||||||
page.Script,
|
page.Script,
|
||||||
isAuthenticated ? string.Empty : "bottom: 0px;");
|
isAuthenticated ? string.Empty : "bottom: 0px;");
|
||||||
|
|
||||||
Result<ThemePageRenderResultDto> renderResult = _templateEngine.RenderPage(layoutDocument.Template, layoutModel);
|
Result<ThemePageRenderResultDto> renderResult = _templateEngine.RenderPage(layoutDocument.Template, layoutModel);
|
||||||
if (renderResult.IsFailure)
|
if (renderResult.IsFailure)
|
||||||
return renderResult.Errors;
|
return renderResult.Errors;
|
||||||
|
|
||||||
return renderResult.Value.Content;
|
return renderResult.Value.Content;
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// the themed shell is best effort: when the active theme cannot be loaded, the application layout renders the
|
||||||
|
// page instead. The exception must not propagate, otherwise the exception handling middleware turns the page
|
||||||
|
// into a JSON error instead of the fallback layout, so any page breaks whenever the theme API is unavailable.
|
||||||
|
return Error.NotFound(code: "Theme.Template.Unavailable", description: "The active theme could not be loaded.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using Lumina.Presentation.Web.Common.DTO.Themes;
|
using Lumina.Presentation.Web.Common.DTO.Themes;
|
||||||
using Lumina.Presentation.Web.Common.Primitives;
|
using Lumina.Presentation.Web.Common.Primitives;
|
||||||
using Lumina.Presentation.Web.Common.Services;
|
using Lumina.Presentation.Web.Common.Services;
|
||||||
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
#endregion
|
#endregion
|
||||||
@@ -36,13 +37,27 @@ public class ThemePageRenderer
|
|||||||
/// <returns>An <see cref="Result{TValue}"/> containing either the rendered page, or an error.</returns>
|
/// <returns>An <see cref="Result{TValue}"/> containing either the rendered page, or an error.</returns>
|
||||||
public virtual async Task<Result<ThemePageRenderResultDto>> RenderAsync(ThemePageDto model, string? requestedThemeId = null, CancellationToken cancellationToken = default)
|
public virtual async Task<Result<ThemePageRenderResultDto>> RenderAsync(ThemePageDto model, string? requestedThemeId = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
ThemeRenderDocumentDto document = await _themeService.GetRenderDocumentAsync(model.PageKey, requestedThemeId, cancellationToken);
|
try
|
||||||
|
{
|
||||||
|
ThemeRenderDocumentDto document = await _themeService.GetRenderDocumentAsync(model.PageKey, requestedThemeId, cancellationToken);
|
||||||
|
|
||||||
model.ThemeId = document.Theme.Id;
|
model.ThemeId = document.Theme.Id;
|
||||||
model.AssetBase = $"/theme-assets/{document.Theme.Id}/assets";
|
model.AssetBase = $"/theme-assets/{document.Theme.Id}/assets";
|
||||||
// a fresh script id per render lets the AJAX navigator unload this view's script when navigating away
|
// a fresh script id per render lets the AJAX navigator unload this view's script when navigating away
|
||||||
model.ScriptId = ScriptIdentifierHelper.GenerateScriptId();
|
model.ScriptId = ScriptIdentifierHelper.GenerateScriptId();
|
||||||
|
|
||||||
return _templateEngine.RenderPage(document.Template, model);
|
return _templateEngine.RenderPage(document.Template, model);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// themed rendering is best effort: when the active theme cannot be loaded, the caller falls back to the Razor
|
||||||
|
// view. The exception must not propagate, otherwise the exception handling middleware turns the page into a
|
||||||
|
// JSON error instead of the Razor fallback, so any themed page breaks whenever the theme API is unavailable.
|
||||||
|
return Error.NotFound(code: "Theme.Template.Unavailable", description: "The active theme could not be loaded.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user