Skip to content

Commit

Permalink
add:mock-ssr-result
Browse files Browse the repository at this point in the history
  • Loading branch information
admirsaheta committed Jan 10, 2025
1 parent d2e7f99 commit 260a840
Showing 1 changed file with 53 additions and 16 deletions.
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,
Expand Down

0 comments on commit 260a840

Please sign in to comment.