首先,這與Wordpress redirecting to localhost instead of virtual host when being proxied by Webpack Dev Server有關。我面臨着類似的問題,但唯一的解決方案並沒有爲我做任何事情。當通過webpack-dev-server代理訪問WordPress時,WordPress重定向到siteurl
我在Vagrant開發機器中運行WordPress 4.7,它響應http://wordpress.local就像它應該。以前,我使用Browsersync觀看我的文件並觸發刷新,並按預期工作:browser-sync start --proxy 'https://wordpress.local' --files '**/dist/js/*.js, **/*.css, **/*.php'
。
但是,對於webpack-dev-server,我無法複製該行爲。這是應該發生的。
- 服務器在
https://localhost:9000
- 導航到
https://localhost:9000
應該用相同的頁面展示我作爲導航到https://wordpress.local
開始,沒有任何重定向。網站的工作原理爲https://wordpress.local
,但網址爲https://localhost:9000
。 - 發生變化,頁面重新加載。
而是發生這種情況。
- 導航到
https://localhost:9000
重定向我https://wordpress.local
有301我已經停用的規範與remove_filter('template_redirect', 'redirect_canonical');
重定向,但於事無補。 - 導航到
https://localhost:9000/404
爲我提供了一個由我的主題提供的404頁面。沒有重定向發生。 - 導航到
https://localhost:9000/existing-page/
重定向我https://localhost/existing-page/
有301
這到底是怎麼回事?我已經縮小問題到WordPress,如預期進行代理非WordPress的目錄作品:
直接的$ _ SERVER內容:https://gist.github.com/k1sul1/0aff7ba905464ca7852f2ce00b459922
已代理中,$ _ SERVER內容:https://gist.github.com/k1sul1/f090aa103dc3a3cb0b339269560ac19d
我我試着玩頭文件等,沒有運氣。下面是我的webpack.config.js樣子:
const path = require('path');
const url = 'https://wordpress.local/';
const themeDir = '/wp-content/themes/themename/';
module.exports = {
entry: './src/js/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: url
},
devServer: {
historyApiFallback: true,
compress: true,
port: 9000,
https: url.indexOf('https') > -1 ? true : false,
publicPath: themeDir,
proxy: {
'*': {
'target': url,
'secure': false
},
// '/': { // This doesn't do much.
// target: url,
// secure: false
// }
},
}
};
TL; DR:我如何複製與的WebPack-DEV-服務器Browsersync行爲,而WordPress的要瘋了?
但是這樣做,我失去訪問安裝非代理,這可能會導致問題的能力。我在dev和prod實例之間差不多完全平等,這會對平價造成很大影響;)我認爲Browsersync更像是一箇中間人服務器,它將我的請求發送到未經修改的WordPress,並重寫響應所以一切都繼續工作。我想我需要webpack-dev-server來做到這一點,或類似的。 – Christian
目前我只使用Browsersync並通過它運行Webpack,但我寧願通過開發服務器運行它,因爲它速度更快。 – Christian
顯然我可以使用wp-config更改'siteurl'和'home',而不用使用'define()'實際觸及數據庫。我只需要以某種方式傳達一個事實,即我正在使用webpack-dev-server到WordPress並應用一些不同的配置。 devServer設置中的標題可以工作。有一個upvote。 – Christian