Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`<br>
Default: `true`
Expand Down
33 changes: 21 additions & 12 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
};
Expand All @@ -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;

Expand Down