├── .gitignore
├── .travis.yml
├── README.md
├── index.js
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | coverage
2 | node_modules
3 | package-lock.json
4 | .idea
5 | *.tgz
6 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "6"
4 | - "8"
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # next-preload-headers
2 |
3 | [](https://badge.fury.io/js/next-preload-headers)
4 |
5 | express middleware that adds preload tags in body of initial response into header where
6 | proxy will generally upgrade them to http2 server push. Server pushing critical assets improves response time, especially for mobile users.
7 |
8 | ## Usage
9 |
10 | ```
11 | npm install --save next-preload-headers
12 | ```
13 |
14 | Add it to the express server.js like this:
15 |
16 | ```
17 | const nextPreloadHeaders = require('next-preload-headers')
18 |
19 | const app = express()
20 |
21 | app.use(nextPreloadHeaders)
22 | ```
23 |
24 |
25 | TODO:
26 | - add css to the top of the list since it is generally the most important
27 | - send/check a cookie with buildId so we know if resources need to be pushed for this build
28 | - consider sorting style elements first
29 | - figure out why sometimes getting "can't add headers to already sent response". I think this is just an HMR issue in dev.
30 |
31 |
32 | ##Changelog:
33 | * 3.0.4 - adding stylesheet link tags
34 | * 3.0.3 - fix error with semicolon separating links when should have been comma
35 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | /* eslint no-cond-assign: 0 */
2 |
3 | const interceptor = require("express-interceptor");
4 |
5 |
6 | function linkFile(name) {
7 | // All the nextjs stuff is simple. This would need to get more complex if we wanted to do more than
8 | // next static (endings may not be just js or css)
9 | let as = "script";
10 | if (name.endsWith(".css")) {
11 | as = "style";
12 | }
13 | return `<${name}>; as=${as}; rel=preload; crossorigin=anonymous`;
14 | }
15 |
16 | module.exports = interceptor((req, res) => ({
17 | // Only HTML responses will be intercepted
18 | isInterceptable() {
19 | return /text\/html/.test(res.get("Content-Type"));
20 | },
21 | // Appends a paragraph at the end of the response body
22 | intercept(body, send) {
23 | const matches = [];
24 | const preloadRegex = / linkFile(m)).join(", ");
31 |
32 | // Add to existing links if exist
33 | const existingLinks = res.getHeader("link");
34 |
35 | if (existingLinks) {
36 | res.setHeader("link", `${existingLinks}, ${newLinks}`);
37 | } else {
38 | res.setHeader("link", newLinks);
39 | }
40 |
41 | send(body);
42 | },
43 | }));
44 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "next-preload-headers",
3 | "description": "next.js static assets upgraded to preload response headers",
4 | "version": "3.0.4",
5 | "homepage": "https://github.com/Enalmada/next-preload-headers",
6 | "main": "index.js",
7 | "repository": {
8 | "type": "git",
9 | "url": "https://github.com/Enalmada/next-preload-headers.git"
10 | },
11 | "keywords": [
12 | "next.js",
13 | "http2",
14 | "server push",
15 | "header",
16 | "link"
17 | ],
18 | "bugs": {
19 | "url": "https://github.com/Enalmada/next-preload-headers/issues"
20 | },
21 | "scripts": {
22 | "test": "standard && mocha"
23 | },
24 | "dependencies": {
25 | "express-interceptor": "^1.2.0"
26 | },
27 | "devDependencies": {
28 | "express": "^4.16.4",
29 | "mocha": "^6.1.4",
30 | "standard": "^12.0.1",
31 | "supertest": "^4.0.2"
32 | },
33 | "author": "Adam Lane ",
34 | "license": "MIT"
35 | }
36 |
--------------------------------------------------------------------------------