|
@ -214,20 +214,23 @@ class MiddlewareStack extends Array { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* for any URL not matched by static (e.g. `/search`), serve the SPA */ |
|
|
/* for any URL not matched by static (e.g. `/search`), serve the SPA */ |
|
|
addSpa (spa) { |
|
|
|
|
|
|
|
|
addSpa (spa, assetTest) { |
|
|
this.push({ |
|
|
this.push({ |
|
|
optionDefinitions: { |
|
|
optionDefinitions: { |
|
|
name: 'spa', alias: 's', type: String, typeLabel: '[underline]{file}', |
|
|
name: 'spa', alias: 's', type: String, typeLabel: '[underline]{file}', |
|
|
description: 'Path to a Single Page App, e.g. app.html.' |
|
|
description: 'Path to a Single Page App, e.g. app.html.' |
|
|
}, |
|
|
}, |
|
|
middleware: function (cliOptions) { |
|
|
middleware: function (cliOptions) { |
|
|
spa = t.isDefined(cliOptions.spa) ? cliOptions.spa : spa |
|
|
|
|
|
|
|
|
spa = cliOptions.spa || spa || 'index.html' |
|
|
|
|
|
assetTest = new RegExp(cliOptions['spa-asset-test'] || assetTest || '\\.') |
|
|
if (spa) { |
|
|
if (spa) { |
|
|
const historyApiFallback = require('koa-connect-history-api-fallback') |
|
|
|
|
|
|
|
|
const send = require('koa-send') |
|
|
|
|
|
const _ = require('koa-route') |
|
|
debug('SPA', spa) |
|
|
debug('SPA', spa) |
|
|
return historyApiFallback({ |
|
|
|
|
|
index: spa, |
|
|
|
|
|
verbose: cliOptions.verbose |
|
|
|
|
|
|
|
|
return _.get('*', function spaMw (ctx, route, next) { |
|
|
|
|
|
const root = path.resolve(cliOptions.directory || process.cwd()) |
|
|
|
|
|
debug(`SPA request. Route: ${route}, isAsset: ${assetTest.test(route)}`) |
|
|
|
|
|
return send(ctx, assetTest.test(route) ? route : spa, { root: root }).then(next) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|