Skip to content

IPageRequestHook

IPageRequestHook runs your code on every page request, before the page renders. Use it to add scripts, styles, meta tags or data binding values to the page, to allow a domain in the Content-Security-Policy header, or to set a redirect or a status code.

Interface declaration

	namespace Veva.Core.Domain.Interfaces;

	public interface IPageRequestHook
	{
		int OrderIndex { get; }

		Task OnRequestAsync(PageRequest pageRequest);
	}

Example

This hook adds a meta tag to every page that was found, and a value that editors can print in text with the data binding token CopyrightYear.

	using Veva.Core.Domain.Interfaces;
	using Veva.Core.Domain.Pages;

	public class SiteDefaultsHook : IPageRequestHook
	{
		// After Veva has resolved the page (500) and its meta data (650),
		// before it writes the security headers (1100).
		public int OrderIndex => 900;

		public Task OnRequestAsync(PageRequest pageRequest)
		{
			// Page is empty when no page answers the address and the website has no 404 page.
			if (pageRequest.Page is null)
			{
				return Task.CompletedTask;
			}

			pageRequest.MetaTags.Add(new MetaTag { Name = "generator", Content = "Veva" });
			pageRequest.DataBindingKeys["CopyrightYear"] = DateTime.Today.Year;

			return Task.CompletedTask;
		}
	}

Registration

This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).

	using Veva.Core.Domain.Interfaces;

	public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
	{
		// Every registered hook runs; there is no key and nothing for an editor to switch on.
		services.AddScoped<IPageRequestHook, SiteDefaultsHook>();
	}

Veva builds a PageRequest (Veva.Core.Domain.Pages) for each page it serves, and it does so by running page request hooks. Its own steps are hooks too: resolving the website, the page, the template and the theme, loading the content and writing the security headers. Yours runs among them. Unlike a render hook, it is not picked by an editor: once registered it runs on every request, on every website in the application.

OrderIndex decides when it runs. Hooks run from the lowest number to the highest, and every hook runs: none can stop the ones after it. Veva's own hooks use these numbers, so choose yours by what must already be known:

  • 200: the website (pageRequest.Website).
  • 500: the page (pageRequest.Page). At 503 a request that found no page gets status 404 and the website's 404 page, if one is set.
  • 510: the template, and the page's own redirect.
  • 600: the theme. 650: the page's settings and meta data.
  • 700: the page's content.
  • 1100: the Content-Security-Policy header is written from pageRequest.SecurityOptions.

A hook that only adds to the page is safe between 700 and 1100. A hook that adds Content-Security-Policy sources must run before 1100; see Content-Security-Policy header.

What a hook can change. Scripts and Styles hold the script and stylesheet references of the page, MetaTags and OpenGraphTags its meta tags, and SecurityOptions the sources the Content-Security-Policy header allows. DataBindingKeys holds values that become data binding tokens on the page. RedirectUrl together with ResponseType sets a redirect, and StatusCode the status of the response. Check RedirectUrl before you set it, so you do not replace a redirect an earlier hook has set.

What a hook can read. Uri, Query, User, Culture, IsBot and EditMode describe the request. EditMode is true when an editor is looking at the page in the designer or at a version that is not published, so a hook that adds tracking can leave those views alone.

The hook is created from the service container, so constructor injection works. Keep it quick: it runs before every page.

Type to search the docs, components and API.

↑↓ to move ↵ to open Esc to close Search by Veva