1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-03-18 00:19:49 +01:00

fix: Add appropriate response headers to SPA entry point HTML response (#6992)

## About the changes

Current state, when returning the HTML entry point from the server,
there are no headers attached. We encountered an issue with a deployment
and this had an impact for us.

A brief description:

1. We deployed the most recent version. Noticed an unrelated issue.
2. Users tried to use the most recent version and due to their client
cache, requested assets that did not exist in the newest version.
3. Our cache layer cached the assets that were not there with the HTML
response. It had to infer the type based on the filename because there
was no attached `Content-Type` header. This cache was very sticky.
4. After rolling back we saw the HTML response (from the cache) instead
of the appropriate response from the upstream Unleash application.

This PR does a few things.

1. When responding with the HTML entry point, it adds header
(`Content-Type: text/html`).
2. When the client is requesting an asset (a path that ends with an
extension), it also instructs the resource not to be cached
(`Cache-Control: no-cache`) and returns a 404. This will prevent misses
from getting cached.


## Discussion points

To me, there doesn't seem to be a lot of test infra on serving the SPA
application. If that is an error, please indicate where that is and an
appropriate test can be added.
This commit is contained in:
Zachary Skalko 2024-05-15 08:07:07 -05:00 committed by GitHub
parent e9c4b471c7
commit 9903cf1090
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -201,6 +201,7 @@ export default async function getApp(
}
app.get(`${baseUriPath}`, (req, res) => {
res.set('Content-Type', 'text/html');
res.send(indexHTML);
});
@ -214,6 +215,14 @@ export default async function getApp(
});
app.get(`${baseUriPath}/*`, (req, res) => {
res.set('Content-Type', 'text/html');
const requestPath = path.parse(req.url);
// appropriately return 404 requests for assets with an extension (js, css, etc)
if (requestPath.ext !== '' && requestPath.ext !== 'html') {
res.set('Cache-Control', 'no-cache');
res.status(404).send(indexHTML);
return;
}
res.send(indexHTML);
});