mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 07:01: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.Primitives;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -49,31 +50,45 @@ public class ThemeLayoutRenderer
|
||||
/// <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)
|
||||
{
|
||||
ThemeRenderDocumentDto layoutDocument = await _themeService.GetRenderDocumentAsync(LAYOUT_PAGE_KEY, requestedThemeId, cancellationToken);
|
||||
string themeId = layoutDocument.Theme.Id;
|
||||
bool isAuthenticated = _httpContextAccessor.HttpContext?.User.Identity?.IsAuthenticated == true;
|
||||
try
|
||||
{
|
||||
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 nav = await RenderNavAsync(themeId, cancellationToken);
|
||||
string audioPlayer = isAuthenticated ? await _viewRenderer.RenderPartialAsync("_AudioPlayer", cancellationToken) : string.Empty;
|
||||
string appScripts = await _viewRenderer.RenderPartialAsync("_ThemeLayoutScripts", cancellationToken);
|
||||
string appHead = await _viewRenderer.RenderPartialAsync("_ThemeLayoutHead", cancellationToken);
|
||||
string nav = await RenderNavAsync(themeId, cancellationToken);
|
||||
string audioPlayer = isAuthenticated ? await _viewRenderer.RenderPartialAsync("_AudioPlayer", cancellationToken) : string.Empty;
|
||||
string appScripts = await _viewRenderer.RenderPartialAsync("_ThemeLayoutScripts", cancellationToken);
|
||||
|
||||
ThemeLayoutModelDto layoutModel = new(
|
||||
page.Title,
|
||||
$"/theme-assets/{themeId}/assets",
|
||||
appHead,
|
||||
nav,
|
||||
page.Content,
|
||||
audioPlayer,
|
||||
appScripts,
|
||||
page.Script,
|
||||
isAuthenticated ? string.Empty : "bottom: 0px;");
|
||||
ThemeLayoutModelDto layoutModel = new(
|
||||
page.Title,
|
||||
$"/theme-assets/{themeId}/assets",
|
||||
appHead,
|
||||
nav,
|
||||
page.Content,
|
||||
audioPlayer,
|
||||
appScripts,
|
||||
page.Script,
|
||||
isAuthenticated ? string.Empty : "bottom: 0px;");
|
||||
|
||||
Result<ThemePageRenderResultDto> renderResult = _templateEngine.RenderPage(layoutDocument.Template, layoutModel);
|
||||
if (renderResult.IsFailure)
|
||||
return renderResult.Errors;
|
||||
Result<ThemePageRenderResultDto> renderResult = _templateEngine.RenderPage(layoutDocument.Template, layoutModel);
|
||||
if (renderResult.IsFailure)
|
||||
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>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Lumina.Presentation.Web.Common.DTO.Themes;
|
||||
using Lumina.Presentation.Web.Common.Primitives;
|
||||
using Lumina.Presentation.Web.Common.Services;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -36,13 +37,27 @@ public class ThemePageRenderer
|
||||
/// <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)
|
||||
{
|
||||
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.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
|
||||
model.ScriptId = ScriptIdentifierHelper.GenerateScriptId();
|
||||
model.ThemeId = document.Theme.Id;
|
||||
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
|
||||
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