From 8720013b36767243741efb9945d0d4756db31639 Mon Sep 17 00:00:00 2001 From: Moreno Feltscher Date: Tue, 17 Feb 2026 13:09:52 +0100 Subject: [PATCH] feat: add support for request headers when building query ID --- README.md | 4 ++-- src/cache-tags/utils.ts | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bf1bbcd..94ba098 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Utilities for managing [DatoCMS cache tags](https://www.datocms.com/docs/content import { generateQueryId, parseXCacheTagsResponseHeader } from '@smartive/datocms-utils/cache-tags'; // Generate a unique ID for a GraphQL query -const queryId = generateQueryId(document, variables); +const queryId = generateQueryId(document, variables, headers); // Parse DatoCMS's X-Cache-Tags header const tags = parseXCacheTagsResponseHeader('tag-a tag-2 other-tag'); @@ -168,7 +168,7 @@ const provider = new RedisCacheTagsProvider({ }); // After making a DatoCMS query -const queryId = generateQueryId(document, variables); +const queryId = generateQueryId(document, variables, request.headers); const cacheTags = parseXCacheTagsResponseHeader(response.headers['x-cache-tags']); await provider.storeQueryCacheTags(queryId, cacheTags); diff --git a/src/cache-tags/utils.ts b/src/cache-tags/utils.ts index c334f79..f12bd5f 100644 --- a/src/cache-tags/utils.ts +++ b/src/cache-tags/utils.ts @@ -13,15 +13,21 @@ export const parseXCacheTagsResponseHeader = (string?: null | string) => (string?.split(' ') ?? []).map((tag) => tag as CacheTag); /** - * Generates a unique query ID based on the query document and its variables. + * Generates a unique query ID based on the query document, its variables, and optional HTTP headers. * * @param {DocumentNode} document Query document - * @param {TVariables} variables Query variables + * @param {TVariables} variables Optional query variables + * @param {HeadersInit} headers Optional HTTP headers that might affect the query result (e.g., for authentication) * @returns Unique query ID */ -export const generateQueryId = (document: DocumentNode, variables?: TVariables): string => { +export const generateQueryId = ( + document: DocumentNode, + variables?: TVariables, + headers?: HeadersInit, +): string => { return createHash('sha1') .update(print(document)) .update(JSON.stringify(variables) || '') + .update(JSON.stringify(headers) || '') .digest('hex'); };