From 20d9a15cff7304615ac410ca2fb5064eee0be047 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Fri, 31 Dec 2021 09:30:12 +0100 Subject: [PATCH] Add etag header to last modified check of motd (#1739) * Add etag header to last modified check of motd Signed-off-by: Tilman Vatteroth --- cypress/integration/motd.spec.ts | 18 +++++++++++++++--- .../initializers/fetch-motd.ts | 7 ++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cypress/integration/motd.spec.ts b/cypress/integration/motd.spec.ts index 51c12a6eb..686b2c00b 100644 --- a/cypress/integration/motd.spec.ts +++ b/cypress/integration/motd.spec.ts @@ -9,16 +9,16 @@ const MOCK_LAST_MODIFIED = 'mockETag' const motdMockContent = 'This is the mock Motd call' describe('Motd', () => { - const mockExistingMotd = () => { + const mockExistingMotd = (useEtag?: boolean) => { cy.intercept('GET', '/mock-backend/public/motd.txt', { statusCode: 200, - headers: { 'Last-Modified': MOCK_LAST_MODIFIED }, + headers: { [useEtag ? 'etag' : 'Last-Modified']: MOCK_LAST_MODIFIED }, body: motdMockContent }) cy.intercept('HEAD', '/mock-backend/public/motd.txt', { statusCode: 200, - headers: { 'Last-Modified': MOCK_LAST_MODIFIED } + headers: { [useEtag ? 'etag' : 'Last-Modified']: MOCK_LAST_MODIFIED } }) } @@ -32,6 +32,18 @@ describe('Motd', () => { cy.getByCypressId('motd').contains(motdMockContent) }) + it('can be dismissed using etag', () => { + mockExistingMotd(true) + cy.visit('/') + cy.getByCypressId('motd').contains(motdMockContent) + cy.getByCypressId('motd-dismiss') + .click() + .then(() => { + expect(localStorage.getItem(MOTD_LOCAL_STORAGE_KEY)).to.equal(MOCK_LAST_MODIFIED) + }) + cy.getByCypressId('motd').should('not.exist') + }) + it('can be dismissed', () => { mockExistingMotd() cy.visit('/') diff --git a/src/components/application-loader/initializers/fetch-motd.ts b/src/components/application-loader/initializers/fetch-motd.ts index fe746f93a..6720b4864 100644 --- a/src/components/application-loader/initializers/fetch-motd.ts +++ b/src/components/application-loader/initializers/fetch-motd.ts @@ -32,7 +32,8 @@ export const fetchMotd = async (customizeAssetsUrl: string): Promise => { if (response.status !== 200) { return } - if (response.headers.get('Last-Modified') === cachedLastModified) { + const lastModified = response.headers.get('Last-Modified') || response.headers.get('etag') + if (lastModified === cachedLastModified) { return } } @@ -47,9 +48,9 @@ export const fetchMotd = async (customizeAssetsUrl: string): Promise => { const motdText = await response.text() - const lastModified = response.headers.get('Last-Modified') + const lastModified = response.headers.get('Last-Modified') || response.headers.get('etag') if (!lastModified) { - log.warn("'Last-Modified' not found for motd.txt!") + log.warn("'Last-Modified' or 'Etag' not found for motd.txt!") } setMotd(motdText, lastModified)