From deaea6190646b45309967dc783b5dbfd4de5b8c7 Mon Sep 17 00:00:00 2001 From: shellscape Date: Sun, 27 Jan 2019 19:13:05 -0500 Subject: [PATCH] fix: strip accept header when using history-api-fallback --- README.md | 2 ++ lib/middleware.js | 33 +++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bce2217..48003f4 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,8 @@ If `true`, enables History API Fallback via [`connect-history-api-fallback`](htt This setting can be handy when using the HTML5 History API; `index.html` page will likely have to be served in place of any 404 responses from the server, specially when developing Single Page Applications. +_Note: The `Accept` header is explicitly stripped from the `/wps` WebSocket path when using `historyFallback`, due to [an issue](https://github.com/shellscape/webpack-plugin-serve/issues/94) with how Firefox and the middleware interact. + ### `hmr` Type: `boolean`
Default: `true` diff --git a/lib/middleware.js b/lib/middleware.js index 62c8a92..d1f3eba 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -88,8 +88,28 @@ const getBuiltins = (app, options) => { } }; + const four0four = (fn = () => {}) => { + app.use(async (ctx, next) => { + await next(); + if (ctx.status === 404) { + render404(ctx); + fn(ctx); + } + }); + }; + const historyFallback = () => { if (options.historyFallback) { + // https://github.com/shellscape/webpack-plugin-serve/issues/94 + // When using Firefox, the browser sends an accept header for /wps when using connect-history-api-fallback + app.use(async (ctx, next) => { + if (ctx.path.match(/^\/wps/)) { + const { accept, ...headers } = ctx.request.header; + ctx.request.header = headers; // eslint-disable-line no-param-reassign + } + await next(); + }); + app.use(convert(historyApiFallback(options.historyFallback))); } }; @@ -102,19 +122,8 @@ const getBuiltins = (app, options) => { } }; - const websocket = () => app.use(wsMiddleware); - const proxy = (...args) => convert(httpProxyMiddleware(...args)); - - const four0four = (fn = () => {}) => { - app.use(async (ctx, next) => { - await next(); - if (ctx.status === 404) { - render404(ctx); - fn(ctx); - } - }); - }; + const websocket = () => app.use(wsMiddleware); proxy.skip = true;