All Projects → jsakamoto → Toolbelt.Blazor.HeadElement

jsakamoto / Toolbelt.Blazor.HeadElement

Licence: MPL-2.0 License
Head element support (change the document title, "meta" elements such as OGP, and "link" elements) for Blazor apps.

Programming Languages

C#
18002 projects
HTML
75241 projects
typescript
32286 projects
CSS
56736 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Toolbelt.Blazor.HeadElement

vuepress-plugin-autometa
Auto meta tags plugin for VuePress 1.x
Stars: ✭ 40 (-70.8%)
Mutual labels:  meta, head
H2PC TagExtraction
A application made to extract assets from cache files of H2v using BlamLib by KornnerStudios.
Stars: ✭ 12 (-91.24%)
Mutual labels:  meta
BadgeHub
Raspberry Pi, Dymo Turbo Writer 450 badge printing service that logs user information such as name and email and prints a name badge and QR code associated with that information.
Stars: ✭ 25 (-81.75%)
Mutual labels:  meta
BlazorWasmWithDocker
Companion code sample for my blog post - Containerising a Blazor WebAssembly App
Stars: ✭ 16 (-88.32%)
Mutual labels:  blazor
metalang
Meta-language used to generate code stubs in a variety of real-world programming languages.
Stars: ✭ 19 (-86.13%)
Mutual labels:  meta
Harbour-MVP
Building a decentralised p2p meta-tx relayer network [MVP] Codename: Harbour ## We solved this problem: https://medium.com/tabookey/1-800-ethereum-gas-stations-network-for-toll-free-transactions-4bbfc03a0a56
Stars: ✭ 31 (-77.37%)
Mutual labels:  meta
javascript-can-do-what
Talk: JavaScript can do WHAT?!
Stars: ✭ 16 (-88.32%)
Mutual labels:  meta
OpenGraph-Net
.Net Open Graph Parser written in C#
Stars: ✭ 111 (-18.98%)
Mutual labels:  ogp
Blazor.Diagrams
A fully customizable and extensible all-purpose diagrams library for Blazor
Stars: ✭ 327 (+138.69%)
Mutual labels:  blazor
BlazorGrid
A simple, light weight data grid component for Blazor, focused on displaying remote data. Supports sorting and row highlighting, dynamic column changes and custom cell markup.
Stars: ✭ 61 (-55.47%)
Mutual labels:  blazor
MachineLearning-BaseballPrediction-BlazorApp
Machine Learning over historical baseball data using latest Microsoft AI & Development technology stack (.Net Core & Blazor)
Stars: ✭ 36 (-73.72%)
Mutual labels:  blazor
BlazorServerWithDocker
Companion code sample for my blog post - Containerising a Blazor Server App
Stars: ✭ 16 (-88.32%)
Mutual labels:  blazor
QSerializer
This repo for Qt/C++ serialization objects in JSON or XML based on QtCore
Stars: ✭ 33 (-75.91%)
Mutual labels:  meta
FlashCards
Learning Blazor By Creating A Flash Cards Application
Stars: ✭ 17 (-87.59%)
Mutual labels:  blazor
IdentityServer.BlazorAdmin
Identity Server AdminUI using Blazor.
Stars: ✭ 60 (-56.2%)
Mutual labels:  blazor
WebExtensions.Net
A package for consuming WebExtensions API in a browser extension.
Stars: ✭ 22 (-83.94%)
Mutual labels:  blazor
Blazor.Pagination
A reusable pagination component for Blazor.
Stars: ✭ 27 (-80.29%)
Mutual labels:  blazor
vim-eightheader
Vim plugin: Easily create custom headlines, foldtext, toc, etc
Stars: ✭ 17 (-87.59%)
Mutual labels:  title
blazor-tour-of-heroes
The Angular Tour of Heroes tutorial, but done using Blazor instead
Stars: ✭ 17 (-87.59%)
Mutual labels:  blazor
documentation
DocPad's Documentation. Cloned into the Bevry and DocPad websites for generation. Replaced by GitBook: https://docpad.bevry.me
Stars: ✭ 19 (-86.13%)
Mutual labels:  meta

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].