-
-
Save nonsensecreativity/e8000d34ae7470e0a5ad9e8f5267c556 to your computer and use it in GitHub Desktop.
webpack proxy for wordpress
This file contains hidden or 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
| config.devServer = { | |
| proxy: { | |
| //here we declare to proxy all the requests not handled by webpack. I this case everything webpack bundle is in | |
| // dist/ and so we don't proxy it to let the webpack dev server handle it | |
| '!**/dist/**': { | |
| //set the URL of the wordpress site | |
| target: 'http://mysite.test', | |
| changeOrigin: true, | |
| onProxyRes: function(proxyRes, req, res) { | |
| if( proxyRes.headers && | |
| proxyRes.headers[ 'content-type' ] && | |
| proxyRes.headers[ 'content-type' ].match( 'text/html|application/json' ) ) { | |
| var _write = res.write, _writeHead = res.writeHead; | |
| res.writeHead = function(){ | |
| if( proxyRes.headers && proxyRes.headers[ 'content-length' ] ){ | |
| res.removeHeader('content-length'); | |
| } | |
| // This disables chunked encoding | |
| res.setHeader( 'transfer-encoding', '' ); | |
| // Disable cache for all http as well | |
| //res.setHeader( 'cache-control', 'no-cache' ); | |
| _writeHead.apply( this, arguments ); | |
| }; | |
| res.write = function (data) { | |
| //set again the domain of the wordpress site to replace js, css, images paths to the webpack dev server | |
| const newdata = data.toString().replace(/mysite\.test/g, req.headers.host); | |
| _write.call(res, newdata); | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment