Answers.Body: <p>It looks like your approach is working rather well, so I'll assume you are simply asking for an alternative approach.</p>
  <p>To make 1 AJAX request rather than the 80 or so that the page is currently making, I would use Blitz's dynamic content feature to load <em>all</em> of the products in one single template. In that template you can check whether the user is logged in, and hence whether to the display the prices or not, as you are already doing.</p>
  <p>An alternative alternative would be to use <a href="https://putyourlightson.com/plugins/sprig" rel="nofollow noreferrer">Sprig</a>, see the <a href="https://putyourlightson.com/sprig/cookbook#dynamic-content" rel="nofollow noreferrer">dynamic content recipe</a>. Sprig gives you more control over how things are dynamically loaded. You could even tweak the way products are loaded to create a smoother user experience. For example, cache the products without the prices (the default for non-logged in users) and on page load, use Sprig to re-render the component with or without the prices (based on the user type).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-04T07:42:07.087Z
Answers.Text: I was wondering whether my approach was a good solution. After I wrote the question, I started googling for `dynamic content in cached pages` and found some thoughts on the subject. I have already tested Sprig for other use cases, and I will have an extra look at the framework as you suggested. 
  
  So maybe my question is rather: Have you got any experience with dynamic content in cached pages - and how would you do it?
Answers.UserDisplayName: nitech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-04T08:12:56.553Z
Answers.Text: I would use Sprig if I had another use-case for it on the site, otherwise the approach in my answer.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-06T20:08:05.033Z
Answers.Text: I am hesitant to load the products via Ajax because of the speed benifit of caching. Also, it is recommended to serve any content you want Google to know about as a part of the cached content (although I know it is said that Google runs JS).
Answers.UserDisplayName: nitech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-07T08:33:11.877Z
Answers.Text: I added to my answer to about how to achieve a smoother user experience using Sprig.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-03T19:15:20.027Z
Answers.LastActivityDate: 2021-02-07T08:32:41.637Z
Answers.LastEditDate: 2021-02-07T08:32:41.637Z
Answers.UserDisplayName: Ben Croker
Body: <p>What strategy should I take when I want to serve different data to different users with (Blitz) cache? Logged in users get one set of prices (that can vary depending on their price group memberships), users that are not logged in don't see prices at all.</p>
  <p>I read and implemented a test of Dynamic Content as <a href="https://putyourlightson.com/plugins/blitz#dynamic-content" rel="nofollow noreferrer">described in the Blitz Cache documentation</a>, and the result looks promising. Lighthouse says between 97-99 on performance with a 0,6s Fist Contentful Paint, serving gzipped caches from Blitz Storage on the Hyperlane VPS (I will test Cloudflare too).</p>
  <p>The question is rather about the potential user experience of having the prices ajaxed into the body of the html document. I've never seen any site do it this way before. Have a look at the result. Pay attention to how the prices appear and the related network events on the right:</p>
  <p><a href="https://vimeo.com/507996281/0d16df559d" rel="nofollow noreferrer">https://vimeo.com/507996281/0d16df559d</a></p>
  <p><a href="https://i.sstatic.net/J3IY2.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/J3IY2.jpg" alt="Ajaxing in content" /></a></p>
  <p>(I realize that the <code>defaultPrice</code> is passed as a parameter to the <code>productPrice</code> template, and therefore the price is exposed - I've later replaced it with an encrypted string using the free plugin <a href="https://plugins.craftcms.com/cryptographer" rel="nofollow noreferrer">Cryptographer</a>)</p>
  
Tags: <cache><plugin-blitz>
Title: How to cache and serve different data to different users using Blitz?
ThreadText: # How to cache and serve different data to different users using Blitz?
  
  (Asked by: username_0 on 2021-02-03)
  
  What strategy should I take when I want to serve different data to different users with (Blitz) cache? Logged in users get one set of prices (that can vary depending on their price group memberships), users that are not logged in don't see prices at all.
  
  I read and implemented a test of Dynamic Content as [described in the Blitz Cache documentation](https://putyourlightson.com/plugins/blitz#dynamic-content), and the result looks promising. Lighthouse says between 97-99 on performance with a 0,6s Fist Contentful Paint, serving gzipped caches from Blitz Storage on the Hyperlane VPS (I will test Cloudflare too).
  
  The question is rather about the potential user experience of having the prices ajaxed into the body of the html document. I've never seen any site do it this way before. Have a look at the result. Pay attention to how the prices appear and the related network events on the right:
  
  <https://vimeo.com/507996281/0d16df559d>
  
  [![Ajaxing in content](https://i.sstatic.net/J3IY2.jpg)](https://i.sstatic.net/J3IY2.jpg)
  
  (I realize that the `defaultPrice` is passed as a parameter to the `productPrice` template, and therefore the price is exposed - I've later replaced it with an encrypted string using the free plugin [Cryptographer](https://plugins.craftcms.com/cryptographer))
  
  **Tags:** cache, plugin-blitz
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-02-03. Score: 1
  
  It looks like your approach is working rather well, so I'll assume you are simply asking for an alternative approach.
  
  To make 1 AJAX request rather than the 80 or so that the page is currently making, I would use Blitz's dynamic content feature to load *all* of the products in one single template. In that template you can check whether the user is logged in, and hence whether to the display the prices or not, as you are already doing.
  
  An alternative alternative would be to use [Sprig](https://putyourlightson.com/plugins/sprig), see the [dynamic content recipe](https://putyourlightson.com/sprig/cookbook#dynamic-content). Sprig gives you more control over how things are dynamically loaded. You could even tweak the way products are loaded to create a smoother user experience. For example, cache the products without the prices (the default for non-logged in users) and on page load, use Sprig to re-render the component with or without the prices (based on the user type).
  
  > **Comments:**
  > **username_0**: I was wondering whether my approach was a good solution. After I wrote the question, I started googling for `dynamic content in cached pages` and found some thoughts on the subject. I have already tested Sprig for other use cases, and I will have an extra look at the framework as you suggested.   So maybe my question is rather: Have you got any experience with dynamic content in cached pages - and how would you do it?  
  > **username_1**: I would use Sprig if I had another use-case for it on the site, otherwise the approach in my answer.  
  > **username_0**: I am hesitant to load the products via Ajax because of the speed benifit of caching. Also, it is recommended to serve any content you want Google to know about as a part of the cached content (although I know it is said that Google runs JS).  
  > **username_1**: I added to my answer to about how to achieve a smoother user experience using Sprig.  
  
  ---
  
  