From 0e81c0529bb32b90e17e403ffd1f8a097a48c851 Mon Sep 17 00:00:00 2001 From: web-padawan Date: Sat, 16 Apr 2016 18:53:47 +0300 Subject: [PATCH 1/3] SPA: use koa-connect-history-api-fallback --- lib/local-web-server.js | 20 +++++++++++--------- package.json | 1 + 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/local-web-server.js b/lib/local-web-server.js index 8db7cd9..2aa5f49 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -164,6 +164,17 @@ function localWebServer (options) { } }) + /* for any URL not matched by static (e.g. `/search`), serve the SPA */ + if (options.spa) { + const historyApiFallback = require('koa-connect-history-api-fallback'); + debug('SPA', options.spa) + app.use(historyApiFallback({ + index: options.spa, + verbose: options.verbose, + rewrites: options.rewrite + })); + } + /* serve static files */ if (options.static.root) { const serve = require('koa-static') @@ -176,15 +187,6 @@ function localWebServer (options) { app.use(serveIndex(options.serveIndex.path, options.serveIndex.options)) } - /* for any URL not matched by static (e.g. `/search`), serve the SPA */ - if (options.spa) { - const send = require('koa-send') - debug('SPA', options.spa) - app.use(_.all('*', function spa (ctx, route, next) { - const root = path.resolve(options.static.root) || process.cwd() - return send(ctx, options.spa, { root: root }).then(next) - })) - } return app } diff --git a/package.json b/package.json index 11a9c3e..136c4e2 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "koa-bodyparser": "^3.0.0", "koa-compress": "^1.0.9", "koa-conditional-get": "^1.0.3", + "koa-connect-history-api-fallback": "^0.3.0", "koa-convert": "^1.2.0", "koa-etag": "^2.1.1", "koa-json": "^1.1.1", From 2ab1fffc0b37e3e96b75ef369ae554eed94cf0b4 Mon Sep 17 00:00:00 2001 From: web-padawan Date: Sat, 16 Apr 2016 19:50:49 +0300 Subject: [PATCH 2/3] remove semicolons --- lib/local-web-server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/local-web-server.js b/lib/local-web-server.js index 2aa5f49..17d08ac 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -166,13 +166,13 @@ function localWebServer (options) { /* for any URL not matched by static (e.g. `/search`), serve the SPA */ if (options.spa) { - const historyApiFallback = require('koa-connect-history-api-fallback'); + const historyApiFallback = require('koa-connect-history-api-fallback') debug('SPA', options.spa) app.use(historyApiFallback({ index: options.spa, verbose: options.verbose, rewrites: options.rewrite - })); + })) } /* serve static files */ From f9d823356ec6fe621b70d04abd5159f409e9334e Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Sat, 16 Apr 2016 20:55:18 +0100 Subject: [PATCH 3/3] don't use 3rd party rewrites --- lib/local-web-server.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/local-web-server.js b/lib/local-web-server.js index 17d08ac..1eec7ca 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -170,8 +170,7 @@ function localWebServer (options) { debug('SPA', options.spa) app.use(historyApiFallback({ index: options.spa, - verbose: options.verbose, - rewrites: options.rewrite + verbose: options.verbose })) }