Skip to content

Commit

Permalink
Add more support for dotnet 6
Browse files Browse the repository at this point in the history
  • Loading branch information
albertwoo committed Mar 14, 2024
1 parent 8df7a66 commit b84b88f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Fun.Blazor.Server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## [4.0.5] - 2024-03-14

- Support MapRazorComponentsForSSR for dotnet6.0

## [4.0.4] - 2024-03-12

- Upgrade dependencies
Expand Down
4 changes: 0 additions & 4 deletions Fun.Blazor.Server/CacheControl.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Fun.Blazor

#if !NET6_0

open System

/// This can be used for any blazor component which is served by MapRazorComponentsForSSR or MapCustomElementsForSSR
Expand All @@ -11,5 +9,3 @@ type ComponentResponseCacheAttribute() =
member val Vary = "" with get, set
member val Pragma = "" with get, set
member val CacheControl = "" with get, set

#endif
45 changes: 35 additions & 10 deletions Fun.Blazor.Server/DIExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Routing
open Microsoft.AspNetCore.Mvc.Rendering
#if !NET6_0
open Microsoft.AspNetCore.Antiforgery
open Microsoft.AspNetCore.Http.HttpResults
open Microsoft.AspNetCore.Components
#if !NET6_0
open Microsoft.AspNetCore.Http.HttpResults
open Microsoft.AspNetCore.Components.Endpoints
#endif
open Microsoft.AspNetCore.Mvc.ViewFeatures
Expand All @@ -25,7 +25,6 @@ open Fun.Blazor
open Fun.Blazor.Operators



#if !NET6_0
type FunBlazorEndpointFilter(preventStreamingRendering: bool, statusCode: int) =
interface IEndpointFilter with
Expand All @@ -41,6 +40,7 @@ type FunBlazorEndpointFilter(preventStreamingRendering: bool, statusCode: int) =
| x -> return x
}
|> ValueTask<obj>
#endif

module internal Utils =
let mutable isRazorComponentsForSSRMapped = false
Expand All @@ -61,6 +61,7 @@ module internal Utils =
|}>
())

#if !NET6_0
type internal RequiresAntiforgeryMetadata(?requires) =
interface IAntiforgeryMetadata with
member _.RequiresValidation = defaultArg requires true
Expand Down Expand Up @@ -173,7 +174,7 @@ type FunBlazorServerExtensions =
)
=
builder.AddEndpointFilter(FunBlazorEndpointFilter(defaultArg preventStreamingRendering false, defaultArg statusCode 200))

#endif

static member private MakeCreateAttrFn(ty: Type, ?forCustomElement) =
let forCustomElement = defaultArg forCustomElement false
Expand Down Expand Up @@ -246,11 +247,15 @@ type FunBlazorServerExtensions =
(
builder: IEndpointRouteBuilder,
types: Type seq,
?notFoundNode: NodeRenderFragment,
?enableAntiforgery: bool
?notFoundNode: NodeRenderFragment
#if !NET6_0
, ?enableAntiforgery: bool
#endif
)
=
#if !NET6_0
let enableAntiforgery = defaultArg enableAntiforgery false
#endif

types
|> Seq.iter (fun x ->
Expand All @@ -262,6 +267,7 @@ type FunBlazorServerExtensions =
)

let builder =
#if !NET6_0
builder
.Map(
"/fun-blazor-server-side-render-components/{componentType}",
Expand All @@ -272,9 +278,23 @@ type FunBlazorServerExtensions =
)
)
.AddFunBlazor()
#else
builder.Map(
"/fun-blazor-server-side-render-components/{componentType}",
Func<_, _, _>(fun (componentType: string) (ctx: HttpContext) ->
match Utils.razorComponentsForSSRTypes.Value.TryGetValue(componentType) with
| true, comp -> ctx.WriteFunDom(html.blazor (comp.Type, attr = comp.CreateAttr ctx), renderMode = RenderMode.Static)
| _ -> ctx.WriteFunDom(defaultArg notFoundNode html.none, RenderMode.Static)
)
)
#endif

#if !NET6_0
if enableAntiforgery then
builder.WithMetadata(RequiresAntiforgeryMetadata()) |> ignore
#else
()
#endif

/// This will serve all blazor components (inherit from ComponentBase) in the target assembly for server side rendering
/// route pattern: /fun-blazor-server-side-render-components/{componentType}.
Expand All @@ -284,17 +304,22 @@ type FunBlazorServerExtensions =
(
builder: IEndpointRouteBuilder,
assembly: Assembly,
?notFoundNode: NodeRenderFragment,
?enableAntiforgery: bool
?notFoundNode: NodeRenderFragment
#if !NET6_0
,?enableAntiforgery: bool
#endif
)
=
builder.MapRazorComponentsForSSR(
assembly.GetTypes() |> Seq.filter (fun x -> x.IsAssignableTo(typeof<IComponent>)),
defaultArg notFoundNode html.none,
defaultArg enableAntiforgery false
defaultArg notFoundNode html.none
#if !NET6_0
,defaultArg enableAntiforgery false
#endif
)


#if !NET6_0
/// This will serve all components for server side rendering with custom element enabled.
/// You should use it with: services.AddServerSideBlazor(fun options -> options.RootComponents.RegisterCustomElementForFunBlazor<YourComponent>()),
/// route pattern: /fun-blazor-custom-elements/{componentType}
Expand Down
4 changes: 4 additions & 0 deletions Fun.Htmx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## [4.0.5] - 2024-03-14

- Support hxRequestBlazorSSR for dotnet6.0

## [4.0.4] - 2024-03-12

- Upgrade dependencies
Expand Down
2 changes: 1 addition & 1 deletion Fun.Htmx/DslHtmxBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type DomAttrBuilder with
index + 1
)

#if !NET6_0
/// Issues a request to get the blazor component and reader as static dom
[<CustomOperation "hxRequestBlazorSSR">]
member inline _.hxRequestBlazorSSR([<InlineIfLambda>] render: AttrRenderFragment, compTy: System.Type, ?queries: (string * obj) seq, ?method: string) =
Expand Down Expand Up @@ -68,6 +67,7 @@ type DomAttrBuilder with
this.hxRequestBlazorSSR(render, HttpMethods.Post, queryBuilder)


#if !NET6_0
/// Issues a request to get the blazor custom element as the return dom,
/// and it will open a websocket for the component's interactivity
[<CustomOperation "hxRequestCustomElement">]
Expand Down

0 comments on commit b84b88f

Please sign in to comment.