This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
middleware-factory.js
98 lines (79 loc) · 3.1 KB
/
middleware-factory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const polyfillIo = require('./polyfill-io');
module.exports = ({
getAssetUrl,
stylesheetManager,
linkHeaderHelper
}) => {
return (req, res, next) => {
// define a helper for adding a link header
res.locals.resourceHints = {
highest: [],
normal: []
};
res.linkResource = linkHeaderHelper;
if (req.accepts('text/html')) {
res.locals.javascriptBundles = [];
res.locals.stylesheets = {
inline: [],
lazy: [],
blocking: []
};
res.locals.stylesheets.inline = ['head'];
res.locals.stylesheets.lazy = ['main'];
res.locals.polyfillIo = polyfillIo(res.locals.flags);
res.locals.javascriptBundles.push(
res.locals.polyfillIo.enhanced,
getAssetUrl({
file: 'font-loader.js',
flags: res.locals.flags,
isNUi: true
}),
getAssetUrl({
file: 'o-errors.js',
flags: res.locals.flags,
isNUi: true
}),
getAssetUrl({
file: 'es5.js',
flags: res.locals.flags,
isNUi: true
}),
getAssetUrl('main-without-n-ui.js')
);
res.locals.javascriptBundles.push();
// output the default link headers just before rendering
const originalRender = res.render;
res.render = function (template, templateData) {
// Add standard n-ui stylesheets
res.locals.stylesheets.inline.unshift('head-n-ui-core');
// For now keep building n-ui-core in the main app stylesheet
// res.locals.stylesheets.lazy.unshift('n-ui-core');
res.locals.stylesheets.inline = stylesheetManager.concatenateStyles(res.locals.stylesheets.inline);
// TODO collect metrics on this similar to inline stylesheets
res.locals.stylesheets.lazy = res.locals.stylesheets.lazy
.map(name => getAssetUrl(stylesheetManager.nameToUrlConfig(name)));
res.locals.stylesheets.blocking = res.locals.stylesheets.blocking
.map(name => getAssetUrl(stylesheetManager.nameToUrlConfig(name)));
res.locals.stylesheets.lazy.forEach(file => res.linkResource(file, { as: 'style' }, { priority: 'highest' }));
res.locals.stylesheets.blocking.forEach(file => res.linkResource(file, { as: 'style' }, { priority: 'highest' }));
res.locals.javascriptBundles.forEach(file => res.linkResource(file, { as: 'script' }, { priority: 'highest' }));
// TODO make this a setting on the app - template data feels like a messy place
if (templateData.withAssetPrecache) {
res.locals.stylesheets.lazy.forEach(file => res.linkResource(file, {as: 'style', rel: 'precache'}));
res.locals.stylesheets.blocking.forEach(file => res.linkResource(file, {as: 'style', rel: 'precache'}));
res.locals.javascriptBundles.forEach(file => res.linkResource(file, {as: 'script', rel: 'precache'}));
}
// supercharge the masthead image
res.linkResource(
'https://www.ft.com/__origami/service/image/v2/images/raw/ftlogo:brand-ft-masthead?source=o-header&tint=%23333333,%23333333&format=svg',
{ as: 'image' },
{ priority: 'highest' }
);
res.append('Link', this.locals.resourceHints.highest);
res.append('Link', this.locals.resourceHints.normal);
return originalRender.apply(res, [].slice.call(arguments));
};
}
next();
};
};