Feature Request: Add 'open' as an option.

Open
#28 10 comments 20 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
58/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
javascript
Domain
devtools

Research direction

Start in server.js, especially the DEFAULT_OPTIONS definition and the get server() listening handler shown in the issue. Trace how server options are updated while the server runs and determine how the browser should open only when startup requires it. Done means an open option is supported without adding an unnecessary dependency or reopening the browser after the option changes.

Written by the indexing model from the issue text.

Description

enhancement needs-votes

The previous BrowserSync dev server had an open: true option that would open the browser window when the server started.

Would be nice to have that added back in. This horrible hack (nearly) works:

// server.js
const DEFAULT_OPTIONS = {
...
 open: false,           // Open URL in browser on launch.
...

and

// server.js → get server()
...
this._server.on("listening", (e) => {
  this.setupReloadNotifier();
  let { port } = this._server.address();

  let hostsStr = "";
  if(this.options.showAllHosts) {
    // TODO what happens when the cert doesn’t cover non-localhost hosts?
    let hosts = devip().map(host => `${this._serverProtocol}//${host}:${port}${this.options.pathPrefix} or`);
    hostsStr = hosts.join(" ") + " ";
  }

  this.logger.info(`Server at ${hostsStr}${this._serverProtocol}//localhost:${port}${this.options.pathPrefix}${this.options.showVersion ? ` (v${pkg.version})` : ""}`);

  if(this.options.open) {
    this.logger.info(`Opening in browser...`);
    require('out-url').open(`${hostsStr}${this._serverProtocol}//localhost:${port}${this.options.pathPrefix}`);
  }

});


return this._server;
...

But adds a dependency and if you change the open option from true to false in .eleventy.js while the server is running it opens the browser again. I'm sure there is a much better way to do this.

Dominant language
JavaScript
Stars
109
Forks
20
Avg merge
1m
Merged PRs (30d)
1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from 11ty/dev-server

All issues in 11ty/dev-server

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.