Skip to content

Instantly share code, notes, and snippets.

@rformato
Last active February 9, 2024 13:50
Show Gist options
  • Save rformato/15d76238615de8c34ff9cf05f4a39e6e to your computer and use it in GitHub Desktop.
Save rformato/15d76238615de8c34ff9cf05f4a39e6e to your computer and use it in GitHub Desktop.
webpack proxy for wordpress
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