diff --git a/.changeset/clean-dragons-jump.md b/.changeset/clean-dragons-jump.md new file mode 100644 index 0000000..049c8aa --- /dev/null +++ b/.changeset/clean-dragons-jump.md @@ -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. diff --git a/apps/products-feed/README.md b/apps/products-feed/README.md index f46c645..88186bd 100644 --- a/apps/products-feed/README.md +++ b/apps/products-feed/README.md @@ -79,7 +79,7 @@ pnpm dev 3. Expose local environment using tunnel: 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: @@ -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 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. diff --git a/apps/products-feed/src/pages/api/feed/[url]/[channel]/google.xml.ts b/apps/products-feed/src/pages/api/feed/[url]/[channel]/google.xml.ts index edb95fe..8700e0f 100644 --- a/apps/products-feed/src/pages/api/feed/[url]/[channel]/google.xml.ts +++ b/apps/products-feed/src/pages/api/feed/[url]/[channel]/google.xml.ts @@ -8,6 +8,11 @@ import { getGoogleFeedSettings } from "../../../../../lib/google-feed/get-google import { generateGoogleXmlFeed } from "../../../../../lib/google-feed/generate-google-xml-feed"; 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) => { const url = req.query.url 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"); - // TODO: add cache headers res.setHeader("Content-Type", "text/xml"); + res.setHeader("Cache-Control", `s-maxage=${FEED_CACHE_MAX_AGE}`); res.write(xmlContent); res.end(); }; diff --git a/turbo.json b/turbo.json index d049c7d..ef43366 100644 --- a/turbo.json +++ b/turbo.json @@ -115,7 +115,8 @@ "SENTRY_ORG", "SENTRY_PROJECT", "SENTRY_AUTH_TOKEN", - "NEXT_PUBLIC_VERCEL_ENV" + "NEXT_PUBLIC_VERCEL_ENV", + "FEED_CACHE_MAX_AGE" ] }, "build#saleor-app-monitoring": {