Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto open browser when run dev #1433

Closed
thangman22 opened this issue Aug 21, 2017 · 9 comments

Comments

Projects
None yet
5 participants
@thangman22
Copy link

commented Aug 21, 2017

Nuxt should auto browser when start dev command

This question is available on Nuxt.js community (#c1275)
@yezongyang

This comment has been minimized.

Copy link

commented Aug 21, 2017

It should be optional config

@Atinux

This comment has been minimized.

Copy link
Member

commented Aug 21, 2017

Hi @thangman22

I don't like this behavior since it's too intrusive, the url is displayed into the terminal and it's good enough :)

@Atinux Atinux closed this Aug 21, 2017

@sosout

This comment has been minimized.

Copy link

commented Jun 12, 2018

@thangman22 Hi, I opened the browser through express and OPN plug-ins.
package.json:

image

server.js:

const opn = require('opn');
const { Nuxt, Builder } = require('nuxt');
const app = require('express')();
const port = process.env.PORT || 8888;

// 传入配置初始化 Nuxt.js 实例(config and init)
const config = require('../nuxt.config.js');

const nuxt = new Nuxt(config);
app.use(nuxt.render);

// 在开发模式下进行编译(Compile in the development mode)
if (config.dev) {
  new Builder(nuxt).build()
  .catch((error) => {
    console.log(error);
    process.exit(1);
  })
}

// 监听指定端口(Listen on port)
app.listen(port, 'localhost', function () {
  console.log('成功开启'+ port +'端口');
  var url = 'http://localhost:' + port;
  console.log('> 服务器运行于 ' + url + '\n');
  opn(url);
})

This is a proposal I recommend!

This comment has been minimized.

Copy link
Member

commented Jun 12, 2018 — with CMTY

You can add this feature in the nuxt.config.js using hooks and the opn module:

const opn = require('opn')

module.exports = {
  hooks: {
    listen(server, { host, port }) {
      opn(`http://${host}:${port}`)
    }
  }
}
@cmty

This comment has been minimized.

Copy link

commented Jun 12, 2018

This question has been resolved by @Atinux, see answer.

@cmty cmty bot added the cmty:status:resolved label Jun 12, 2018

@sosout

This comment has been minimized.

Copy link

commented Jun 12, 2018

@Atinux Thank you. But it's not perfect enough. when nuxt.config.js has not been modified, if you pressed Ctrl + s, it will trigger. And each time is triggered once a time.

This comment has been minimized.

Copy link

commented Aug 31, 2018 — with CMTY

server.js
if (config.dev){
config.build.plugin = [ new OpenBrowserPlugin({http://${host}:${port}})]
}
still the same problem

ctrl + s it will trigger .

another problem : when I start the server ,it open the browser twice.

my question is how to make the server.js don't be watched.

another is : solve it trigger twice.

This comment has been minimized.

Copy link

commented Sep 5, 2018 — with CMTY

@thangman22 I have solve this problem .
package.json
"scripts":{
"dev" : "backpack dev"
}
"devDependencies":{
backpacke-core:"^0.7.0"
}
backpack.config.js:
const OpenBrowserPlugin = require('open-browser-webpack-plugin')
module.exports = {
webpack:(config, options, webpack) => {
config.entry.main = './server/index.js'
const openBrowser = new OpenBrowserPlugin({
url: http://localhsot:3000 ,
delay: 20*1000
})
config.plugins.push(openBrowser)
return config
}
}

@lock

This comment has been minimized.

Copy link

commented Oct 31, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 31, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.