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:astro-mdx-component-rendering #12963

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
add:mock-ssr-result
admirsaheta committed Jan 10, 2025
commit 260a840837c12e1da1c3d2d2e990dafa7a7b872e
69 changes: 53 additions & 16 deletions packages/integrations/mdx/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,76 @@
import { AstroJSX, jsx } from 'astro/jsx-runtime';

import { AstroError } from 'astro/errors';
import type { NamedSSRLoadedRendererValue } from 'astro';
import { AstroError } from 'astro/errors';
import { renderJSX } from 'astro/runtime/server/index.js';
import type { SSRResult } from '../../../astro/src/types/public/internal.js';

const slotName = (str: string) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());

const mockSSRResult: SSRResult = {
cancelled: false,
base: '/',
styles: new Set(),
scripts: new Set(),
links: new Set(),
componentMetadata: new Map(),
inlinedScripts: new Map(),
createAstro: () => {
throw new Error('createAstro is not implemented in mock');
},
params: {},
resolve: async (s: string) => s,
response: new Response(),
request: new Request('http://localhost'),
renderers: [],
clientDirectives: new Map(),
compressHTML: false,
partial: false,
pathname: '/',
cookies: undefined,
serverIslandNameMap: new Map(),
trailingSlash: 'ignore',
key: Promise.resolve(
crypto.subtle.generateKey({ name: 'HMAC', hash: 'SHA-256' }, true, ['sign', 'verify']),
),
_metadata: {
propagators: new Set(),
hasHydrationScript: false,
rendererSpecificHydrationScripts: new Set<string>(),
renderedScripts: new Set<string>(),
hasDirectives: new Set<string>(),
hasRenderedHead: false,
headInTree: false,
extraHead: [],
},
};

// NOTE: In practice, MDX components are always tagged with `__astro_tag_component__`, so the right renderer
// is used directly, and this check is not often used to return true.
export async function check(
Component: any,
props: any,
{ default: children = null, ...slotted } = {}
) {
{ default: children = null, ...slotted } = {},
) {
if (typeof Component !== 'function') return false;

const slots: Record<string, any> = {};
for (const [key, value] of Object.entries(slotted)) {
const name = slotName(key);
slots[name] = value;
const name = slotName(key);
slots[name] = value;
}

try {
const vnode = jsx(Component, { ...props, ...slots, children });


const result = { styles: [], scripts: [], links: [] };
const rendered = await renderJSX(result, vnode);

return rendered[AstroJSX];
const vnode = jsx(Component, { ...props, ...slots, children });

const rendered = await renderJSX(mockSSRResult, vnode);

return rendered[AstroJSX];
} catch (e) {
throwEnhancedErrorIfMdxComponent(e as Error, Component);
throwEnhancedErrorIfMdxComponent(e as Error, Component);
}
return false;
}
}

export async function renderToStaticMarkup(
this: any,