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

Commit

Permalink
feat(html): rewrite static asset references to ESI includes that prov…
Browse files Browse the repository at this point in the history
…ide 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
  • Loading branch information
trieloff committed Apr 24, 2019
1 parent dce696e commit aa2538f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/defaults/html.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const validate = require('../utils/validate');
const { cache, uncached } = require('../html/shared-cache');
const embeds = require('../html/find-embeds');
const parseFrontmatter = require('../html/parse-frontmatter');
const rewriteLinks = require('../html/static-asset-links');

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

Expand Down
71 changes: 71 additions & 0 deletions test/testRewriteStatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
*/
/* eslint-env mocha */
const assert = require('assert');
const { Logger } = require('@adobe/helix-shared');
const rewrite = require('../src/html/static-asset-links');
const { pipe } = require('../src/defaults/html.pipe.js');


const logger = Logger.getTestLogger({
// tune this for debugging
level: 'info',
});

function rw(content) {
return rewrite({
Expand All @@ -24,6 +32,69 @@ function rw(content) {
}).response.body;
}

describe('Integration Test Static Asset Rewriting', () => {
let production;

before('Fake Production Mode', () => {
// eslint-disable-next-line no-underscore-dangle
production = process.env.__OW_ACTIVATION_ID;
// eslint-disable-next-line no-underscore-dangle
process.env.__OW_ACTIVATION_ID = 'fake';
});

it('Test static asset rewriting in full pipeline', async () => {
const context = {
content: {
body: 'Hello World',
},
};
const action = {
request: {
params: {},
},
logger,
};
const once = ({ content }) => ({
response: {
status: 200,
body: `<html>
<head>
<title>${content.document.body.textContent}</title>
<script src="index.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
${content.document.body.innerHTML}
<script>
alert('ok');
</script>
</body>
</html>`,
},
});

const res = await pipe(once, context, action);

assert.equal(res.response.body, `<html><head>
<title>Hello World</title>
<script src='<esi:include src="index.js.esi"/><esi:remove>index.js</esi:remove>'></script>
<link rel="stylesheet" href='<esi:include src="style.css.esi"/><esi:remove>style.css</esi:remove>'>
</head>
<body>
<p>Hello World</p>
<script>
alert('ok');
</script>
</body></html>`);
});

after('Reset Production Mode', () => {
// eslint-disable-next-line no-underscore-dangle
process.env.__OW_ACTIVATION_ID = production;
});
});

describe('Test Static Asset Rewriting', () => {
it('Ignores non-HTML', () => {
assert.deepEqual(rewrite({
Expand Down

0 comments on commit aa2538f

Please sign in to comment.