I’m trying to implement caching at strapi server side but i’m kinda confused on how do go about it, i’ve gone through couple of strapi forums, issues about the same but not able to implement it correctly. I have a nextjs app & strapi project (a blog website), i’ve imlpemented caching at client side using @apollo/client package, now i want to do the same for server. So for that i’ve done the following :-
config/plugins.js
const apolloServerPluginResponseCache =
require("@apollo/server-plugin-response-cache").default;
const ApolloServerPluginCacheControl =
require("apollo-server-core").ApolloServerPluginCacheControl;
module.exports = ({ env }) => {
return {
graphql: {
enabled: true,
config: {
playgroundAlways: true,
defaultLimit: 20,
maxLimit: -1,
apolloServer: {
tracing: true,
plugins: [
ApolloServerPluginCacheControl({
defaultMaxAge: 60,
}),
apolloServerPluginResponseCache({
shouldReadFromCache: async (requestContext) => {
return true;
},
shouldWriteToCache: async (requestContext) => {
return true;
},
extraCacheKeyData: async (requestContext) => {
return true;
},
sessionId: async (requestContext) => {
return null;
},
}),
]
}
}
strapi project package.json
{
"name": "blog-cms",
"version": "0.1.0",
"private": true,
"description": "A Strapi application",
"license": "MIT",
"author": {
"name": "A Strapi developer"
},
"scripts": {
"build": "strapi build",
"dev": "strapi develop",
"start": "strapi start",
"strapi": "strapi"
},
"dependencies": {
"@_sh/strapi-plugin-ckeditor": "^1.1.1",
"@apollo/server": "^4.10.5",
"@apollo/server-plugin-response-cache": "^4.1.3",
"@aws-sdk/client-secrets-manager": "^3.480.0",
"@strapi/plugin-color-picker": "^4.9.1",
"@strapi/plugin-graphql": "^4.24.2",
"@strapi/plugin-i18n": "4.24.2",
"@strapi/plugin-users-permissions": "4.24.2",
"@strapi/provider-upload-aws-s3": "^4.24.2",
"@strapi/strapi": "4.24.2",
"apollo-server-core": "^3.13.0",
"apollo-server-redis-cache": "^0.1.22",
"mysql": "^2.18.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^5.3.4",
"styled-components": "^5.3.11"
},
"engines": {
"node": ">=12.x.x <=16.x.x",
"npm": ">=6.0.0"
},
"strapi": {
"uuid": "isioa828s-r2f6-123s-sjx4-cusy2628s71"
}
}
But now, when i access my nextjs app using this config, it returns 404 (500 status code from server), but when i remove the plugins array from config, it works fine.
Also, even though i manage to get this plugin to work, how am i supposed to test if the caching is really working?