Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add disable cache hook #288

Closed
buuing opened this issue May 11, 2022 · 1 comment
Closed

add disable cache hook #288

buuing opened this issue May 11, 2022 · 1 comment

Comments

@buuing
Copy link

buuing commented May 11, 2022

First of all, this is a good project. It solves the compatibility problem of different browsers, but I think we can add a method to disable caching

I have a problem here. In the following example, when I modify the content on the left for the second time, I click the render button and find that the iframe on the right is not re rendered because the fetch request is cached

At this point, unless I call the reload method to clear the cache

But I can't do that

This will cause the CDN link to send the request again and the waiting time is too long

So I'd like to add a method to let users freely decide whether to use fetch cache

code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 48vw;
      height: 300px;
      resize: none;
    }
  </style>
</head>
<body>
<textarea class="box" spellcheck="false">
export default function() {
  return 'Current Time: ' + Date.now()
}
</textarea>
<iframe class="box"></iframe>
<div style="text-align: center;">
  <button id="render">Render</button>
</div>
<script>
  const btn = document.querySelector('#render')
  const textarea = document.querySelector('textarea')
  const iframe = document.querySelector('iframe')
  btn.addEventListener('click', () => {
    const template = genTemplate(textarea.value)
    iframe.contentDocument.open()
    iframe.contentDocument.write(template)
    iframe.contentDocument.close()
  })
  function genTemplate(text) {
    return `
      <div id="app"></div>
      <script>
        var originPath = window.location.origin + window.location.pathname
        var publicPath = originPath.slice(0, originPath.lastIndexOf('/') + 1)
        var VirtualFiles = ${JSON.stringify({ 'app.js': text })}
        var files = {}
        for (const filename in VirtualFiles) {
          files[publicPath + filename] = VirtualFiles[filename]
        }
        window.esmsInitOptions = {
          shimMode: true,
          fetch: async (url, options) => {
            const content = files[url]
            if (content) return new Response(new Blob([content], { type: 'application/javascript' }))
            return fetch(url, options)
          },
          // // This method can be used to selectively disable caching
          // disableCache: (url, options, source) => {
          //   if (files[url]) return true
          // }
        }
      <\/script>
      <script async src="https://cdn.jsdelivr.net/npm/[email protected]/dist/es-module-shims.min.js"><\/script>
      <script type="module-shim">
        import RenderFn from './app.js'
        const dom = document.querySelector('#app')
        setInterval(() => dom.innerHTML = RenderFn(), 1000 / 60)
      <\/script>
    `
  }
</script>
</body>
</html>
@guybedford
Copy link
Owner

In general, one of the goals for ES Module Shims is to follow native browser specifications as closely as possible.

Not caching modules is unfortunately quite a big shift away from the way that browsers work.

Rather a better way to achieve this kind of thing is to add a query parameter to the URL with a resolve hook. The hot reloader uses a similar technique in the PR at #269 coming in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants