Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

dojo - How to prevent Meteor from watching files?

I would like to use Dojo Toolkit with Meteor.

  1. I first copy the whole Dojo Toolkit tree in /public

  2. Then, I include it on the client side with:

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

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.

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


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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

see: https://github.com/meteor/meteor/issues/437

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.

The workaround I'm using for the moment

  • 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
    
  • 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
    

Limitations of this approach:

  • not suitable to serve assets with query parameters. The regex would also match /static/html/image.html?src=/static/img/cat.png trying to serve a file with the filename including the parameters. This is easy to change.
  • Meteor is completely unaware of the files, they therefore don't get included into the appcache manifest. If you want to make them available offline, check out the addPaths option I added to https://github.com/buildhybrid/appcache-extra

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...