gpt4 book ai didi

dom - jsdom 窗口缓存

转载 作者:行者123 更新时间:2023-12-03 22:45:37 25 4
gpt4 key购买 nike

在 express.js 中使用 jsdom.jsdom() 我创建了一个带有一些“基本”布局标记的文档,并将一些客户端库(例如 jQuery)附加到其窗口。

只需进行一次此设置就好了。

问题是窗口文档的 DOM 会根据请求的 url 发生变化。有没有办法让每个请求从同一个缓存的窗口对象开始并增强它,还是必须在每个请求上从头开始设置?

最佳答案

听起来您想要一个简单的 JavaScript 对象哈希?

var cache = Object.create(null); // avoids spurious entries for `hasOwnProperty` etc.

// Incoming request happens, assume `req.url` is available...

if (req.url in cache) {
processDom(cache[req.url]);
} else {
jsdom.env(req.url, function (err, window) {
if (err) {
// handle error
return;
}
cache[req.url] = window;
processDom(cache[req.url]);
});
}

关于dom - jsdom 窗口缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7829334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com