Javascript NW.js未正确清除缓存

Javascript NW.js未正确清除缓存,javascript,module,nw.js,Javascript,Module,Nw.js,环境 NW.js v0.12.3(32位) 视窗8 (玩家)VLC构建(2.2.1) 问题 新窗口从父窗口打开Webchimera模块已加载,但由于其窗口对象指向父窗口而崩溃。那是因为NW.js 家长 <div id="chimera_container"></div> ... var gui; // global variable for parent object var wjs; // global variable for parent object ..

环境

  • NW.js v0.12.3(32位)
  • 视窗8
  • (玩家)VLC构建(2.2.1)
问题

新窗口从父窗口打开Webchimera模块已加载,但由于其窗口对象指向父窗口而崩溃。那是因为NW.js

家长

<div id="chimera_container"></div>

...
var gui; // global variable for parent object
var wjs; // global variable for parent object
....
gui = require('nw.gui');
wjs = wjs = require('wcjs-player');
....
video_emit = gui.Window.open('video_emit.html',{
    "window" : {
        "focus": true,
        "toolbar": true,
        "frame": true,
        "width" : 640,
        "height" : 640
    },
    "dependencies": {
        "require-new": "^1.1.0",
        "wcjs-player": "^0.5.6"
    },  
});
#document
...
<div id="chimera_container"></div>
#document
...
<div id="chimera_container"></div>  
父控制台输出

<div id="chimera_container"></div>

...
var gui; // global variable for parent object
var wjs; // global variable for parent object
....
gui = require('nw.gui');
wjs = wjs = require('wcjs-player');
....
video_emit = gui.Window.open('video_emit.html',{
    "window" : {
        "focus": true,
        "toolbar": true,
        "frame": true,
        "width" : 640,
        "height" : 640
    },
    "dependencies": {
        "require-new": "^1.1.0",
        "wcjs-player": "^0.5.6"
    },  
});
#document
...
<div id="chimera_container"></div>
#document
...
<div id="chimera_container"></div>  
不工作。同样的问题,就好像我根本没有解决一样。仍然指向父窗口对象

使用需要新模块

这段代码没有科学性,我加载模块并在视频窗口中使用它

var req_new = require('require-new');
wjs = req_new('wcjs-player');
仍然崩溃


我知道,将**new instance:true**添加到父级的
gui.Window.open
会解决这个问题,但我需要父级和新窗口通过全局变量进行通信,只有在它们位于同一渲染上时才可用

------------------------解决方案-----------------------------------------

单独清除缓存不起作用,为了使其有效,必须从父窗口执行以下操作

在子窗口中:

clearRequiredModules(); //Same code as protrayed above
wjs = require('wcjs-player');  
video_container = new wjs("#vid");
video_emit = gui.Window.open('video_emit.html',{
        "window" : {
            "focus": true,
            "toolbar": true,
            "frame": true,
            "width" : 640,
            "height" : 640
        },
        "new-instance" : true // <---- That right there
    });
问题

注意,这样做会导致全局变量(用于在窗口之间共享信息的变量,无用。因此,这与在父窗口中执行此操作相同(并且不清除子窗口中的缓存):

video\u emit=gui.Window.open('video\u emit.html'{
“窗口”:{
“聚焦”:没错,
“工具栏”:正确,
“框架”:正确,
“宽度”:640,
“高度”:640
},

“新实例”:true//
wcjs播放器
严重依赖于能够访问
窗口。文档
NW.js
v0.12.3有一个bug,将父窗口
对象泄漏给子窗口

我看到这会产生许多问题,包括将所有错误和日志推送到主窗口,并且它总是会破坏依赖于
窗口的所有模块

解决方法是在创建子窗口后重新加载node.js级别的子窗口

用法示例:

var new_win = require('nw.gui').Window.open('index.html');
new_win.on('document-start', function() {
    new_win.reload(3);
});
注意事项:

var new_win = require('nw.gui').Window.open('index.html');
new_win.on('document-start', function() {
    new_win.reload(3);
});
  • 还应该提到的是,这是一个
    NW.js
    特定的问题,
    Electron
    在新窗口中没有这个问题

  • 由于这是一个高级页面重新加载,因此
    全局
    对象也将被清除,将信息传递到此新窗口的另一种解决方案是使用
    本地存储
    (这是持久的),或针对更复杂的需要使用WebSocket

尝试“触摸”服务器上的所有js文件。“清除缓存”并不像应该的那么简单。我意识到,在浏览器上不清除缓存,而是“触摸”缓存的服务器文件实际上会更改缓存在服务器上的源文件的日期和时间(在Edge、Chrome和Firefox上测试)大多数浏览器会自动下载服务器上最新的内容(代码、图形和多媒体)。我建议你只需在服务器上复制最新的脚本,然后“做触摸的事情”解决方案,因此它会将所有问题文件的日期更改为最新日期和时间,然后将新副本下载到浏览器:

 <?php
    touch('/www/sample/file1.js');
    touch('/www/sample/file2.js');
    touch('/www/sample/file2.js');
 ?>  

然后…剩下的程序


我花了一些时间来解决这个问题(因为许多浏览器对不同的命令有不同的行为,但它们都会检查文件的时间,并与您在浏览器中下载的副本进行比较,如果不同的日期和时间,将进行刷新),如果你不能走正确的路,总会有另一个更好的解决办法。致以问候,祝你野营愉快。顺便说一句,touch();或替代方案适用于多种编程语言,包括javascript bash sh php,您可以在html中包含或调用它们。

谢谢您的帮助!我现在正在尝试让Electron切换。如果您将失去通过全局变量进行交互的能力这一事实添加到这个答案中,我会将其标记为答案。