diff --git a/homeassistant/components/hassio/http.py b/homeassistant/components/hassio/http.py index 4a0def62b4d..fe01cbe3197 100644 --- a/homeassistant/components/hassio/http.py +++ b/homeassistant/components/hassio/http.py @@ -10,6 +10,7 @@ import aiohttp from aiohttp import web from aiohttp.client import ClientTimeout from aiohttp.hdrs import ( + CACHE_CONTROL, CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TYPE, @@ -51,6 +52,8 @@ NO_AUTH = re.compile( r"^(?:" r"|app/.*" r"|addons/[^/]+/logo" r"|addons/[^/]+/icon" r")$" ) +NO_STORE = re.compile(r"^(?:" r"|app/entrypoint.js" r")$") + class HassIOView(HomeAssistantView): """Hass.io view to handle base part.""" @@ -104,7 +107,7 @@ class HassIOView(HomeAssistantView): # Stream response response = web.StreamResponse( - status=client.status, headers=_response_header(client) + status=client.status, headers=_response_header(client, path) ) response.content_type = client.content_type @@ -139,7 +142,7 @@ def _init_header(request: web.Request) -> dict[str, str]: return headers -def _response_header(response: aiohttp.ClientResponse) -> dict[str, str]: +def _response_header(response: aiohttp.ClientResponse, path: str) -> dict[str, str]: """Create response header.""" headers = {} @@ -153,6 +156,9 @@ def _response_header(response: aiohttp.ClientResponse) -> dict[str, str]: continue headers[name] = value + if NO_STORE.match(path): + headers[CACHE_CONTROL] = "no-store, max-age=0" + return headers diff --git a/tests/components/hassio/test_http.py b/tests/components/hassio/test_http.py index f411b465774..16121393170 100644 --- a/tests/components/hassio/test_http.py +++ b/tests/components/hassio/test_http.py @@ -185,3 +185,21 @@ async def test_stream(hassio_client, aioclient_mock): aioclient_mock.get("http://127.0.0.1/test") await hassio_client.get("/api/hassio/test", data="test") assert isinstance(aioclient_mock.mock_calls[-1][2], StreamReader) + + +async def test_entrypoint_cache_control(hassio_client, aioclient_mock): + """Test that we return cache control for requests to the entrypoint only.""" + aioclient_mock.get("http://127.0.0.1/app/entrypoint.js") + aioclient_mock.get("http://127.0.0.1/app/entrypoint.fdhkusd8y43r.js") + + resp1 = await hassio_client.get("/api/hassio/app/entrypoint.js") + resp2 = await hassio_client.get("/api/hassio/app/entrypoint.fdhkusd8y43r.js") + + # Check we got right response + assert resp1.status == 200 + assert resp2.status == 200 + + assert len(aioclient_mock.mock_calls) == 2 + assert resp1.headers["Cache-Control"] == "no-store, max-age=0" + + assert "Cache-Control" not in resp2.headers