2018-04-10 19:50:28 +03:00
|
|
|
const Boom = require('boom');
|
2018-01-08 17:13:05 +02:00
|
|
|
const Inert = require('inert');
|
|
|
|
const Path = require('path');
|
2018-03-22 17:34:49 +02:00
|
|
|
const Intercept = require('apr-intercept');
|
|
|
|
const Fs = require('mz/fs');
|
2018-01-08 17:13:05 +02:00
|
|
|
|
2018-05-07 15:33:08 +03:00
|
|
|
const { NAMESPACE = 'navigation', NODE_ENV = 'development' } = process.env;
|
2018-01-08 17:13:05 +02:00
|
|
|
|
|
|
|
exports.register = async server => {
|
2018-05-07 15:33:08 +03:00
|
|
|
let manifest = {};
|
|
|
|
|
|
|
|
try {
|
|
|
|
manifest = require('../build/asset-manifest.json');
|
|
|
|
} catch (err) {
|
|
|
|
if (NODE_ENV === 'production') {
|
|
|
|
throw err;
|
|
|
|
} else {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:34:49 +02:00
|
|
|
const buildRoot = Path.join(__dirname, '../build');
|
|
|
|
const buildStatic = Path.join(buildRoot, `${NAMESPACE}`);
|
|
|
|
const publicRoot = Path.join(__dirname, `../public/static`);
|
2018-01-08 17:13:05 +02:00
|
|
|
|
2018-03-22 17:34:49 +02:00
|
|
|
await server.register([
|
|
|
|
{
|
|
|
|
plugin: Inert
|
|
|
|
}
|
|
|
|
]);
|
2018-01-08 17:13:05 +02:00
|
|
|
|
|
|
|
server.route([
|
|
|
|
{
|
|
|
|
method: 'GET',
|
2018-03-22 17:34:49 +02:00
|
|
|
path: `/${NAMESPACE}/service-worker.js`,
|
|
|
|
config: {
|
|
|
|
auth: false,
|
|
|
|
handler: {
|
|
|
|
file: {
|
|
|
|
path: Path.join(__dirname, '../build/service-worker.js')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
path: `/${NAMESPACE}/favicon.ico`,
|
2018-01-08 17:13:05 +02:00
|
|
|
config: {
|
|
|
|
auth: false,
|
2018-03-22 17:34:49 +02:00
|
|
|
handler: {
|
|
|
|
file: {
|
|
|
|
path: Path.join(__dirname, '../build/favicon.ico')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
path: `/${NAMESPACE}/static/{rest*}`,
|
|
|
|
config: {
|
|
|
|
auth: false
|
|
|
|
},
|
|
|
|
handler: async (request, h) => {
|
|
|
|
const { params } = request;
|
|
|
|
const { rest } = params;
|
2018-01-08 17:13:05 +02:00
|
|
|
|
2018-04-10 19:50:28 +03:00
|
|
|
if (!rest) {
|
|
|
|
return Boom.notFound();
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:34:49 +02:00
|
|
|
const publicPathname = Path.join(publicRoot, rest);
|
|
|
|
const [err1] = await Intercept(
|
|
|
|
Fs.access(publicPathname, Fs.constants.R_OK)
|
|
|
|
);
|
2018-01-08 17:13:05 +02:00
|
|
|
|
2018-03-22 17:34:49 +02:00
|
|
|
if (!err1) {
|
|
|
|
return h.file(publicPathname, {
|
|
|
|
confine: publicRoot
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-09 12:34:49 +03:00
|
|
|
const buildPathname = Path.join(buildStatic, 'static', rest);
|
2018-03-22 17:34:49 +02:00
|
|
|
const [err2] = await Intercept(
|
2018-05-09 12:34:49 +03:00
|
|
|
Fs.access(buildPathname, Fs.constants.R_OK)
|
2018-03-22 17:34:49 +02:00
|
|
|
);
|
2018-01-08 17:13:05 +02:00
|
|
|
|
2018-03-22 17:34:49 +02:00
|
|
|
if (!err2) {
|
2018-05-09 12:34:49 +03:00
|
|
|
return h.file(buildPathname, {
|
2018-03-22 17:34:49 +02:00
|
|
|
confine: buildStatic
|
2018-02-19 16:59:31 +02:00
|
|
|
});
|
2018-01-08 17:13:05 +02:00
|
|
|
}
|
2018-03-22 17:34:49 +02:00
|
|
|
|
2018-05-09 12:34:49 +03:00
|
|
|
const filename = manifest[rest];
|
|
|
|
if (!filename) {
|
|
|
|
return Boom.notFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
const buildMapPathname = Path.join(buildRoot, filename);
|
|
|
|
return h.file(buildMapPathname, {
|
2018-03-22 17:34:49 +02:00
|
|
|
confine: buildStatic
|
|
|
|
});
|
2018-01-08 17:13:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.pkg = require('../package.json');
|