简体   繁体   English

如何在wordpress网站内使用PHP查找散列的bundle.js文件并将相应的文件名插入script标签?

[英]How can I use PHP inside a wordpress site to find a hashed bundle.js file and insert the appropriate file name into the script tag?

So, I've never written php until today, and I'm trying to implement a cache breaking system on a wordpress site that has some React components living inside it. 因此,直到今天我才写过php,并且我正在尝试在其中包含一些React组件的wordpress网站上实现一个缓存破坏系统。 So inside of the footer-home.php file I have this: 因此,在footer-home.php文件中,我具有以下内容:

      </div> <?php // close #app ?>
    </main>
  <div class="container footer">

    <div class="row">
      <div class="col-sm-12">
        <div id="instagram"></div>
      </div>
    </div>

    <?php get_footer('shared') ?>
  </div>

  </div><?php //close container ?>

<?php
function grab_hashed_bundle_script() {
  $path = '/client/';
  $fileName = null;
  $dirJS = new DirectoryIterator($path);

  foreach ($dirJS as $file) {
    if (pathinfo($file, PATHINFO_EXTENSION) === 'js') {
      $fileName = basename($file);
      break;
    }
  }
  return $fileName;
}

$bundle_js = grab_hashed_bundle_script();
?>  
  <script src="/client/<?php echo $bundle_js ?>"></script>
  <?php wp_footer(); ?>
</body>
</html>

I know this is ugly AF and hacky, so if anyone can point out a better way to do this, I'm all ears. 我知道这是丑陋的AF,而且很笨拙,因此,如果有人可以指出一种更好的方法来做到这一点,我将不胜枚举。

The reason I need to do this is I'm having webpack add a random 6-digit hash to the bundle.js filename(as in bundle-123456.js ) every time we run a new build. 我需要这样做的原因是,每次运行新版本时,我都会让webpack向bundle.js文件名(如bundle-123456.js )添加一个随机的6位数字哈希。 Previously, we just had a regular script tag in this footer file like so: <script src=/client/bundle.js"></script> but clients' browsers would end up caching bundle.js even after we had updated it requiring customers to have to empty their cache in order to get the new .js files. 以前,我们在此页脚文件中只是有一个常规脚本标签,如下所示: <script src=/client/bundle.js"></script>但是即使更新了要求,客户端的浏览器仍会缓存bundle.js。客户必须清空其缓存以获取新的.js文件。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Also, I'm not trying to cache bust with a param as suggested in the comment. 另外,我并没有按照注释中的建议尝试缓存带有参数的半身像。 I'm trying to bust based on the random hash that I'm having webpack insert into the name of the bundle.js file upon building. 我正在尝试根据构建时将webpack插入bundle.js文件名称中的随机散列进行破坏。

This is the solution one of my co-workers came up with: 这是我的一位同事提出的解决方案:

Inside of functions.php : functions.php内部:

/**
 * Grab Hashed Bundle Files
 */

function enqueue_hashed_bundle_files() {
  // soooo gross. would love to know of a cleaner way.
  $build_dir = get_theme_root() . '/../../../client/build/';
  $all_files = scandir($build_dir);

  $css_files = array();
  $js_files = array();

  foreach( $all_files as $file ){
    $pathinfo = pathinfo($file);
    switch( $pathinfo['extension'] ){
      case 'js':
        array_push($js_files, $file);
        break;
      case 'css':
        array_push($css_files, $file);
        break;
      default:
        break;
    }
  }

  // now that we have the filenames, we can access them directly with the
  // absolute url

  $base_url = get_option('siteurl') . '/client/';

  wp_enqueue_script( 'bundlejs', $base_url . $js_files[0], array(), null, true );
  wp_enqueue_style( 'bundlecss', $base_url . $css_files[0], array(), null );
}

Change your request for the js file to have a query param instead of a random string in the filename. 将您的js文件请求更改为具有查询参数,而不是文件名中的随机字符串。

See this post. 看到这篇文章。 The browser shouldn't cache it with a query string. 浏览器不应使用查询字符串对其进行缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM