Giter Club home page Giter Club logo

toolbelt.blazor.headelement's Introduction

Blazor Head Element Helper NuGet Package

Summary

This components and services allows you to change the title of document, "meta" elements such as OGP, and "link" elements such as canonical url, favicon etc. on your Blazor app.

This package supports both seiver-side Blazor and client-side Blazor WebAssembly app.

And also supports server-side pre-rendering on your server-side Blazor app.

See also following the live demonstration sites.

Notice

Blazor on .NET 6 (since preview 7) or later has started to provide <PageTitle> and <HeadContent> components.

"ASP.NET Core updates in .NET 6 Preview 7 - Modify HTML <head> content from Blazor components" | ASP.NET Blog

However, I'm going to continue to provide this package and keep maintaining, because this library still has a few advantages in some edge cases.

Please see the following table to know the differences between this library and <PageTitle>/<HeadContent> components in .NET 6 or later.

Feature This library .NET6 or later
Overriding pre-rendered meta or link ๐Ÿ‘ Can handle it properly. ๐Ÿ˜ข Just append it. it may cause duplication.
Canceling <meta "http-equiv"="refresh" /> ๐Ÿ‘ Works well. ๐Ÿ˜ข There is no support.
Using it as a service, not components ๐Ÿ‘ Supported. ๐Ÿ˜ข Maybe not Supported.
Modify <head> content from many places ๐Ÿ‘ Can do. ๐Ÿ˜ข Only one instance of the <HeadContent> component can effect.
Server-side pre-rendering ๐Ÿ‘ Just add one line inside the server code. ๐Ÿ˜ข You have to split the fallback page into two .cshtml files for resolving complicated rendering order problems.
Controlling the order of elements inside a <head> ๐Ÿ˜ข hard ๐Ÿ‘ Can control perfectly
Support ๐Ÿ˜ข Just personal hobby ๐Ÿ‘ Official support from the ASP.NET Team

How to use

Installation

  1. Add package to your project like this.
dotnet add package Toolbelt.Blazor.HeadElement
  1. Register "Head Element Helper" service at your Blazor app's Startup.
using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this, and...

public class Startup
{
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddHeadElementHelper(); // <- Add this.
    ...

If the project is Blazor WebAssembly App v.3.2+, you should do it in Program class, instead.

using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this, and...
...
public class Program
{
  public static async Task Main(string[] args)
  {
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    ...
    builder.Services.AddHeadElementHelper(); // <- Add this.
    ...
  1. Open Toolbelt.Blazor.HeadElement namespace in _Imports.razor file.
@* This is "_Imports.razor" *@
...
@using Toolbelt.Blazor.HeadElement

A. Change the title of the document

  1. Markup <Title> component in your .razor file.
@* This is "Pages/Counter.razor" *@
@page "/counter"

<Title>Counter(@currentCount) - Server Side App</Title>

The title of document will be changed.

fig1

B. Change "meta" elements

You can also add or override "meta" elements at runtime dynamically using <Meta> component like this.

@* This is "Pages/Counter.razor" *@
@page "/counter"

<Meta Property="ogp:title" Content="Counter" />

Note: You can also use native attribute names (lower and kebab case. ex: "http-equiv") instead of Razor component parameter names (pascal case).

C. Change "link" elements

You can add or override "link" elements at runtime dynamically using <Link> component like this.

@* This is "Pages/Counter.razor" *@
@page "/counter"

<Link Rel="icon" Href="@($"/favicons/{GetFaviconName()}")" />

fig3

Note: You can also use native attribute names (lower and kebab case) instead of Razor component parameter names (pascal case).

D. IHeadElementHelper

You can do these tasks by using IHeadElementHelper service instead of using <Title>, <Meta>, and <Link> components.

You can get the IHeadElementHelper service instnace by "Dependency Injection" mechanism.

@inject IHeadElementHelper HeadElementHelper
@using static Toolbelt.Blazor.HeadElement.MetaElement
...
@code {
  protected override async Task OnInitializedAsync()
  {
    await HeadElementHelper.SetTitleAsync("Wow!");
    await HeadElementHelper.SetMetaElementsAsync(
      ByName("description", "Foo bar..."),
      ByProp("og:title", "WoW!")
    );
    await HeadElementHelper.SetLinkElementsAsync(
      new LinkElement("canonical", "https://foo.com/bar")
    );
    ...

Server-side pre-rendering support

If you want to get srever-side pre-rendering support, do this.

  1. Add Toolbelt.Blazor.HeadElement.ServerPrerendering package to your project like this.
dotnet add package Toolbelt.Blazor.HeadElement.ServerPrerendering
  1. Register "Head Element Server Prerendering" middleware at your server-side Blazor app's Startup, before app.UseStaticFiles().
using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this, and...
...
public class Startup
{
  ...
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddHeadElementHelper(); // <!- Don't miss this line, and...
    ...

  public void Configure(IApplicationBuilder app)
  {
    app.UseHeadElementServerPrerendering(); // <- Add this.
    ...
    app.UseStaticFiles()
    ...

fig2

License

Mozilla Public License Version 2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.