BrowserSync / browser-sync

Keep multiple browsers & devices in sync when building websites. https://browsersync.io
https://discord.gg/2d2xUThp
Apache License 2.0
12.17k stars 754 forks source link

Dynamic proxy change during runtime #1354

Open kettanaito opened 7 years ago

kettanaito commented 7 years ago

Issue details

I would like to know whether it is possible to change BrowserSync proxy option on runtime. My research brought me here. However, the mentioned example does not seem to be working, created instance of BrowserSync doesn't have subscribe and setOpion methods.

Please specify which version of Browsersync, node and npm you're running

Affected platforms

BrowserSync usage (Gulp):

const browserSync = require('browser-sync').create('My project');

...

gulp.task('serve', () => {
    browserSync.init({
        proxy: 'localhost',
        port: 8080,
        files: baseDir,
        open: false,
        middleware: [
            // custom middleware to change proxy address depending on URL
            (req, res, next) => {
                const languages = ['en', 'de', 'pl'];
                const channel = req.url.match(/(?!\/)(\w+)(?=\/)/);

                if (channel && languages.indexOf(channel[0]) != -1) {
                    const target = `${channel[0]}.hippo.local`;
                    // should change `proxy` option here
                    browserSync.setOption('proxy', existing => target);
                }

                next();
            }
        ]
    });
});

Would be thankful for help whether this is possible to achieve. Thank you.

viktorlarsson commented 6 years ago

Tried this as well, any updates here?

kettanaito commented 6 years ago

@viktorlarsson so far no updates at all.