Browse Source

added 'spa-asset-test' config option. Fixes #36

master
Lloyd Brookes 9 years ago
parent
commit
9dcccec177
  1. 3
      example/spa/.local-web-server.json
  2. BIN
      example/spa/image.jpg
  3. 2
      example/spa/index.html
  4. 15
      lib/middleware-stack.js
  5. 2
      package.json

3
example/spa/.local-web-server.json

@ -1,3 +1,4 @@
{ {
"spa": "index.html"
"spa": "index.html",
"no-cache": true
} }

BIN
example/spa/image.jpg

After

Width: 91  |  Height: 71  |  Size: 2.3 KiB

2
example/spa/index.html

@ -7,6 +7,8 @@
<li><a href="/login">/login</a></li> <li><a href="/login">/login</a></li>
<li><a href="/search">/search</a></li> <li><a href="/search">/search</a></li>
</ul> </ul>
<p>Found asset: <img src="image.jpg" /></p>
<p>Missing asset (should 404): <img src="sdafsadf.jpg" /></p>
<script> <script>
document.querySelector('h2 span').textContent = window.location.pathname document.querySelector('h2 span').textContent = window.location.pathname
</script> </script>

15
lib/middleware-stack.js

@ -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)
}) })
} }
} }

2
package.json

@ -40,7 +40,6 @@
"koa-compose": "^3.1.0", "koa-compose": "^3.1.0",
"koa-compress": "^1.0.9", "koa-compress": "^1.0.9",
"koa-conditional-get": "^1.0.3", "koa-conditional-get": "^1.0.3",
"koa-connect-history-api-fallback": "~0.3.0",
"koa-convert": "^1.2.0", "koa-convert": "^1.2.0",
"koa-etag": "^2.1.1", "koa-etag": "^2.1.1",
"koa-json": "^1.1.3", "koa-json": "^1.1.3",
@ -53,7 +52,6 @@
"path-to-regexp": "^1.5.0", "path-to-regexp": "^1.5.0",
"reduce-flatten": "^1.0.0", "reduce-flatten": "^1.0.0",
"stream-log-stats": "^1.1.3", "stream-log-stats": "^1.1.3",
"string-tools": "^1.0.0",
"test-value": "^2.0.0", "test-value": "^2.0.0",
"typical": "^2.4.2" "typical": "^2.4.2"
}, },

Loading…
Cancel
Save