From 6de638cbae47bf986e14e0dc7831320cb947ff6c Mon Sep 17 00:00:00 2001 From: Mister Magoo Date: Fri, 27 Dec 2019 02:12:13 +0000 Subject: [PATCH] Swapped to a new method of detecting pre-rendering and included a service so you don't have to use the cascading value if you don't want to. Updated to aspnetcore 3.1.0 --- Directory.Build.props | 3 +- README.md | 42 ++++++++++++----- samples/PreRenderSample/Components/App.razor | 19 +++++--- .../Components/Pages/Counter.razor | 14 ++++-- .../Components/Pages/FetchData.razor | 17 +++++-- .../Components/Pages/Index.razor | 15 ++++-- .../{_ViewImports.cshtml => _Imports.razor} | 0 .../Components/Shared/NavMenu.razor | 4 +- .../{_ViewImports.cshtml => _Imports.razor} | 4 +- .../Pages/{Index.cshtml => _Host.cshtml} | 26 ++++++++-- samples/PreRenderSample/Pages/_Imports.razor | 1 + .../PreRenderSample/Pages/_ViewImports.cshtml | 3 -- .../PreRenderSample/PreRenderSample.csproj | 12 ++--- samples/PreRenderSample/Program.cs | 32 +++++-------- .../Properties/launchSettings.json | 2 +- .../Services/IWeatherForecastService.cs | 11 +++++ .../Services/WeatherForecast.cs | 14 +++--- .../Services/WeatherForecastService.cs | 34 +++++++------- samples/PreRenderSample/Startup.cs | 28 +++++------ src/PreRenderComponent/IPreRenderFlag.cs | 7 +++ src/PreRenderComponent/PreRenderCascade.cs | 47 ++----------------- .../PreRenderComponent.csproj | 16 +++---- src/PreRenderComponent/PreRenderFlag.cs | 36 ++++++++++++++ .../Properties/launchSettings.json | 27 ----------- 24 files changed, 221 insertions(+), 193 deletions(-) rename samples/PreRenderSample/Components/Pages/{_ViewImports.cshtml => _Imports.razor} (100%) rename samples/PreRenderSample/Components/{_ViewImports.cshtml => _Imports.razor} (62%) rename samples/PreRenderSample/Pages/{Index.cshtml => _Host.cshtml} (52%) create mode 100644 samples/PreRenderSample/Pages/_Imports.razor delete mode 100644 samples/PreRenderSample/Pages/_ViewImports.cshtml create mode 100644 samples/PreRenderSample/Services/IWeatherForecastService.cs create mode 100644 src/PreRenderComponent/IPreRenderFlag.cs create mode 100644 src/PreRenderComponent/PreRenderFlag.cs delete mode 100644 src/PreRenderComponent/Properties/launchSettings.json diff --git a/Directory.Build.props b/Directory.Build.props index d52bd6e..8608cb6 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,5 @@ - 0.9.0-preview3-19154-02 - 0.1.0-beta-1 + 0.2.0-beta-1 \ No newline at end of file diff --git a/README.md b/README.md index 83ecb5d..1f0cb95 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,49 @@ # PreRenderComponent -Provides a CascadingValue exposing whether the app is in PreRendering or not. +Provides a DI Service and/or a CascadingValue exposing whether the app is in PreRendering or not. ## Usage Install the nuget https://nuget.org/packages/PreRenderComponent -Add references to Components/_ViewImports.cshtml +### Add the service (Required) +Add the service to your startup Configure method +This component has a dependency on HttpContextAccessor, so also add that. -``` -@using PreRenderComponent -@addTagHelper *, PreRenderComponent +``` CSharp +services.AddHttpContextAccessor(); +services.AddScoped(); ``` +Consume the service wherever you need it (unless you want to use the Cascading Value component) +``` HTML +@inject PreRenderComponent.IPreRenderFlag PreRenderFlag +@if (PreRenderFlag.IsPreRendering) +{ +

Pre-Rendering

+} +``` +### Cascading Value (Optional) Wrap the Router component in PreRenderCascade in the App.razor file -``` - - - +``` HTML + + + + + + + +

Sorry, there's nothing at this address.

+
+
+
+
``` Consume the CascadingValue in your own pages/components -``` +``` CSharp @if (IsPreRendering) { @@ -34,7 +54,7 @@ else } -@functions { +@code { [CascadingParameter(Name = "PreRendering")] protected bool IsPreRendering { get; set; } } diff --git a/samples/PreRenderSample/Components/App.razor b/samples/PreRenderSample/Components/App.razor index b9e1aed..793d6bf 100644 --- a/samples/PreRenderSample/Components/App.razor +++ b/samples/PreRenderSample/Components/App.razor @@ -1,7 +1,12 @@ -@* - The Router component displays whichever component has a @page - directive matching the current URI. -*@ - - - \ No newline at end of file + + + + + + + +

Sorry, there's nothing at this address.

+
+
+
+
\ No newline at end of file diff --git a/samples/PreRenderSample/Components/Pages/Counter.razor b/samples/PreRenderSample/Components/Pages/Counter.razor index c20205c..d8f2b72 100644 --- a/samples/PreRenderSample/Components/Pages/Counter.razor +++ b/samples/PreRenderSample/Components/Pages/Counter.razor @@ -1,6 +1,12 @@ @page "/counter" -To see the Pre-Rendering in effect, open the browser dev tools, -set the network speed to slow, then refresh. + + To see the Pre-Rendering in effect, open the browser dev tools, + set the network speed to slow, then refresh. + +@if (IsPreRendering) +{ +

Pre-Rendering

+}

Counter

@@ -8,11 +14,11 @@ set the network speed to slow, then refresh. @if (IsPreRendering) { - + } else { - + } @functions { diff --git a/samples/PreRenderSample/Components/Pages/FetchData.razor b/samples/PreRenderSample/Components/Pages/FetchData.razor index ea48234..00132b7 100644 --- a/samples/PreRenderSample/Components/Pages/FetchData.razor +++ b/samples/PreRenderSample/Components/Pages/FetchData.razor @@ -1,6 +1,15 @@ @page "/fetchdata" @using PreRenderSample.Services -@inject WeatherForecastService ForecastService +@inject PreRenderComponent.IPreRenderFlag PreRenderFlag +@inject IWeatherForecastService ForecastService + + To see the Pre-Rendering in effect, open the browser dev tools, + set the network speed to slow, then refresh. + +@if (PreRenderFlag.IsPreRendering) +{ +

Pre-Rendering

+}

Weather forecast

@@ -34,11 +43,11 @@ else } - -@functions { +@code +{ WeatherForecast[] forecasts; - protected override async Task OnInitAsync() + protected override async Task OnInitializedAsync() { forecasts = await ForecastService.GetForecastAsync(DateTime.Now); } diff --git a/samples/PreRenderSample/Components/Pages/Index.razor b/samples/PreRenderSample/Components/Pages/Index.razor index c1bbe86..d6381c0 100644 --- a/samples/PreRenderSample/Components/Pages/Index.razor +++ b/samples/PreRenderSample/Components/Pages/Index.razor @@ -1,17 +1,24 @@ @page "/" + + To see the Pre-Rendering in effect, open the browser dev tools, + set the network speed to slow, then refresh. + Also check out the Counter page and Weather forecast for other examples. + +@if (IsPreRendering) +{ +

Pre-Rendering

+}

Hello, world!

Welcome to your new app. -To see the Pre-Rendering in effect, open the browser dev tools and set the network speed to slow, then refresh. -Also check out the Counter page for another example. @if (IsPreRendering) { -

Warming up the engine...

+

Warming up the engine...

} @functions { - [CascadingParameter(Name = "PreRendering")] protected bool IsPreRendering { get; set; } + [CascadingParameter(Name = "PreRendering")] protected bool IsPreRendering { get; set; } } diff --git a/samples/PreRenderSample/Components/Pages/_ViewImports.cshtml b/samples/PreRenderSample/Components/Pages/_Imports.razor similarity index 100% rename from samples/PreRenderSample/Components/Pages/_ViewImports.cshtml rename to samples/PreRenderSample/Components/Pages/_Imports.razor diff --git a/samples/PreRenderSample/Components/Shared/NavMenu.razor b/samples/PreRenderSample/Components/Shared/NavMenu.razor index f35aeee..3a0382c 100644 --- a/samples/PreRenderSample/Components/Shared/NavMenu.razor +++ b/samples/PreRenderSample/Components/Shared/NavMenu.razor @@ -1,11 +1,11 @@ -
+