Add cache to products feed (#327)

* Add cache to products feed

* Create .changeset/clean-dragons-jump.md

---------

Co-authored-by: Lukasz Ostrowski <lukasz.ostrowski@saleor.io>
This commit is contained in:
Krzysztof Wolski 2023-03-27 15:49:55 +02:00 committed by GitHub
parent 37f924ece3
commit a811d307ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
"saleor-app-products-feed": patch
---
Added default cache of feed file to 5 minutes. It can be overwritten by env variable.

View file

@ -79,7 +79,7 @@ pnpm dev
3. Expose local environment using tunnel: 3. Expose local environment using tunnel:
Use tunneling tools like [localtunnel](https://github.com/localtunnel/localtunnel) or [ngrok](https://ngrok.com/). Use tunneling tools like [localtunnel](https://github.com/localtunnel/localtunnel) or [ngrok](https://ngrok.com/).
4. Install aplication at your dashboard: 4. Install application at your dashboard:
If you use Saleor Cloud or your local server is exposed, you can install your app by following this link: If you use Saleor Cloud or your local server is exposed, you can install your app by following this link:
@ -108,3 +108,7 @@ The choice of the APL is done using `APL` environment variable. If value is not
- `upstash`: use [Upstash](https://upstash.com/) Redis as storage method. Free account required. Can be used for development and production and supports multi-tenancy. Requires `UPSTASH_URL` and `UPSTASH_TOKEN` environment variables to be set - `upstash`: use [Upstash](https://upstash.com/) Redis as storage method. Free account required. Can be used for development and production and supports multi-tenancy. Requires `UPSTASH_URL` and `UPSTASH_TOKEN` environment variables to be set
If you want to use your own database, you can implement your own APL. [Check the documentation to read more.](https://github.com/saleor/saleor-app-sdk/blob/main/docs/apl.md) If you want to use your own database, you can implement your own APL. [Check the documentation to read more.](https://github.com/saleor/saleor-app-sdk/blob/main/docs/apl.md)
### Environment variables
- `FEED_CACHE_MAX_AGE`: Amount of seconds the the response will be cached for. Default time is 5 minutes.

View file

@ -8,6 +8,11 @@ import { getGoogleFeedSettings } from "../../../../../lib/google-feed/get-google
import { generateGoogleXmlFeed } from "../../../../../lib/google-feed/generate-google-xml-feed"; import { generateGoogleXmlFeed } from "../../../../../lib/google-feed/generate-google-xml-feed";
import { fetchShopData } from "../../../../../lib/google-feed/fetch-shop-data"; import { fetchShopData } from "../../../../../lib/google-feed/fetch-shop-data";
// By default we cache the feed for 5 minutes. This can be changed by setting the FEED_CACHE_MAX_AGE
const FEED_CACHE_MAX_AGE = process.env.FEED_CACHE_MAX_AGE
? parseInt(process.env.FEED_CACHE_MAX_AGE, 10)
: 60 * 5;
export const handler = async (req: NextApiRequest, res: NextApiResponse) => { export const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const url = req.query.url as string; const url = req.query.url as string;
const channel = req.query.channel as string; const channel = req.query.channel as string;
@ -98,8 +103,8 @@ export const handler = async (req: NextApiRequest, res: NextApiResponse) => {
logger.debug("Feed generated. Returning formatted XML"); logger.debug("Feed generated. Returning formatted XML");
// TODO: add cache headers
res.setHeader("Content-Type", "text/xml"); res.setHeader("Content-Type", "text/xml");
res.setHeader("Cache-Control", `s-maxage=${FEED_CACHE_MAX_AGE}`);
res.write(xmlContent); res.write(xmlContent);
res.end(); res.end();
}; };

View file

@ -115,7 +115,8 @@
"SENTRY_ORG", "SENTRY_ORG",
"SENTRY_PROJECT", "SENTRY_PROJECT",
"SENTRY_AUTH_TOKEN", "SENTRY_AUTH_TOKEN",
"NEXT_PUBLIC_VERCEL_ENV" "NEXT_PUBLIC_VERCEL_ENV",
"FEED_CACHE_MAX_AGE"
] ]
}, },
"build#saleor-app-monitoring": { "build#saleor-app-monitoring": {