Skip to content

Commit a858760

Browse files
aves84SpaceK33z
authored andcommitted
historyApiFallback fix (#617)
1 parent f454402 commit a858760

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

examples/history-api-fallback/app.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
11
var path = document.location.pathname;
22
document.write("It's working from path <b>" + path + "</b>");
3+
4+
document.addEventListener("DOMContentLoaded", function() {
5+
var tests = [
6+
{ url: "/", name: "index", re: /^<!DOCTYPE html>/ },
7+
{ url: "/test", name: "unexisting path", re: /^<!DOCTYPE html>/ },
8+
{ url: "/file.txt", name: "existing path", re: /^file/ },
9+
];
10+
11+
var table = document.createElement("table");
12+
var tbody = document.createElement("tbody");
13+
table.appendChild(tbody);
14+
document.body.appendChild(table);
15+
16+
tests.forEach(function(test) {
17+
var tr = document.createElement("tr");
18+
tbody.appendChild(tr);
19+
check(test.url, test.re, function(res) {
20+
tr.innerHTML = "<td>" + test.name + "</td>";
21+
tr.innerHTML += "<td><a href=\"" + test.url + "\">" + test.url + "</a></td>";
22+
tr.innerHTML += "<td class=\"" + res + "\">" + res + "</td>";
23+
});
24+
});
25+
});
26+
27+
function check(url, re, cb) {
28+
var xhr = new XMLHttpRequest();
29+
xhr.addEventListener("load", function() {
30+
cb(re.test(xhr.responseText) ? "ok" : "error");
31+
});
32+
xhr.open("GET", url);
33+
xhr.send();
34+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file

examples/history-api-fallback/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22
<html>
33
<head>
44
<script src="/bundle.js" type="text/javascript" charset="utf-8"></script>
5+
<style>
6+
table {
7+
border-collapse: collapse;
8+
border: 1px solid black;
9+
}
10+
table td {
11+
border: 1px dashed black;
12+
padding: 2px 15px;
13+
}
14+
table tr td:last-child {
15+
text-align: center;
16+
}
17+
table td.ok {
18+
background-color: #8f8;
19+
}
20+
table td.error {
21+
background-color: #f88;
22+
}
23+
</style>
524
</head>
625
<body>
726
<h1>Example: history API fallback</h1>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
module.exports = {
22
context: __dirname,
33
entry: "./app.js",
4+
devServer: {
5+
historyApiFallback: {
6+
disableDotRule: true
7+
}
8+
}
49
}

lib/Server.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ function Server(compiler, options) {
122122
res.end("</body></html>");
123123
}.bind(this));
124124

125+
var contentBase = options.contentBase || process.cwd();
126+
125127
var features = {
126128
compress: function() {
127129
if(options.compress) {
@@ -235,13 +237,14 @@ function Server(compiler, options) {
235237
historyApiFallback: function() {
236238
if(options.historyApiFallback) {
237239
// Fall back to /index.html if nothing else matches.
238-
app.use(historyApiFallback(typeof options.historyApiFallback === "object" ? options.historyApiFallback : null));
240+
app.use(
241+
historyApiFallback(typeof options.historyApiFallback === "object" ? options.historyApiFallback : null),
242+
express.static(contentBase)
243+
);
239244
}
240245
},
241246

242247
contentBase: function() {
243-
var contentBase = options.contentBase || process.cwd();
244-
245248
if(Array.isArray(contentBase)) {
246249
contentBase.forEach(function(item) {
247250
app.get("*", express.static(item));
@@ -293,11 +296,11 @@ function Server(compiler, options) {
293296
var defaultFeatures = ["setup", "headers", "middleware"];
294297
if(options.proxy)
295298
defaultFeatures.push("proxy", "middleware");
296-
if(options.historyApiFallback)
297-
defaultFeatures.push("historyApiFallback", "middleware");
298299
defaultFeatures.push("magicHtml");
299300
if(options.contentBase !== false)
300301
defaultFeatures.push("contentBase");
302+
if(options.historyApiFallback)
303+
defaultFeatures.push("historyApiFallback", "middleware");
301304
// compress is placed last and uses unshift so that it will be the first middleware used
302305
if(options.compress)
303306
defaultFeatures.unshift("compress");

0 commit comments

Comments
 (0)