Skip to content

Commit

Permalink
fix: /hoodie/unknown responds with 404
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Jan 23, 2017
1 parent ae404fb commit 4eb2a42
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 52 deletions.
17 changes: 1 addition & 16 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,7 @@ function register (server, options, next) {
return next(error)
}

// we register a router handler for /hoodie/* which must be registered
// after all other plugins, otherwise routes like /hoodie/account/api/*
// will be handled by the public route handler
server.register({
register: require('./plugins/public'),
options: {
config: options
}
}, function (error) {
/* istanbul ignore next */
if (error) {
return next(error)
}

next(null, server, options)
})
next(null, server, options)
})
})
}
3 changes: 2 additions & 1 deletion server/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function registerPlugins (server, config, callback) {
var localPlugins = [
'./client',
'./logger',
'./maybe-force-gzip'
'./maybe-force-gzip',
'./public'
]
.concat(
[
Expand Down
38 changes: 3 additions & 35 deletions server/plugins/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ module.exports.register.attributes = {
dependencies: 'inert'
}

var fs = require('fs')
var path = require('path')
var pathJoin = require('path').join

function register (server, options, next) {
var publicFolder = options.config.paths.public
var app = path.join(publicFolder, 'index.html')
var hoodieVersion
try {
hoodieVersion = require('hoodie/package.json').version
} catch (err) {
hoodieVersion = 'development'
}

var hoodiePublicPath = path.join(require.resolve('../../package.json'), '..', 'public')
var adminPublicPath = path.join(require.resolve('@hoodie/admin/package.json'), '..', 'dist')
var hoodiePublicPath = pathJoin(require.resolve('../../package.json'), '..', 'public')
var adminPublicPath = pathJoin(require.resolve('@hoodie/admin/package.json'), '..', 'dist')

server.route([{
method: 'GET',
Expand Down Expand Up @@ -62,35 +60,5 @@ function register (server, options, next) {
}
}])

// serve app whenever an html page is requested
// and no other document is available
server.ext('onPostHandler', function (request, reply) {
var response = request.response

if (!response.isBoom) {
return reply.continue()
}

var is404 = response.output.statusCode === 404
var isResource = /(text\/css|application\/json|application\/javascript|image\/png|image\/jpeg)/.test(request.headers.accept) || /.(css|js|png|jpg)$/.test(request.path)
var isAdminPath = /^\/hoodie\/admin\//.test(request.path)
var isHoodiePath = /^\/hoodie\//.test(request.path)

// We only care about 404 for html requests...
if (is404 && isResource) {
return reply(fs.createReadStream(path.join(hoodiePublicPath, '404.html'))).code(404)
}

if (isAdminPath) {
return reply(fs.createReadStream(path.join(adminPublicPath, 'index.html')))
}
if (isHoodiePath) {
return reply(fs.createReadStream(path.join(hoodiePublicPath, 'index.html')))
}

// Serve index.html
reply(fs.createReadStream(app))
})

return next()
}

0 comments on commit 4eb2a42

Please sign in to comment.