0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-24 23:21:57 -05:00

fix(markdoc & mdx): Proxy crimes (#10278)

* fix(markdoc & mdx): Proxy cimes

* chore: changeset
This commit is contained in:
Erika 2024-03-01 15:48:38 +01:00 committed by GitHub
parent 87a3d51f2c
commit a548a3a99c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 108 additions and 5 deletions

View file

@ -0,0 +1,6 @@
---
"@astrojs/markdoc": patch
"astro": patch
---
Fixes original images sometimes being kept / deleted when they shouldn't in both MDX and Markdoc

View file

@ -108,6 +108,7 @@
"dev": "astro-scripts dev --copy-wasm --prebuild \"src/runtime/server/astro-island.ts\" --prebuild \"src/runtime/client/{idle,load,media,only,visible}.ts\" \"src/**/*.{ts,js}\"",
"postbuild": "astro-scripts copy \"src/**/*.astro\" && astro-scripts copy \"src/**/*.wasm\"",
"test": "pnpm run test:node",
"test:match": "pnpm run test:node --match",
"test:e2e": "playwright test",
"test:e2e:match": "playwright test -g",
"test:node": "astro-scripts test \"test/**/*.test.js\""

View file

@ -119,7 +119,13 @@ export async function generateImagesForPath(
!globalThis.astroAsset.referencedImages?.has(transformsAndPath.originalSrcPath)
) {
try {
await fs.promises.unlink(getFullImagePath(originalFilePath, env));
if (transformsAndPath.originalSrcPath) {
env.logger.debug(
'assets',
`Deleting ${originalFilePath} as it's not referenced outside of image processing.`
);
await fs.promises.unlink(getFullImagePath(originalFilePath, env));
}
} catch (e) {
/* No-op, it's okay if we fail to delete one of the file, we're not too picky. */
}

View file

@ -11,7 +11,11 @@ export function getProxyCode(options: ImageMetadata, isSSR: boolean): string {
if (name === 'fsPath') {
return ${stringifiedFSPath};
}
${!isSSR ? `globalThis.astroAsset.referencedImages.add(${stringifiedFSPath});` : ''}
${
!isSSR
? `if (target[name] !== undefined) globalThis.astroAsset.referencedImages.add(${stringifiedFSPath});`
: ''
}
return target[name];
}
})

View file

@ -28,6 +28,7 @@ export type LoggerLabel =
| 'preferences'
| 'redirects'
| 'toolbar'
| 'assets'
// SKIP_FORMAT: A special label that tells the logger not to apply any formatting.
// Useful for messages that are already formatted, like the server start message.
| 'SKIP_FORMAT';

View file

@ -0,0 +1,7 @@
import mdx from '@astrojs/mdx';
import markdoc from '@astrojs/markdoc';
import { defineConfig } from 'astro/config';
export default defineConfig({
integrations: [mdx(), markdoc()],
});

View file

@ -3,7 +3,9 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
"astro": "workspace:*",
"@astrojs/mdx": "workspace:*",
"@astrojs/markdoc": "workspace:*"
},
"scripts": {
"dev": "astro dev"

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

View file

@ -0,0 +1,7 @@
---
title: "Markdoc"
---
There is an image below
![A penguin](../../assets/markdocStillExists.jpg)

View file

@ -0,0 +1,7 @@
---
title: "MDX"
---
There is an image below
![A penguin](../../assets/mdxDontExist.jpg)

View file

@ -0,0 +1,12 @@
import { defineCollection, z } from 'astro:content';
const blog = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
})
});
export const collections = {
blog: blog,
};

View file

@ -0,0 +1,22 @@
---
import type { GetStaticPaths } from "astro";
import { getCollection } from "astro:content";
export const getStaticPaths = (async () => {
const blog = await getCollection("blog");
return blog.map((post) => ({
params: {
slug: post.slug,
},
props: {
post
}
}));
}) satisfies GetStaticPaths;
const { post } = Astro.props;
const { Content } = await post.render();
---
<Content />

View file

@ -3,7 +3,7 @@ import { before, describe, it } from 'node:test';
import { testImageService } from './test-image-service.js';
import { loadFixture } from './test-utils.js';
describe('astro:assets - delete images that are unused', () => {
describe('astro:assets - delete images that are unused zzz', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
@ -33,5 +33,15 @@ describe('astro:assets - delete images that are unused', () => {
const imagesUsedElsewhere = await fixture.glob('_astro/url.*.*');
assert.equal(imagesUsedElsewhere.length, 2);
});
it('should delete MDX images only used for optimization', async () => {
const imagesOnlyOptimized = await fixture.glob('_astro/mdxDontExist.*.*');
assert.equal(imagesOnlyOptimized.length, 1);
});
it('should always keep Markdoc images', async () => {
const imagesUsedElsewhere = await fixture.glob('_astro/markdocStillExists.*.*');
assert.equal(imagesUsedElsewhere.length, 2);
});
});
});

View file

@ -196,7 +196,19 @@ async function emitOptimizedImages(
ctx.pluginContext.meta.watchMode,
ctx.pluginContext.emitFile
);
node.attributes[attributeName] = src;
const fsPath = resolved.id;
if (src) {
// We cannot track images in Markdoc, Markdoc rendering always strips out the proxy. As such, we'll always
// assume that the image is referenced elsewhere, to be on safer side.
if (ctx.astroConfig.output === 'static') {
if (globalThis.astroAsset.referencedImages)
globalThis.astroAsset.referencedImages.add(fsPath);
}
node.attributes[attributeName] = { ...src, fsPath };
}
} else {
throw new MarkdocError({
message: `Could not resolve image ${JSON.stringify(

6
pnpm-lock.yaml generated
View file

@ -2516,6 +2516,12 @@ importers:
packages/astro/test/fixtures/core-image-deletion:
dependencies:
'@astrojs/markdoc':
specifier: workspace:*
version: link:../../../../integrations/markdoc
'@astrojs/mdx':
specifier: workspace:*
version: link:../../../../integrations/mdx
astro:
specifier: workspace:*
version: link:../../..