Skip to content

Commit e04d53f

Browse files
tapiobmuenzenmeyer
authored andcommitted
Move --spa handling to external middleware file (#150).
1 parent a29cbea commit e04d53f

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

packages/live-server/index.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function escape(html){
3131
}
3232

3333
// Based on connect.static(), but streamlined and with added code injecter
34-
function staticServer(root, spa) {
34+
function staticServer(root) {
3535
var isFile = false;
3636
try { // For supporting mounting files instead of just directories
3737
isFile = fs.statSync(root).isFile();
@@ -45,14 +45,6 @@ function staticServer(root, spa) {
4545
var injectCandidates = [ new RegExp("</body>", "i"), new RegExp("</svg>") ];
4646
var injectTag = null;
4747

48-
// Single Page App - redirect handler
49-
if (spa && req.url !== '/') {
50-
var route = req.url;
51-
req.url = '/';
52-
res.statusCode = 302;
53-
res.setHeader('Location', req.url + '#' + route);
54-
}
55-
5648
function directory() {
5749
var pathname = url.parse(req.originalUrl).pathname;
5850
res.statusCode = 301;
@@ -146,10 +138,9 @@ LiveServer.start = function(options) {
146138
LiveServer.logLevel = options.logLevel === undefined ? 2 : options.logLevel;
147139
var openPath = (options.open === undefined || options.open === true) ?
148140
"" : ((options.open === null || options.open === false) ? null : options.open);
149-
var spa = options.spa || false;
150141
if (options.noBrowser) openPath = null; // Backwards compatibility with 0.7.0
151142
var file = options.file;
152-
var staticServerHandler = staticServer(root, spa);
143+
var staticServerHandler = staticServer(root);
153144
var wait = options.wait === undefined ? 100 : options.wait;
154145
var browser = options.browser || null;
155146
var htpasswd = options.htpasswd || null;
@@ -170,6 +161,9 @@ LiveServer.start = function(options) {
170161
} else if (LiveServer.logLevel > 2) {
171162
app.use(logger('dev'));
172163
}
164+
if (options.spa) {
165+
middleware.push("spa");
166+
}
173167
// Add middleware
174168
middleware.map(function(mw) {
175169
if (typeof mw === "string") {

packages/live-server/live-server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ for (var i = process.argv.length - 1; i >= 2; --i) {
7474
}
7575
}
7676
else if (arg === "--spa") {
77-
opts.spa = true;
77+
opts.middleware.push("spa");
7878
process.argv.splice(i, 1);
7979
}
8080
else if (arg === "--quiet" || arg === "-q") {
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Single Page Apps - redirect to /#/
2+
module.exports = function(req, res, next) {
3+
if (req.method !== "GET" && req.method !== "HEAD")
4+
next();
5+
if (req.url !== '/') {
6+
var route = req.url;
7+
req.url = '/';
8+
res.statusCode = 302;
9+
res.setHeader('Location', req.url + '#' + route);
10+
res.end();
11+
}
12+
else next();
13+
}

0 commit comments

Comments
 (0)