Umbraco.Community.ServerVariables
0.1.0
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
<PackageReference Include="Umbraco.Community.ServerVariables" Version="0.1.0" />
<PackageVersion Include="Umbraco.Community.ServerVariables" Version="0.1.0" />
<PackageReference Include="Umbraco.Community.ServerVariables" />
paket add Umbraco.Community.ServerVariables --version 0.1.0
#r "nuget: Umbraco.Community.ServerVariables, 0.1.0"
#:package Umbraco.Community.ServerVariables@0.1.0
#addin nuget:?package=Umbraco.Community.ServerVariables&version=0.1.0
#tool nuget:?package=Umbraco.Community.ServerVariables&version=0.1.0
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 | Versions 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. |
-
net9.0
- Umbraco.Cms.Web.Common (>= 15.2.2)
- Umbraco.Cms.Web.Website (>= 15.2.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.