Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
feat(loadModule): optional cache bust key (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
JAdshead committed Mar 17, 2020
1 parent 883c419 commit b34b1bd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ exports[`sendHtml renderModuleScripts adds cache busting key from module map to
exports[`sendHtml renderModuleScripts does not add cache busting key from module map to each module script src if NODE_ENV is development 1`] = `"<script src=\\"https://example.com/cdn/test-root/2.2.2/test-root.browser.js\\" crossorigin=\\"anonymous\\" ></script>"`;

exports[`sendHtml renderModuleScripts does not add cache busting key if not present 1`] = `"<script src=\\"https://example.com/cdn/test-root/2.2.2/test-root.browser.js\\" crossorigin=\\"anonymous\\" integrity=\\"nggdfhr34\\"></script>"`;
exports[`sendHtml renderModuleScripts send a rendered page keeping correctly ordered modules 1`] = `"<script src=\\"https://example.com/cdn/test-root/2.2.2/test-root.browser.js?key=123\\" crossorigin=\\"anonymous\\" integrity=\\"dhhfsdfwer\\"></script><script src=\\"https://example.com/cdn/a/2.2.2/a.browser.js?key=123\\" crossorigin=\\"anonymous\\" integrity=\\"fhgnt543\\"></script><script src=\\"https://example.com/cdn/b/2.2.2/b.browser.js?key=123\\" crossorigin=\\"anonymous\\" integrity=\\"yhrtrhw3\\"></script><script src=\\"https://example.com/cdn/c/2.2.2/c.browser.js?key=123\\" crossorigin=\\"anonymous\\" integrity=\\"323egdsbf\\"></script>"`;
exports[`sendHtml renderModuleScripts send a rendered page with correctly ordered modules 1`] = `"<script src=\\"https://example.com/cdn/test-root/2.2.2/test-root.browser.js?key=123\\" crossorigin=\\"anonymous\\" integrity=\\"dhhfsdfwer\\"></script><script src=\\"https://example.com/cdn/a/2.2.2/a.browser.js?key=123\\" crossorigin=\\"anonymous\\" integrity=\\"fhgnt543\\"></script><script src=\\"https://example.com/cdn/b/2.2.2/b.browser.js?key=123\\" crossorigin=\\"anonymous\\" integrity=\\"yhrtrhw3\\"></script><script src=\\"https://example.com/cdn/c/2.2.2/c.browser.js?key=123\\" crossorigin=\\"anonymous\\" integrity=\\"323egdsbf\\"></script>"`;
Expand Down
11 changes: 11 additions & 0 deletions __tests__/server/middleware/sendHtml.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,17 @@ describe('sendHtml', () => {
})).toMatchSnapshot();
});

it('does not add cache busting key if not present', () => {
const moduleMap = { ...clientModuleMapCache.browser };
delete moduleMap.key;
expect(renderModuleScripts({
clientInitialState: req.store.getState(),
moduleMap,
isDevelopmentEnv: false,
bundle: 'browser',
})).toMatchSnapshot();
});

it('sends a rendered page with cross origin scripts', () => {
expect(renderModuleScripts({
clientInitialState: req.store.getState(),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"cors": "^2.8.5",
"express": "^4.17.1",
"helmet": "^3.21.2",
"holocron": "^1.0.0-15",
"holocron": "^1.0.0",
"holocron-module-route": "^1.0.0-6",
"if-env": "^1.0.4",
"immutable": "^4.0.0-rc.12",
Expand Down
2 changes: 1 addition & 1 deletion src/server/middleware/sendHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function renderModuleScripts({
const { integrity, url: src } = moduleMap.modules[moduleName][bundle];
const { key } = moduleMap;
const additionalAttributes = isDevelopmentEnv ? '' : `integrity="${integrity}"`;
const scriptSource = isDevelopmentEnv ? src : `${src}?key=${key}`;
const scriptSource = isDevelopmentEnv || !key ? src : `${src}?key=${key}`;
return `<script src="${scriptSource}" crossorigin="anonymous" ${additionalAttributes}></script>`;
}).join(isDevelopmentEnv ? '\n ' : '');
}
Expand Down

0 comments on commit b34b1bd

Please sign in to comment.