GithubHelp home page GithubHelp logo

Comments (4)

shakyShane avatar shakyShane commented on August 20, 2024

The check for something like Accept: text/html is just one attempt at verifying that the current request is HTML and can/should be overwritten. It's a simple 'filter' that will exclude things like binary files, JS, CSS etc.

Are there any possibilities to inject the script for requests with not Accept: html header? In other words to disable this check?

You might want to take a look at resp-modifier instead. This module basically 'steals' all of the logic from this one with some added features (that I needed myself for Browsersync). For example, what you're really suggesting in your op is the idea of a whitelist - where the response is overwritten regardless.

var resp = require('resp-modifier');
var mw = resp({
    rules: [
         {
              match: "some string",
              replace: "some other string",
              paths: ["**/*.html"] // whitelist all html files, skips all checks
         }
    ]
});

app.use(mw)

from connect-livereload.

yan-foto avatar yan-foto commented on August 20, 2024

@shakyShane: thanks, I appreciate your help. I came up across this module as I was trying to migrate from all the complexity of Browsersync. But it seems that the logic is the same and the underlying concept also. If I got it right the resp-modifier is just a middleware and does not do all the server/watch/reload magic, right?

Another thing: to see if the script can actually be injected, I would check the response delivered by the server to see if its actually HTML instead of reading the request headers. However, I might be missing something here.

And finally: I want to make use of this module in electron and due to a bug the request headers does not contain Accept. My workaround was to use a middleware and add Accept to all *.html requests, but I also had no success though (also not in case of Browsersync since user middlewares are run after resp-modifier).

from connect-livereload.

shakyShane avatar shakyShane commented on August 20, 2024

yep, resp-modifier is just a simple middleware for modifying responses, it has no direct relationship to Browsersync

Another thing: to see if the script can actually be injected, I would check the response delivered by the server to see if its actually HTML instead of reading the request headers. However, I might be missing something here.

Yep, we do that too - the headers check is just 'first line of defense'

also not in case of Browsersync since user middlewares are run after resp-modifier)

You can force your middlewares to the 'top of the stack' in Browsersync quite easily

var browserSync = require('browser-sync').create();

browserSync.init({
    server: './app'
}, function (err, bs) {
    bs.addMiddleware("*", function (req, res, next) {
        res.setHeader('accept', 'text/html');
        next();
    }, {override: true});
    // override ensures your middleware always runs before any internals
});

from connect-livereload.

yan-foto avatar yan-foto commented on August 20, 2024

@shakyShane thanks for the quick Browsersync tutorial 👍!

I still think that checking the headers is little bit too restrictive, what about Accept: */*? Or accepts with multiple values (e.g. the infamous image/webp,*/*;q=0.8 sent with all chrome requests). Nonetheless, I must confess that I have no better solution for now! I might as well close this issue (or should it leave it open as feature request?).

from connect-livereload.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.