如何防止 Meteor 观看文件? [英] How to prevent Meteor from watching files?

查看:20
本文介绍了如何防止 Meteor 观看文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Dojo Toolkit流星.

  1. 我首先将整个 Dojo Toolkit 树复制到 /public

然后,我将它包含在客户端:

Then, I include it on the client side with:

<script src="/dojo/dojo.js" data-dojo-config="async: true"></script>`

一切正常,除了 Meteor 实际上监视 /public 中的每个文件是否有更改,以便它可以重新启动服务器.这实际上在 localhost:3000 上的第一个请求期间导致了很长时间的延迟.

Everything works fine, except Meteor is actually monitoring every single file in /public for changes, so that it can restart the server. This is actually causing a very long delay during the first request on localhost:3000.

有没有办法阻止 Meteor 监视某个目录下的文件?

Is there a way of preventing Meteor from watching files from a certain directory?

Dojo Toolkit 有 10k+ 个文件,所以我收到了 EMFILE 错误声明 这里,用

Dojo Toolkit is 10k+ files so I get the EMFILE error stated here, corrected with

sudo sh -c 'echo 16384 > /proc/sys/fs/inotify/max_user_watches'

推荐答案

意识到这与以下内容重复:使用 Meteor 生成和提供静态文件

realised this is duplicate to: generating and serving static files with Meteor

参见:https://github.com/meteor/meteor/issues/437

这对我来说是个大问题.我必须提供大约 12000 个静态图像,我最初将它们放入公共文件夹中.这导致节点不断地使用近 100% 的一个 CPU 内核.由于内存有限,应用程序崩溃.

This was a major problem for me. I have to serve ~12000 static images, which I initially put into the public folder. This caused node to use nearly 100% of one CPU core, constantly. With limited memory the app crashes.

我目前使用的解决方法

  • 创建文件夹 public/.#static/ 并将所有静态资产移动到其中.该文件夹不被流星监视
  • 使用静态前缀 (/img/cat.png ->/static/img/cat.png)
  • 安装 mime npm 包

  • create the folder public/.#static/ and move all static assets into it. This folder isn't watched by meteor
  • prefix urls with static (/img/cat.png -> /static/img/cat.png)
  • install the mime npm package

cd ~/.meteor/tools/latest/lib/node_modules/
npm install mime

  • 创建一个 rawConnectionHandler 来为资产提供服务(归功于:https://stackoverflow.com/a/20358612)服务器/static_files_handler.coffee

  • create a rawConnectionHandler to serve the assets (credits to: https://stackoverflow.com/a/20358612) server/static_files_handler.coffee

    fs = Npm.require('fs')
    mime = Npm.require('mime')
    WebApp.rawConnectHandlers.use (req, res, next) ->
      re = /^/static/(.*)$/.exec(req.url)
      if re isnt null # Only handle URLs that start with /static/*
        filePath = process.env.PWD + "/public/.#static/" + re[1]
        type = mime.lookup(filePath)
        data = fs.readFileSync(filePath, data)
        res.writeHead 200,
          "Content-Type": type
    
        res.write data
        res.end()
      else # Other urls will have default behaviors
        next()
      return
    

  • 这种方法的局限性:

    • 不适合提供带有查询参数的资产.正则表达式还将匹配/static/html/image.html?src=/static/img/cat.png 尝试提供文件名包括参数的文件.这很容易改变.
    • Meteor 完全不知道这些文件,因此它们不会包含在 appcache 清单中.如果您想让它们离线可用,请查看我添加到 https://github.com 的 addPaths 选项/buildhybrid/appcache-extra

    如果您不想解决这些问题,请考虑从外部服务(例如 AWS S3)提供资产.

    If you don't want to work around the problems, consider serving the assets from an external service (ex. AWS S3).

    这篇关于如何防止 Meteor 观看文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆