Preventing iframe caching in browser

前端 未结 13 1432
自闭症患者
自闭症患者 2020-11-29 18:03

How do you prevent Firefox and Safari from caching iframe content?

I have a simple webpage with an iframe to a page on a different site. Both the outer page and the

13条回答
  •  旧巷少年郎
    2020-11-29 18:30

    As you said, the issue here is not iframe content caching, but iframe url caching.

    As of September 2018, it seems the issue still occurs in Chrome but not in Firefox.

    I've tried many things (adding a changing GET parameter, clearing the iframe url in onbeforeunload, detecting a "reload from cache" using a cookie, setting up various response headers) and here are the only two solutions that worked from me:

    1- Easy way: create your iframe dynamically from javascript

    For example:

    const iframe = document.createElement('iframe')
    iframe.id = ...
    ...
    iframe.src = myIFrameUrl 
    document.body.appendChild(iframe)
    

    2- Convoluted way

    Server-side, as explained here, disable content caching for the content you serve for the iframe OR for the parent page (either will do).

    AND

    Set the iframe url from javascript with an additional changing search param, like this:

    const url = myIFrameUrl + '?timestamp=' + new Date().getTime()
    document.getElementById('my-iframe-id').src = url
    

    (simplified version, beware of other search params)

提交回复
热议问题