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/quiet-grids-preload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a 404 for a CSS file that a dynamic import preloads when the imported module uses a component that is also a hydrated island on the same page
12 changes: 12 additions & 0 deletions packages/astro/src/core/build/plugins/plugin-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,18 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
}
}
}

// Vite builds the preload list of dynamic imports from `importedCss`, so a
// deleted asset left there is requested at runtime and 404s.
// https://github.com/withastro/astro/issues/18191
for (const cssId of deletedCssAssets.keys()) {
if (bundle[cssId]) continue;
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'chunk') {
chunk.viteMetadata?.importedCss?.delete(cssId);
}
}
}
},
};

Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/css-dangling-references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture, type Fixture } from './test-utils.ts';

const cssAssetReferenceRegExp = /_astro\/[A-Za-z\d\-]+\.[\da-f]{8}\.css/g;
const cssAssetReferenceRegExp = /_astro\/[\w-]+\.[\w-]{8}\.css/g;

describe("When Vite's preloadModule polyfill is used", async () => {
let fixture: Fixture;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<style>
div {
display: grid;
color: red;
}
</style>
<div>Grid</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import Grid from './Grid.svelte';
</script>

<dialog open><Grid /></dialog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
let Modal = $state(null);
async function open() {
Modal = (await import('./LazyModal.svelte')).default;
}
</script>

<button onclick={open}>Open modal</button>
{#if Modal}<Modal />{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
import Grid from "../components/hydrated-island/Grid.svelte"
import Opener from "../components/hydrated-island/Opener.svelte"
---
<Grid client:load/>
<Opener client:load/>
Loading