-
Notifications
You must be signed in to change notification settings - Fork 5
feat: suppress caching for added project hosts via Cache-Control override #798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
528134e
0650cc6
853ac55
5f50df1
e0138a9
2b9bbf5
7f14dfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,10 +19,73 @@ import { ADMIN_ORIGIN, ADMIN_ORIGIN_NEW } from './utils/admin.js'; | |||||||||||||||||||||||||||||||
| const { host: adminHost } = new URL(ADMIN_ORIGIN); | ||||||||||||||||||||||||||||||||
| const { host: newAdminHost } = new URL(ADMIN_ORIGIN_NEW); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** Cache-Control max-age in seconds for added project hosts (HTML/JSON). 60 = 1 minute. */ | ||||||||||||||||||||||||||||||||
| export const CACHE_MAX_AGE_SECONDS = 60; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function getRandomId() { | ||||||||||||||||||||||||||||||||
| return Math.floor(Math.random() * 1000000); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Returns the hostname (domain) from a project host value (may be domain or full URL). | ||||||||||||||||||||||||||||||||
| * @param {string} host | ||||||||||||||||||||||||||||||||
| * @returns {string} | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| export function getHostDomain(host) { | ||||||||||||||||||||||||||||||||
| if (!host || typeof host !== 'string') return ''; | ||||||||||||||||||||||||||||||||
| return host.startsWith('http') ? new URL(host).host : host; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Builds declarativeNetRequest rules that set Cache-Control on responses | ||||||||||||||||||||||||||||||||
| * for added project hosts. | ||||||||||||||||||||||||||||||||
| * @param {Object[]} projectConfigs Configs with host, previewHost, liveHost, reviewHost | ||||||||||||||||||||||||||||||||
| * @returns {Object[]} Rules to add | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| export function getCacheControlRules(projectConfigs) { | ||||||||||||||||||||||||||||||||
| const rules = []; | ||||||||||||||||||||||||||||||||
| projectConfigs | ||||||||||||||||||||||||||||||||
| .flatMap((p) => [ | ||||||||||||||||||||||||||||||||
| p?.host, | ||||||||||||||||||||||||||||||||
| p?.previewHost, | ||||||||||||||||||||||||||||||||
| p?.liveHost, | ||||||||||||||||||||||||||||||||
| p?.reviewHost, | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| p?.host, | |
| p?.previewHost, | |
| p?.liveHost, | |
| p?.reviewHost, | |
| p?.host, | |
| p?.liveHost, |
rofe marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the must-revalidate
| value: `max-age=${CACHE_MAX_AGE_SECONDS}`, | |
| value: `max-age=${CACHE_MAX_AGE_SECONDS}, must-revalidate`, |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually don't need image, media and other
| 'main_frame', | |
| 'sub_frame', | |
| 'script', | |
| 'stylesheet', | |
| 'image', | |
| 'xmlhttprequest', | |
| 'media', | |
| 'font', | |
| 'other', | |
| 'main_frame', | |
| 'sub_frame', | |
| 'script', | |
| 'stylesheet', | |
| 'xmlhttprequest', | |
| 'font', |
rofe marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,7 +18,10 @@ import { setUserAgent } from '@web/test-runner-commands'; | |||||
| import sinon from 'sinon'; | ||||||
|
|
||||||
| import { | ||||||
| CACHE_MAX_AGE_SECONDS, | ||||||
| configureAuthAndCorsHeaders, | ||||||
| getCacheControlRules, | ||||||
| getHostDomain, | ||||||
| setAuthToken, | ||||||
| updateUserAgent, | ||||||
| } from '../src/extension/auth.js'; | ||||||
|
|
@@ -54,6 +57,59 @@ describe('Test auth', () => { | |||||
| await configureAuthAndCorsHeaders(); | ||||||
| }); | ||||||
|
|
||||||
| describe('getHostDomain', () => { | ||||||
| it('returns hostname for full URL', () => { | ||||||
| expect(getHostDomain('https://example.com/path')).to.equal('example.com'); | ||||||
| expect(getHostDomain('https://sub.example.com:443/')).to.equal('sub.example.com'); | ||||||
| }); | ||||||
| it('returns input for plain domain', () => { | ||||||
| expect(getHostDomain('example.com')).to.equal('example.com'); | ||||||
| expect(getHostDomain('main--repo--owner.aem.live')).to.equal('main--repo--owner.aem.live'); | ||||||
| }); | ||||||
| it('returns empty string for invalid or empty input', () => { | ||||||
| expect(getHostDomain('')).to.equal(''); | ||||||
| expect(getHostDomain(null)).to.equal(''); | ||||||
| expect(getHostDomain(undefined)).to.equal(''); | ||||||
| expect(getHostDomain(123)).to.equal(''); | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| describe('getCacheControlRules', () => { | ||||||
| it('returns one rule per unique host', () => { | ||||||
| const configs = [ | ||||||
| { host: 'prod.example.com', previewHost: 'preview.example.com' }, | ||||||
| { liveHost: 'live.example.com' }, | ||||||
| ]; | ||||||
| const rules = getCacheControlRules(configs); | ||||||
| expect(rules).to.have.lengthOf(3); | ||||||
| expect(rules.every((r) => r.action?.responseHeaders?.[0]?.header === 'Cache-Control')).to.be.true; | ||||||
| expect(rules.every((r) => r.action.responseHeaders[0].value === `max-age=${CACHE_MAX_AGE_SECONDS}`)).to.be.true; | ||||||
|
||||||
| expect(rules.every((r) => r.action.responseHeaders[0].value === `max-age=${CACHE_MAX_AGE_SECONDS}`)).to.be.true; | |
| expect(rules.every((r) => r.action.responseHeaders[0].value === `max-age=${CACHE_MAX_AGE_SECONDS}, must-revalidate`)).to.be.true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.