webpack-dev-middleware icon indicating copy to clipboard operation
webpack-dev-middleware copied to clipboard

Response Callback

Open full-stop opened this issue 6 years ago • 9 comments

I used it for the first time, and a lot of it was not very clear, but it was very good, thank you for providing this middleware.

  • Operating System: windows7
  • Node Version:8.9.4
  • NPM Version:5.6.0
  • webpack version:3.11.0
  • webpack-dev-middleware Version:2.0.6
  • [x] This is a feature request
  • [ ] This is a bug

Code

My suggestion is can you add the support of template engine analysis in the middleware, or provide a callback function for us to use? For example, ejs, jade:

    var ejs = require('ejs');
    
 app.use(webpackDevMiddleWare(complier, {
        stats: {
            assets: true,
            children: false,
            modules: false,
            colors: true
        },
        respCallback: function(res, filename, content) {

            if (/\.html$/.test(filename)) {

                res.send(ejs.render(content.toString(), app.locals));

            } else {
                if (res.send) res.send(content);
                else res.end(content);
            }
        }
    }));

middleware.js#85line

const { respCallback } = context.options;
if (respCallback && typeof respCallback === 'function') {
    respCallback(res, filename, content);
} else {
    if (res.send) res.send(content);
    else res.end(content);

}

full-stop avatar May 21 '18 04:05 full-stop

Thanks for the issue. In the future please fill out the entire issue template and don't remove sections.

This seems like a very niche feature and I'm not sure there would be a widespread advantage to users. We'll leave this issue open for some time and if there is a large upvoting of the request we'll consider adding it.

shellscape avatar May 27 '18 14:05 shellscape

Thanks for the reply, I'll pay attention to the template later, yes, the function of the template engine is very small, but I feel it is very meaningful to provide a response callback, and we can do some custom functions based on this callback.

full-stop avatar May 28 '18 02:05 full-stop

/cc @shenguotao2015

alexander-akait avatar Aug 21 '18 12:08 alexander-akait

I would like to see this feature aswell, plus having an extra feature can't hurt.

justablob avatar Oct 21 '18 16:10 justablob

/cc @justablob Do you want send a PR

alexander-akait avatar Dec 13 '18 09:12 alexander-akait

I can't think of a use case but do you need this feature that provides a callback?

hiroppy avatar Apr 10 '20 07:04 hiroppy

I used it for the first time, and a lot of it was not very clear, but it was very good, thank you for providing this middleware.

  • Operating System: windows7

  • Node Version:8.9.4

  • NPM Version:5.6.0

  • webpack version:3.11.0

  • webpack-dev-middleware Version:2.0.6

  • [x] This is a feature request

  • [ ] This is a bug

Code

My suggestion is can you add the support of template engine analysis in the middleware, or provide a callback function for us to use? For example, ejs, jade:

    var ejs = require('ejs');
    
 app.use(webpackDevMiddleWare(complier, {
        stats: {
            assets: true,
            children: false,
            modules: false,
            colors: true
        },
        respCallback: function(res, filename, content) {

            if (/\.html$/.test(filename)) {

                res.send(ejs.render(content.toString(), app.locals));

            } else {
                if (res.send) res.send(content);
                else res.end(content);
            }
        }
    }));

middleware.js#85line

const { respCallback } = context.options;
if (respCallback && typeof respCallback === 'function') {
    respCallback(res, filename, content);
} else {
    if (res.send) res.send(content);
    else res.end(content);

}

I would like to work on this feature. Can I have a little more details?

mitchell-frost avatar Jul 16 '20 13:07 mitchell-frost

@mitchell-frost Can you provide real use case?

alexander-akait avatar Jul 16 '20 14:07 alexander-akait

I'm running into a similar issue as outlined in this SO post.

In short, I'm using this indirectly with Angular's ng serve (WebPack dev server) and I need to be able to embed the URL that the dev-server received the request on into my index.html body so that I can calculate a relative href from it. (My user will serve this app from a folder I don't know the path of at build time).

bduncanj avatar Feb 03 '21 21:02 bduncanj