• Resolved timtallop

    (@timtallop)


    Hello,

    Is it possible to exclude inline JS from page caching?

    The data included in the inline script is dynamically generated (using a PHP file) and is used by other JS files. It is unique to each user and can change with every page load.

    Any input would be greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey, it’s possible to exclude dynamically generated inline JavaScript from being cached. Since this data is unique to each user and may change with every page load, here are a few approaches you can try:

    1. Exclude Specific Pages from Caching: If the inline JavaScript is only on specific pages, you can exclude those pages from caching. Here’s how:
      • Go to WP-Optimize -> Cache -> Advanced Settings.
      • In the “URLs to exclude from caching” section, add the specific page URLs or use wildcard patterns to match multiple pages (e.g., /members/*).
    2. Use Cookies to Prevent Caching: If the dynamic content is user-specific and based on cookies, you can configure WP-Optimize to prevent caching when certain cookies are set. To do this:
      • In the same Advanced Settings tab, scroll down to the “Cookies which, if present, will prevent caching” section.
      • Add the cookie name (e.g., wordpress_logged_in* for logged-in users) to the list. This will ensure that pages with those cookies present won’t be cached.
    3. Move Inline Script to an External File: Another approach is to move the inline JavaScript into an external file and load it asynchronously or using the defer attribute. This allows the script to run after the page has loaded, so it won’t be part of the cached content.

    These steps should help prevent caching for the dynamic content on your site. Thank you!

    Thread Starter timtallop

    (@timtallop)

    Thank you very much for the thorough answer Mr. Mansour.

    You have given me the idea to use cookies instead of inline JS for this use case, which would make it possible for the pages to get cached.

    You’re welcome! Glad to hear that : )

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.