Ron[ny] Pinkas
Ron[ny] Pinkas
CDCloudflare Developers
Created by sadek hossain on 9/13/2024 in #general-help
How to cache nextjs pages using cloudflare for non logged in users only using cookie set or not
Which of the above replies doesn't work?
6 replies
CDCloudflare Developers
Created by sadek hossain on 9/13/2024 in #general-help
How to cache nextjs pages using cloudflare for non logged in users only using cookie set or not
Here is the reply I got from the AI Chat Assistant at hy-rag.com To cache Next.js pages for non-logged-in users using Cloudflare, you can utilize cookies to determine whether a user is logged in or not. Here's a general approach: 1. Set a Cookie for Logged-in Users: Ensure that when a user logs in, a specific cookie is set (e.g., loggedIn=true). 2. Cloudflare Workers for Cookie Parsing: Use Cloudflare Workers to parse cookies and determine if the loggedIn cookie is present. If the cookie is absent, the user is considered not logged in, and the page can be cached. 3. Cache Configuration: Configure your caching strategy in Cloudflare to cache pages only when the loggedIn cookie is not present. This can be done by setting cache rules in your Cloudflare Worker script. 4. Next.js Caching: By default, Next.js with Cloudflare Pages caches all fetch() subrequests. You can manage this caching behavior using Cloudflare's Cache API or Workers KV for more advanced scenarios. For more detailed guidance on caching with Cloudflare and Next.js, you can refer to the following resources: - Caching and data revalidation in your Next.js app - Cookie parsing with Cloudflare Workers These documents will help you set up the caching logic and manage cookies effectively.
6 replies