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:
parent
87a3d51f2c
commit
a548a3a99c
16 changed files with 108 additions and 5 deletions
6
.changeset/giant-spies-type.md
Normal file
6
.changeset/giant-spies-type.md
Normal 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
|
|
@ -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\""
|
||||
|
|
|
@ -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. */
|
||||
}
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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';
|
||||
|
|
7
packages/astro/test/fixtures/core-image-deletion/astro.config.mjs
vendored
Normal file
7
packages/astro/test/fixtures/core-image-deletion/astro.config.mjs
vendored
Normal 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()],
|
||||
});
|
|
@ -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"
|
||||
|
|
BIN
packages/astro/test/fixtures/core-image-deletion/src/assets/markdocStillExists.jpg
vendored
Normal file
BIN
packages/astro/test/fixtures/core-image-deletion/src/assets/markdocStillExists.jpg
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
BIN
packages/astro/test/fixtures/core-image-deletion/src/assets/mdxDontExist.jpg
vendored
Normal file
BIN
packages/astro/test/fixtures/core-image-deletion/src/assets/mdxDontExist.jpg
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 35 KiB |
7
packages/astro/test/fixtures/core-image-deletion/src/content/blog/markdoc.mdoc
vendored
Normal file
7
packages/astro/test/fixtures/core-image-deletion/src/content/blog/markdoc.mdoc
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
title: "Markdoc"
|
||||
---
|
||||
|
||||
There is an image below
|
||||
|
||||

|
7
packages/astro/test/fixtures/core-image-deletion/src/content/blog/mdx.mdx
vendored
Normal file
7
packages/astro/test/fixtures/core-image-deletion/src/content/blog/mdx.mdx
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
title: "MDX"
|
||||
---
|
||||
|
||||
There is an image below
|
||||
|
||||

|
12
packages/astro/test/fixtures/core-image-deletion/src/content/config.ts
vendored
Normal file
12
packages/astro/test/fixtures/core-image-deletion/src/content/config.ts
vendored
Normal 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,
|
||||
};
|
22
packages/astro/test/fixtures/core-image-deletion/src/pages/blog/[slug].astro
vendored
Normal file
22
packages/astro/test/fixtures/core-image-deletion/src/pages/blog/[slug].astro
vendored
Normal 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 />
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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
6
pnpm-lock.yaml
generated
|
@ -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:../../..
|
||||
|
|
Loading…
Add table
Reference in a new issue