0

I have a web url www.mywebpage.com/subpage?id=123 which contains:

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
  <meta http-equiv="Pragma" content="no-cache">
  <meta http-equiv="Expires" content="0">
  <script>
    const queryString = window.location.search;
    console.log(queryString);
    const urlParams = new URLSearchParams(queryString);
    const identifier = urlParams.get('id');
    window.location.replace("https://anotherwebpage.com/anothersubpage?id=" + identifier);
  </script>
</head>

<body>
</body>

</html>

All I am doing is taking the id and redirecting to another page, passing in that parameter. This works correctly, until I load the page with a different identifier. It seems that Chrome is still caching the page (in spite of the meta tags), so it redirects to the other page with the old identifier.

How do I ensure that this script runs on every single load of the page so it always redirects with the provided identifier?

Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
  • You're code will always send the correct id, you are just clicking the same link, or to be more clear, only time it might cache is if you use the back button, if you want to ensure this doesn't happen here is a old post on how you can fix it https://stackoverflow.com/questions/30161191/stop-caching-window-object-on-back-reload – Breezer Dec 05 '22 at 01:09
  • That's what I would expect, but that's not when I'm observing: Step 1: In chrome, load website with the new identifier - see the older identifier being loaded. Step 2: In safari, load website with the new identifier - see the new identifier being loaded. I'm attempting to clear the cache in Chrome by going to Chrome -> Clear Browsing Data -> Cached Images & Files and Cookes and other site data but this has no effect. – Josh Gafni Dec 05 '22 at 01:11
  • added further information to be a little more clear on what i meant – Breezer Dec 05 '22 at 01:12
  • Ahh it seems that the redirect is coming from CoudFront. The policy on the subpage in S3 is Cache-Control: no-cache, but it's still being cached by CloudFront. So subpage from first website is called with new identifier, and CloudFront redirects to the same subpage of the first website with the old identifier, and then it redirects to the subpage of the second website with the old identifier. The issue seems to be CloudFront... – Josh Gafni Dec 05 '22 at 01:26
  • great that you found the problem :) good luck – Breezer Dec 05 '22 at 01:28

1 Answers1

1

You can force the link to be different every time by adding a timestamp. Your example:

    window.location.replace('https://anotherwebpage.com/anothersubpage?id=' + identifier
      + '&t=' + new Date().valueOf());
Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20