Umbraco.Community.ServerVariables 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Umbraco.Community.ServerVariables --version 0.1.0
                    
NuGet\Install-Package Umbraco.Community.ServerVariables -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Umbraco.Community.ServerVariables" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Umbraco.Community.ServerVariables" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Umbraco.Community.ServerVariables" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Umbraco.Community.ServerVariables --version 0.1.0
                    
#r "nuget: Umbraco.Community.ServerVariables, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Umbraco.Community.ServerVariables@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Umbraco.Community.ServerVariables&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Umbraco.Community.ServerVariables&version=0.1.0
                    
Install as a Cake Tool

Server Variables for Umbraco

This package reintroduces server variables from C# to JavaScript in Umbraco 15+ with a twist.

NB!: Do not include any secrets in the server variables as they will be exposed to the public.

In short, it is a simple way to expose server variables to your Umbraco Backoffice and/or Frontend. It allows you to add server variables to your site without having to write any or only a little code.

The variables are added through appsettings.json or through the IServerVariablesService interface.

The twist is that the variables are made available through the importmap in the browser. This way, you can import the variables where you need them without having to rely on any global JavaScript objects.

Configuration through appsettings.json

Add the following to your appsettings.json:

{
  "ServerVariables": {
    "Namespace": "vars",
    "CacheHeader": "no-cache, no-store, must-revalidate",
    "Values": {
      "MyVariable": "MyValue"
    }
  }
}
Config Description
Namespace This is the namespace that the variables will be available under in the frontend through the importmap. For example, if you set this to myVars, you will import the variables like this: import { MyVariable } from 'myVars';.
CacheHeader This is the cache header value that will be set on the importmap. This is useful if you want to cache the importmap. By default, it is set to no-cache, no-store, must-revalidate.
Values This is an object with the variables you want to add. The key is the name of the variable, and the value is the value of the variable.

Configuration through IServerVariablesService

Add the following in a composer:

using ServerVariables.Services;
using Umbraco.Cms.Core.Composing;

namespace YourNamespace;

public class ServerVariablesComposer : ComponentComposer<ServerVariablesComponent>
{
}

public class ServerVariablesComponent(IServerVariablesService serverVariablesService) : IAsyncComponent
{
    public Task InitializeAsync(bool isRestarting, CancellationToken cancellationToken)
    {
        serverVariablesService.SetVariable("MyVariable", "MyValue");

        return Task.CompletedTask;
    }

    public Task TerminateAsync(bool isRestarting, CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

Usage in the Backoffice

In any Backoffice component, you can now import the server variables where you need them:

import { MyVariable } from '@server-variables';

console.log('MyVariable', MyVariable);

This will log MyValue to the console.

Usage in the Frontend

In any frontend component, you can now import the server variables where you need them by accessing the virtual path directly:

import { MyVariable } from '/App_Plugins/ServerVariables/index.js';

console.log('MyVariable', MyVariable);

This will log MyValue to the console.

Acknowledgements

<a href="https://www.flaticon.com/free-icons/javascript" title="javascript icons">Javascript icons created by Graphix's Art - Flaticon</a>

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0 188 3/15/2025
0.3.0 201 3/12/2025
0.2.0 209 3/10/2025
0.1.0 202 3/9/2025