Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(assets): Fix image errors when images were used on the client #9049

Merged
merged 4 commits into from
Nov 10, 2023
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/sour-schools-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix image errors when images were used on the client
16 changes: 13 additions & 3 deletions packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export default function assets({
configResolved(viteConfig) {
resolvedConfig = viteConfig;
},
async load(id) {
async load(id, options) {
// If our import has any query params, we'll let Vite handle it
// See https://github.com/withastro/astro/issues/8333
if (id !== removeQueryString(id)) {
Expand All @@ -191,8 +191,18 @@ export default function assets({
});
}

return `
export default ${getProxyCode(meta, isServerLikeOutput(settings.config))}`;
// We can only reliably determine if an image is used on the server, as we need to track its usage throughout the entire build.
// Since you cannot use image optimization on the client anyway, it's safe to assume that if the user imported
// an image on the client, it should be present in the final build.
if (options?.ssr) {
return `export default ${getProxyCode(meta, isServerLikeOutput(settings.config))}`;
} else {
if (!globalThis.astroAsset.referencedImages)
globalThis.astroAsset.referencedImages = new Set();

globalThis.astroAsset.referencedImages.add(meta.fsPath);
return `export default ${JSON.stringify(meta)}`;
}
}
},
},
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,18 @@ describe('astro:image', () => {
expect(isReusingCache).to.be.true;
});

it('client images are written to build', async () => {
const html = await fixture.readFile('/client/index.html');
const $ = cheerio.load(html);
let $script = $('script');

// Find image
const regex = /src:"([^"]*)/gm;
const imageSrc = regex.exec($script.html())[1];
const data = await fixture.readFile(imageSrc, null);
expect(data).to.be.an.instanceOf(Buffer);
});

describe('custom service in build', () => {
it('uses configured hashes properties', async () => {
await fixture.build();
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import light_walrus from "../assets/light_walrus.avif";
console.log(light_walrus);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script src="../client/client-image.ts"></script>
<div></div>
Loading