You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Re-use current tab instead of open a new one #1400
For Features; What is the motivation and/or use-case for the feature?
Inspired on create-react-app solution.
Since I usually use Google Chrome, I expect to stay in the same (current) browser tab
when re-launch webpack-dev-server, instead of use a new one and close those more.
Just curious if the Webpack people/ecosystem are interested in having this behaviour (will PR in case), since is very specific to Chrome browser in OSX platform.
Furthermore, the solution requires to have an extra file .applescript in the package.
Just in case somebody else stumbles upon this ticket and wonders "should I use webpack-serve"? webpack-serve is now deprecated!
DEPRECATED. Please use webpack-dev-server (he is in support and update mode again). Feel free to create a issue if some features are not implemented in webpack-dev-server.
Would it be possible for the devServer.open property to accept a function? Maybe then the developer could write a function to check if a active session already exists. Possibly using the tabs.query() method.
I have an idea for a more cross platform solution - we could add a shortcut (R - reload) for restart webpack from terminal. Similar to how it works in jest - by press R webpack restarting and refresh the already opened page.
Because we know that webpack already run open and openPage options will not triggered again.
There is a holdover solution for people waiting on this feature to be implemented into webpack-dev-server. The way create-react-app does it is with a react-dev-utils script called openBrowser. Importing this function into a webpack after function with the parameter of the host and port, works perfectly.
// webpack.config.js or webpack.config.babel.js import openBrowser from "react-dev-utils/openBrowser"; ... devServer: { ..., after: () => { openBrowser("http://localhost:8080"); }, ... }
There is a holdover solution for people waiting on this feature to be implemented into webpack-dev-server. The way create-react-app does it is with a react-dev-utils script called openBrowser. Importing this function into a webpack after function with the parameter of the host and port, works perfectly.
// webpack.config.js or webpack.config.babel.js import openBrowser from "react-dev-utils/openBrowser"; ... devServer: { ..., after: () => { openBrowser("http://localhost:8080"); }, ... }
Hey,I fount it work for me in webpack 5 like this:
// you could install react-dev-utilsimportopenBrowserfrom"react-dev-utils/openBrowser";// webpack devServer config{devServer: {host: '0.0.0.0',port: 8080,hot: false,onListening: function(devServer){if(!devServer){thrownewError('webpack-dev-server is not defined');}constaddr=devServer.server.address();openBrowser(`http://${addr.address}:${addr.port}`);},}}
good luck
Reacted by vladislav
Reacted by Mingwei, Vladyslav Fomenko and trend-henry-chang
After going through all the suggestions, I came across a solution that works for me.
Note that I’m using Firefox Developer Edition on a Linux Ubuntu machine.
Here’s what I did to make it work:
Create a directory named webpack at the root of your project.
Inside the webpack directory, create another directory named scripts.
Inside the scripts directory, create a shell script file (let’s call it lookup_tabs_names.sh).
Add the following code:
#!/bin/bash
tabName=$1
found=false
window_ids=()
tab_titles=()
# Search for browser by name and store ID in an array
window_ids+=("$(xdotool search --name "Firefox Developer Edition")")
forwindowIDin"${window_ids[@]}";do# Activate browser window
xdotool windowactivate --sync "$windowID"# Get the initial tab title
initial_tab_title=$(xdotool getactivewindow getwindowname | awk -F '—''{print $1}')
tab_titles+=("$initial_tab_title")
# Cycle through tabs and get their titleswhiletrue;do# Switch to the next tab
xdotool key --clearmodifiers ctrl+Tab
sleep 0.1 # Wait for the tab to switch# Get the current tab title
current_tab_title=$(xdotool getwindowfocus getwindowname | awk -F '—''{print $1}')# Trim whitespace and convert to lowercase
current_tab_title=$(echo "${current_tab_title}"| xargs)
initial_tab_title=$(echo "${initial_tab_title}"| xargs)
tabName=$(echo "${tabName}"| xargs)# Check if the current tab matches the one we needif [[ "${current_tab_title,,}"=="${tabName,,}" ]];then
found=true
xdotool key --window "$current_tab_title""ctrl+r"breakfi# Check if we've cycled back to the initial tabif [[ "${current_tab_title,,}"=="${initial_tab_title,,}" ]];then
found=false
breakfidoneif$found;thenbreakfidone# Output the resultecho$found
Inside the scripts directory, create another shell script named webpack_dev_server_tab_manager.sh.
Add the following code:
#!/bin/bash# Main script
browser=$1
tabName=$2# Check if a browser is specifiedif [[ -z$browser ]];thenecho"Please specify the browser: chrome, firefox, or another browser name."exit 1
fi# Check if the specified browser is runningif! pgrep -x "$browser"> /dev/null;thenechofalseexit 0
fi# Look for the tab running Webpack DevServer in the specified browser
result=$(bash ./webpack/scripts/lookup_tab_with_url.sh "$tabName")echo"$result"
Inside the webpack directory, create a new directory called middlewares.
Add the following code:
const{ exec }=require('child_process');functionopenBrowserMiddleware(){returnnewPromise((resolve)=>{constbrowser='firefox-bin';// 'firefox', 'chrome', or the name of your browserconsttabTitle='Webpack React TS';exec(`bash ./webpack/scripts/webpack_devserver_tab_manager.sh "${browser}" "${tabTitle}"`,(error,stdout,stderr)=>{if(error){console.error(`Error executing script: ${error.message}`);resolve(false);return;}if(stderr){console.error(`Script stderr: ${stderr}`);}resolve(stdout.trim()==='true');});});}module.exports=openBrowserMiddleware;
Inside your webpack.config.js, as mentioned by @hcw2175, add the following code:
constopenBrowser=require('react-dev-utils/openBrowser');constopenBrowserMiddleware=require('./webpack/middlewares/open_browser_middleware');// Webpack DevServer configuration{devServer: {open: false,onListening: asyncfunction(devServer){const{ port }=devServer.server.address();constURL=`http://localhost:${port}`;constisOpen=awaitopenBrowserMiddleware();if(!isOpen){openBrowser(URL);}},}}
You'll need to adapt this setup to your specific needs (e.g., using import instead of require, installing xdotool, etc.).
Let me know what you think!
Happy Coding! 🚀
P.S. Special thanks to @hcw2175 and all the programmers whose posts I read on the web to make this work for me.
Activity
alexander-akait commentedon May 7, 2018
elrumordelaluz commentedon May 7, 2018
alexander-akait commentedon May 7, 2018
elrumordelaluz commentedon May 7, 2018
alexander-akait commentedon May 7, 2018
@elrumordelaluz yes, here issue webpack/webpack.js.org#2069
flekschas commentedon Nov 1, 2018
Just in case somebody else stumbles upon this ticket and wonders "should I use webpack-serve"?
webpack-serveis now deprecated!alexander-akait commentedon Nov 1, 2018
feel free to send a PR
flekschas commentedon Nov 1, 2018
Just as a reference, @elrumordelaluz submitted a PR to
webpack-serveregarding this matter: webpack-contrib/webpack-serve#117Since it's much more involved than I thought it would be I am not sure it's simply to copy it over to
webpack-dev-server. :)alexander-akait commentedon Nov 1, 2018
@flekschas feel free to send a PR
1 remaining item
speaud commentedon Feb 26, 2020
Would it be possible for the
devServer.openproperty to accept a function? Maybe then the developer could write a function to check if a active session already exists. Possibly using the tabs.query() method.alexander-akait commentedon Mar 31, 2020
@atilkan PR welcome if you have idea how to implement it
Akiyamka commentedon May 15, 2020
I have an idea for a more cross platform solution - we could add a shortcut (R - reload) for restart webpack from terminal. Similar to how it works in jest - by press R webpack restarting and refresh the already opened page.
Because we know that webpack already run
openandopenPageoptions will not triggered again.ckken commentedon Jun 29, 2020
mark
kaspar-p commentedon Jul 14, 2020
There is a holdover solution for people waiting on this feature to be implemented into webpack-dev-server. The way create-react-app does it is with a react-dev-utils script called openBrowser. Importing this function into a webpack
afterfunction with the parameter of the host and port, works perfectly.// webpack.config.js or webpack.config.babel.jsimport openBrowser from "react-dev-utils/openBrowser";...devServer: { ..., after: () => { openBrowser("http://localhost:8080"); }, ... }With one other caveat, the .applescript file needs to be copied into the root of your project. Information about that script and where to find these files here: https://stackoverflow.com/questions/48913566/how-does-create-react-app-re-use-an-existing-browser-tab-after-running-npm-run/48915952
Akiyamka commentedon Jul 16, 2020
@Kaspar78 it's work only for mac platform
ckken commentedon Jul 17, 2020
fixed it
Open in same browser tab
white-than-wood commentedon Jun 5, 2022
It seems to only apply to the mac platform, and create-react-app does the same: https://github.com/facebook/create-react-app/blob/25184c4e91ebabd16fe1cde3d8630830e4a36a01/packages/react-dev-utils/openBrowser.js#L65-L86
alexander-akait commentedon Jun 5, 2022
Yes PR welcome
hcw2175 commentedon Oct 31, 2022
Hey,I fount it work for me in webpack 5 like this:
good luck
tarunsankhla commentedon Apr 20, 2024
is the issue closed?
alexander-akait commentedon Apr 22, 2024
No
yohDone commentedon Feb 9, 2025
Yo!
After going through all the suggestions, I came across a solution that works for me.
Note that I’m using Firefox Developer Edition on a Linux Ubuntu machine.
Here’s what I did to make it work:
webpackat the root of your project.webpackdirectory, create another directory namedscripts.scriptsdirectory, create a shell script file (let’s call itlookup_tabs_names.sh).scriptsdirectory, create another shell script namedwebpack_dev_server_tab_manager.sh.webpackdirectory, create a new directory calledmiddlewares.webpack.config.js, as mentioned by @hcw2175, add the following code:You'll need to adapt this setup to your specific needs (e.g., using
importinstead ofrequire, installingxdotool, etc.).Let me know what you think!
Happy Coding! 🚀
P.S. Special thanks to @hcw2175 and all the programmers whose posts I read on the web to make this work for me.