Skip to content

Adds variation by the header name Accept-Language to the delivery API output cache policy#19709

Merged
AndyButland merged 3 commits intov13/devfrom
v13/bugfix/vary-by-language-header-for-delivery-api-output-cache
Jul 11, 2025
Merged

Adds variation by the header name Accept-Language to the delivery API output cache policy#19709
AndyButland merged 3 commits intov13/devfrom
v13/bugfix/vary-by-language-header-for-delivery-api-output-cache

Conversation

@AndyButland
Copy link
Contributor

@AndyButland AndyButland commented Jul 11, 2025

Prerequisites

  • I have added steps to test this contribution in the description below

Resolves: #19706

Description

The delivery API output cache isn't varying over the accepted headers, leading to the problem described in the linked issue.

This PR adjusts the policy to vary over the headers that could be used for the document and media requests are included in the collection of details to vary by.

Note that "Preview" isn't necessary to include as preview requests aren't cached anyway.

Testing

See the linked issue. Note that once the PR is applied the request for the same item by different languages is not cached such that each language can be retrieved.

Confirm that output caching still works by modifying the content item and note that the old values are retrieved until the cache time expires.

Merge and Release

When merged this will be part of 13.10, but should also be cherry-picked up for 16.2.

When porting to 16, include "Accept-Segment" in the constants.

Copilot AI review requested due to automatic review settings July 11, 2025 07:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the Delivery API’s output caching by varying cached responses based on the Accept-Language and Start-Item headers, ensuring different languages don’t share the same cache entry.

  • Added StringValues import and new constructor to accept header names in DeliveryApiOutputCachePolicy
  • Updated AddOutputCache to pass header names for content and media caching policies
  • Modified CacheRequestAsync to apply the header-based vary rules

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Umbraco.Cms.Api.Delivery/DependencyInjection/UmbracoBuilderExtensions.cs Imported StringValues and updated AddOutputCache to pass header names for both content and media caching policies
src/Umbraco.Cms.Api.Delivery/Caching/DeliveryApiOutputCachePolicy.cs Introduced a new constructor overload accepting StringValues and applied the header names to CacheVaryByRules.HeaderNames in CacheRequestAsync
Comments suppressed due to low confidence (2)

src/Umbraco.Cms.Api.Delivery/DependencyInjection/UmbracoBuilderExtensions.cs:112

  • Consider adding a unit or integration test that sends requests with different Accept-Language headers to verify the output cache varies as expected and does not return the same cached response.
                options.AddPolicy(Constants.DeliveryApi.OutputCache.ContentCachePolicy, new DeliveryApiOutputCachePolicy(outputCacheSettings.ContentDuration, new StringValues(["Accept-Language", "Start-Item"])));

src/Umbraco.Cms.Api.Delivery/DependencyInjection/UmbracoBuilderExtensions.cs:112

  • The array literal syntax new StringValues(["Accept-Language", "Start-Item"]) is invalid C#. Use either new StringValues(new[] { "Accept-Language", "Start-Item" }) or the params-based constructor: new StringValues("Accept-Language", "Start-Item").
                options.AddPolicy(Constants.DeliveryApi.OutputCache.ContentCachePolicy, new DeliveryApiOutputCachePolicy(outputCacheSettings.ContentDuration, new StringValues(["Accept-Language", "Start-Item"])));

@AndyButland AndyButland merged commit 2748fdf into v13/dev Jul 11, 2025
18 of 19 checks passed
@AndyButland AndyButland deleted the v13/bugfix/vary-by-language-header-for-delivery-api-output-cache branch July 11, 2025 13:51
AndyButland added a commit that referenced this pull request Jul 11, 2025
… output cache policy (#19709)

* Adds variation by the header name Accept-Language to the develivery API output cache policy

* Removed obsolete constructor (not necessary as the class is internal).

* Introduce contants for header names.
@AndyButland
Copy link
Contributor Author

AndyButland commented Jul 11, 2025

Merged for 13 and cherry-picked over to main with 4deb756 for 16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants