Repository URL to install this package:
|
Version:
0.0.15 ▾
|
/**
* @file this whole file is already done?
*/
import { resolveToRoot } from '../aliased';
import { logger } from '../log';
/**
* @todo resolve to root same as express static
* @see https://medium.com/@rajaraodv/webpack-hot-module-replacement-hmr-e756a726a07
* @see http://code.fitness/post/2016/02/webpack-public-path-and-hot-reload.html
*/
export function fileMiddleware(req, res, next) {
const pathname = req.pathname || req.originalUrl || '';
const send = (path) => {
const resolvedPathname = resolveToRoot(path);
const sanitizedPathname = resolvedPathname.replace('/undefined', '').replace('/undefined', '');
logger.debug(`[express] handling request
pathname: ${pathname}
file: ${sanitizedPathname}
`.replace(/ /gm, ''));
res.sendFile(sanitizedPathname);
};
if (pathname.includes('robots.txt')) {
send('dist/assets/robots.txt');
}
else if (pathname.includes('workbox.js')) {
send('dist/dist/bundled/client/workbox.js');
}
else if (pathname.includes('manifest.json')) {
send('dist/assets/manifest.json');
}
else if (pathname.includes('googlebf877feb3c9e8b84.html')) {
// @todo - make this generic so people can enter their code
res
.status(200)
.contentType('text/html')
.send('google-site-verification: googlebf877feb3c9e8b84.html');
}
else if (pathname.includes('sitemap.xml')) {
// @todo
// bootstrapper/pathParams
// => xml
}
// else if (process.env.NODE_ENV === 'production') {
// console.log('ignoring route, on deployment')
// next()
// }
else if (pathname === undefined) {
next();
}
else if (pathname.includes('/compat')) {
// example
// https://uxui.skavaone.com/search/compat/Account.AddressSection.page~Checkout.page~Product.page~ProductList.page~Search.page~SignInSuccess.pa~09e1225e-6ffc991cb85002ce799b.js
// => '/compat/Account.AddressSection.page~Checkout.page~Product.page~ProductList.page~Search.page~SignInSuccess.pa~09e1225e-6ffc991cb85002ce799b.js'
const [before, after] = pathname.split('/compat');
const distPath = 'dist/dist/bundled/compat' + after;
send(distPath);
}
else if (pathname.includes('/client')) {
// example
// https://uxui.skavaone.com/search/undefined/client/Account.AddressSection.page~Checkout.page~Product.page~ProductList.page~Search.page~SignInSuccess.pa~09e1225e-6ffc991cb85002ce799b.js
// => '/client/Account.AddressSection.page~Checkout.page~Product.page~ProductList.page~Search.page~SignInSuccess.pa~09e1225e-6ffc991cb85002ce799b.js'
const [before, after] = pathname.split('/client');
const distPath = 'dist/dist/bundled/client' + after;
// @todo ensure deployed path is the same...
// const resolvedPathname = resolveToRoot('dist/dist/bundled' + pathname)
// const resolvedPathnameBackup = resolveToRoot('dist/bundled' + pathname)
// console.log({ resolvedPathname })
// res.sendFile(resolvedPathname)
send(distPath);
}
else {
logger.info('[express] handling request: did not match request: ' + pathname);
next();
}
}
//# sourceMappingURL=fileMiddleware.js.map