napcs/node-livereload

feature request: support CORS for hosts

Closed this issue · 1 comments

Use case:

  • Static Java application with it's own serving logic and host (dev.foo.bar)
  • Frontend Development & LiveReload server running in parallel, to simplify frontend development (host: localhost)

We point to live-reload script and getting CORS error.

Desired behavour:

LiveReload server has option, to configure "allowed" host.

  • we could add "if" condition to check for "origin" value, and apply CORS headers if exists.
  • config.origin is full host: https://foo.bar:80
diff --git a/node_modules/livereload/lib/livereload.js b/node_modules/livereload/lib/livereload.js
index 512f7bf..c68ff2e 100644
--- a/node_modules/livereload/lib/livereload.js
+++ b/node_modules/livereload/lib/livereload.js
@@ -237,9 +237,13 @@
     }
     requestHandler = function(req, res) {
       if (url.parse(req.url).pathname === '/livereload.js') {
-        res.writeHead(200, {
-          'Content-Type': 'text/javascript'
-        });
+        const headers = {
+          'Content-Type': 'text/javascript',
+          'Access-Control-Allow-Methods': 'GET, OPTIONS',
+          'Access-Control-Allow-Origin': config.origin
+        };
+        // corsDomains
+        res.writeHead(200, headers);
         return res.end(fs.readFileSync(require.resolve('livereload-js')));
       }
     };
napcs commented

See #155. Discussing this there.