-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
133 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Proxy: hot reload | ||
|
||
```shell | ||
node ../../bin/webpack-dev-server.js --open | ||
``` | ||
|
||
Enables hot reloading for proxy config. If function is provided instead of | ||
object, dev server calls it on each request to get proxy config and replaces proxy middleware if config was changed. | ||
|
||
## What should happen | ||
|
||
The script should open `http://localhost:8080/`. It should show "It's working." | ||
|
||
Go to `http://localhost:8080/api/users`. It should show a couple of JSON objects. | ||
|
||
While dev server is running, open `proxy-config.js` and replace | ||
```js | ||
module.exports = { | ||
target: 'http://jsonplaceholder.typicode.com/', | ||
pathRewrite: { | ||
'^/api': '' | ||
} | ||
}; | ||
``` | ||
with | ||
```js | ||
module.exports = { | ||
target: 'http://reqres.in/' | ||
}; | ||
``` | ||
|
||
Now `http://localhost:8080/api/users` should return a response from `http://reqres.in/`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
document.write("It's working."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="bundle.js" type="text/javascript" charset="utf-8"></script> | ||
</head> | ||
<body> | ||
<h1>Example: proxy - hot reload</h1> | ||
<p>Change config in <code>proxy-config.js</code> and open <a href="/api/users">/api/users</a></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/**/ | ||
module.exports = { | ||
target: 'http://jsonplaceholder.typicode.com/', | ||
pathRewrite: { | ||
'^/api': '' | ||
} | ||
}; | ||
/**/ | ||
|
||
// | ||
// Replace it with following and save the file: | ||
// | ||
|
||
/** / | ||
module.exports = { | ||
target: 'http://reqres.in/' | ||
}; | ||
/**/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var fs = require('fs'); | ||
|
||
var proxyConfig = require('./proxy-config'); | ||
var proxyOptions = { | ||
context: '/api', | ||
target: proxyConfig.target, | ||
pathRewrite: proxyConfig.pathRewrite, | ||
changeOrigin: true | ||
}; | ||
|
||
fs.watch('./proxy-config.js', function() { | ||
delete require.cache[require.resolve('./proxy-config')]; | ||
try { | ||
var newProxyConfig = require('./proxy-config'); | ||
if(proxyOptions.target !== newProxyConfig.target) { | ||
console.log('Proxy target changed:', newProxyConfig.target); | ||
proxyOptions = { | ||
context: '/api', | ||
target: newProxyConfig.target, | ||
pathRewrite: newProxyConfig.pathRewrite, | ||
changeOrigin: true | ||
}; | ||
} | ||
} catch(e) {} | ||
}); | ||
|
||
module.exports = { | ||
context: __dirname, | ||
entry: "./app.js", | ||
devServer: { | ||
proxy: [ | ||
function() { | ||
return proxyOptions; | ||
} | ||
] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters