Skip to content
Snippets Groups Projects
Commit a331bad3 authored by Matt Glaman's avatar Matt Glaman
Browse files

Issue #3155966 by mglaman, pixelwhip: ResourceResponseFactory to return a...

Issue #3155966 by mglaman, pixelwhip: ResourceResponseFactory to return a CacheableResourceResponse if request is cacheable
parent b8fdd6ac
Branches
No related tags found
1 merge request!7Only use cacheable resource response if request is cacheable
Pipeline #136355 passed with warnings
name: 'JSON:API Resources'
description: "This module let's you define custom resources at routes of your choice that use existing resource types."
core_version_requirement: ^8.8 || ^9 || ^10
core_version_requirement: ^9.1 || ^10
type: module
dependencies:
- drupal:jsonapi
......@@ -75,21 +75,20 @@ final class ResourceResponseFactory {
$includes = $this->getIncludes($request, $data);
// \Drupal\jsonapi\ResourceResponse no longer implements
// CacheableResponseInterface in Drupal 9.1.
// Drupal\jsonapi\CacheableResourceResponse has ben added for cacheable
// responses. Keep compatibility with Drupal < 9.1.
// See https://www.drupal.org/node/3163310
$document = new JsonApiDocumentTopLevel($data, $includes, $links, $meta);
$response = class_exists('\Drupal\jsonapi\CacheableResourceResponse') ?
new CacheableResourceResponse($document, $response_code, $headers) :
new ResourceResponse($document, $response_code, $headers);
// Make sure that different sparse fieldsets are cached differently.
$cache_contexts[] = 'url.query_args:fields';
// Make sure that different sets of includes are cached differently.
$cache_contexts[] = 'url.query_args:include';
$cacheability = (new CacheableMetadata())->addCacheContexts($cache_contexts);
$response->addCacheableDependency($cacheability);
if ($request->isMethodCacheable()) {
$response = new CacheableResourceResponse($document, $response_code, $headers);
// Make sure that different sparse fieldsets are cached differently.
$cache_contexts[] = 'url.query_args:fields';
// Make sure that different sets of includes are cached differently.
$cache_contexts[] = 'url.query_args:include';
$cacheability = (new CacheableMetadata())->addCacheContexts($cache_contexts);
$response->addCacheableDependency($cacheability);
}
else {
$response = new ResourceResponse($document, $response_code, $headers);
}
return $response;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment