Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nine-jokes-sink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/react': patch
---

Fix React 19 "Float" mechanism injecting <link rel="preload"> into Astro islands instead of the <head>. This PR adds a filter to @astrojs/react to strip these auto-generated resource from the island's HTML output, ensuring valid HTML structure.
7 changes: 7 additions & 0 deletions packages/integrations/react/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ async function renderToStaticMarkup(
} else {
html = await renderToPipeableStreamAsync(vnode, renderOptions);
}
// Strip React 19 auto-injected resource hints (preloads, etc.) from island output.
// These should be in <head>, not inside the island.
// See: https://github.com/facebook/react/issues/27910
html = html.replace(
/<link\s[^>]*rel="(?:preload|modulepreload|stylesheet|preconnect|dns-prefetch)"[^>]*>/g,
'',
);
return { html, attrs };
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from 'astro/config';
import react from '@astrojs/react';

export default defineConfig({
integrations: [react()],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@fixture/react-19-preloads",
"type": "module",
"dependencies": {
"astro": "latest",
"@astrojs/react": "latest",
"react": "^19.0.0",
"react-dom": "^19.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function ImageComponent() {
return (
<div>
<h1>React 19 Island</h1>
<img src="/_astro/test-image.webp" alt="Test" />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import ImageComponent from '../components/ImageComponent';
---
<html>
<head>
<title>React 19 Test</title>
</head>
<body>
<ImageComponent client:load />
</body>
</html>
18 changes: 18 additions & 0 deletions packages/integrations/react/test/react-19-preloads.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assert from 'node:assert';
import { test } from 'node:test';
import { loadFixture } from '../../../astro/test/test-utils.js';

test.describe('React 19 SSR integration', () => {
test('should strip preloads to prevent invalid HTML inside astro-islands', async () => {
const fixture = await loadFixture({ root: new URL('./fixtures/react-19-preloads/', import.meta.url) });
await fixture.build();

const html = await fixture.readFile('/index.html');
const islandPattern = /<astro-island[^>]*>([\s\S]*?)<\/astro-island>/;
const match = islandPattern.exec(html);
const island = match ? match[1] : '';

assert.ok(!island.includes('rel="preload"'), 'React 19: preloads should be stripped');
assert.ok(island.includes('<img'), 'Component content should be preserved');
});
})
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading