Skip to content
This repository was archived by the owner on Feb 28, 2022. It is now read-only.

Commit aa2538f

Browse files
committed
feat(html): rewrite static asset references to ESI includes that provide stable URLs
When run in production, all `<script src>` and `<link href>` references pointing to external assets will be rewritten to become ESI includes of the same URL, with an added extension that can be resolved into a stable URL Fixes #224
1 parent dce696e commit aa2538f

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/defaults/html.pipe.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const validate = require('../utils/validate');
3131
const { cache, uncached } = require('../html/shared-cache');
3232
const embeds = require('../html/find-embeds');
3333
const parseFrontmatter = require('../html/parse-frontmatter');
34+
const rewriteLinks = require('../html/static-asset-links');
3435

3536
/* eslint no-param-reassign: off */
3637
/* eslint newline-per-chained-call: off */
@@ -62,6 +63,7 @@ const htmlpipe = (cont, payload, action) => {
6263
.after(cache).when(uncached)
6364
.after(key)
6465
.after(debug)
66+
.after(rewriteLinks).when(production)
6567
.after(flag).expose('esi').when(esi) // flag ESI when there is ESI in the response
6668
.error(selectStatus(production()));
6769

test/testRewriteStatic.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
*/
1212
/* eslint-env mocha */
1313
const assert = require('assert');
14+
const { Logger } = require('@adobe/helix-shared');
1415
const rewrite = require('../src/html/static-asset-links');
16+
const { pipe } = require('../src/defaults/html.pipe.js');
17+
18+
19+
const logger = Logger.getTestLogger({
20+
// tune this for debugging
21+
level: 'info',
22+
});
1523

1624
function rw(content) {
1725
return rewrite({
@@ -24,6 +32,69 @@ function rw(content) {
2432
}).response.body;
2533
}
2634

35+
describe('Integration Test Static Asset Rewriting', () => {
36+
let production;
37+
38+
before('Fake Production Mode', () => {
39+
// eslint-disable-next-line no-underscore-dangle
40+
production = process.env.__OW_ACTIVATION_ID;
41+
// eslint-disable-next-line no-underscore-dangle
42+
process.env.__OW_ACTIVATION_ID = 'fake';
43+
});
44+
45+
it('Test static asset rewriting in full pipeline', async () => {
46+
const context = {
47+
content: {
48+
body: 'Hello World',
49+
},
50+
};
51+
const action = {
52+
request: {
53+
params: {},
54+
},
55+
logger,
56+
};
57+
const once = ({ content }) => ({
58+
response: {
59+
status: 200,
60+
body: `<html>
61+
<head>
62+
<title>${content.document.body.textContent}</title>
63+
<script src="index.js"></script>
64+
<link rel="stylesheet" href="style.css" />
65+
</head>
66+
<body>
67+
${content.document.body.innerHTML}
68+
<script>
69+
alert('ok');
70+
</script>
71+
</body>
72+
</html>`,
73+
},
74+
});
75+
76+
const res = await pipe(once, context, action);
77+
78+
assert.equal(res.response.body, `<html><head>
79+
<title>Hello World</title>
80+
<script src='<esi:include src="index.js.esi"/><esi:remove>index.js</esi:remove>'></script>
81+
<link rel="stylesheet" href='<esi:include src="style.css.esi"/><esi:remove>style.css</esi:remove>'>
82+
</head>
83+
<body>
84+
<p>Hello World</p>
85+
<script>
86+
alert('ok');
87+
</script>
88+
89+
</body></html>`);
90+
});
91+
92+
after('Reset Production Mode', () => {
93+
// eslint-disable-next-line no-underscore-dangle
94+
process.env.__OW_ACTIVATION_ID = production;
95+
});
96+
});
97+
2798
describe('Test Static Asset Rewriting', () => {
2899
it('Ignores non-HTML', () => {
29100
assert.deepEqual(rewrite({

0 commit comments

Comments
 (0)