** 更新了硝基插件方法 **
我使用Playwright服务器端从我想要在前端使用的页面获取一些数据,这是使用硝基插件的设置
页面index.vue服务器db index.js插件playwright.js路由playwright.js .
server/db/index.js导出const = [];
server/routes/playwright.js从‘./ db’导入{db};导出默认的defineEventHandler(() => db);
server/plugins/playwright.js从‘剧作家’导入{ for };导出默认的defineNitroPlugin(异步() => { console =等待chromium.launch();// . //某些操作在这里//,一些控制台日志用于跟踪进度// .Db.push(结果);//结果是刮擦的数据};
在运行nuxt dev时,plugins/playwright中的脚本将启动、打开浏览器、抓取数据并将其存储到db ,并将其与登录到终端(在prod中不同)的所有记录下来。
当我打开本地主机时,我得到索引页,从db获取数据,没有错误。
但是,在运行nuxt generate时,CLI将像往常一样运行,我只看到一些打印到终端的,如下所示
yarn run v1.22.19
$ nuxt generate
Nuxi 3.0.0
Nuxt 3.0.0 with Nitro 1.0.0
WARN Using experimental payload extraction for full-static output.
You can opt-out by setting experimental.payloadExtraction to false.
i Client built in 1666ms
i Building server...
√ Server built in 581ms
√ Generated public .output/public
i Initializing prerenderer
Starting Playwright server plugin
⚙️ Read User-specifed options
Initiating a new page
// There are more console.logs than these three
i Prerendering 3 initial routes with crawler
├─ / (290ms)
├─ /200.html (3ms)
├─ /404.html (5ms)
├─ /_payload.js (2ms)
√ You can now deploy .output/public to any static hosting!
Done in 6.21s.
* Terminal will be reused by tasks, press any key to close it. 在运行nuxt preview和打开本地主机时,我将得到一个空数组(db的初始值)的索引页。
我是否需要以某种方式强迫generate命令等待硝基插件完成执行?我怎么能做到呢?
发布于 2022-12-02 14:07:02
问题是在剧作家包中,而不是在nuxt本身中,在让剧作家开始工作并使用了正确的路径之后,应用程序就正常工作了。
发布于 2022-11-18 14:11:27
我不确定这是否有帮助,但您可以使用“准备”挂钩来解决这个问题:
首先,创建一个这样的文件(./hooks/hoks.js):
export default (nuxtConfig) => ({
ready: () => {
// Execute your code here
}
});然后将其添加到nuxt.config.js中:
import hooks from './hooks/hooks';
export default {
// Other stuff
hooks: hooks(this),
// Other stuff
}https://stackoverflow.com/questions/74490863
复制相似问题