0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

Fix server island script breaking when charset is added to content-type (#12810)

This commit is contained in:
Louis Escher 2024-12-23 13:20:39 +01:00 committed by GitHub
parent 98f9e833a5
commit 70a9f0b984
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 28 additions and 1 deletions

View file

@ -0,0 +1,7 @@
---
'astro': patch
---
Fixes server islands failing to check content-type header under certain circumstances
Sometimes a reverse proxy or similar service might modify the content-type header to include the charset or other parameters in the media type of the response. This previously wasn't handled by the client-side server island script and thus removed the script without actually placing the requested content in the DOM. This fix makes it so the script checks if the header starts with the proper content type instead of exactly matching `text/html`, so the following will still be considered a valid header: `text/html; charset=utf-8`

View file

@ -0,0 +1,5 @@
---
Astro.response.headers.set('content-type', 'text/html;charset=utf-8');
---
<h2 id="charset-in-content-type">I'm an island with a different content-type response header</h2>

View file

@ -3,6 +3,7 @@ import Island from '../components/Island.astro';
import Self from '../components/Self.astro';
import HTMLError from '../components/HTMLError.astro';
import { generateLongText } from '../lorem';
import MediaTypeInHeader from '../components/MediaTypeInHeader.astro';
const content = generateLongText(5);
@ -22,6 +23,8 @@ export const prerender = false;
<Self server:defer />
<MediaTypeInHeader server:defer />
<div id="big">
<Island server:defer secret="test" content={content} />
</div>

View file

@ -44,6 +44,15 @@ test.describe('Server islands', () => {
await expect(el).toHaveText('test');
});
test("content-type header with media type still allows the island to be displayed", async ({
page,
astro,
}) => {
await page.goto(astro.resolveUrl('/base/'));
let el = page.locator('#charset-in-content-type');
await expect(el).toHaveCount(1);
});
test('Self imported module can server defer', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/base/'));
let el = page.locator('.now');

View file

@ -120,7 +120,10 @@ let response = await fetch('${serverIslandUrl}', {
`
}
if (script) {
if(response.status === 200 && response.headers.get('content-type') === 'text/html') {
if(
response.status === 200
&& response.headers.has('content-type')
&& response.headers.get('content-type').split(";")[0].trim() === 'text/html') {
let html = await response.text();
// Swap!