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

feat: add support for server islands for MDX #12446

Closed
wants to merge 3 commits into from
Closed
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
54 changes: 52 additions & 2 deletions packages/astro/src/jsx/rehype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const rehypeAnalyzeAstroMetadata: RehypePlugin = () => {
if (node.type !== 'mdxJsxFlowElement' && node.type !== 'mdxJsxTextElement') return;

const tagName = node.name;
if (!tagName || !isComponent(tagName) || !hasClientDirective(node)) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could removing this condition be why tests are failing? should you instead change the condition to also account for hasServerDeferDirective but otherwise leave in the hasClientDirective check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing this. Sure I will include hasServerDeferDirective in the existing condition.

if (!tagName || !isComponent(tagName)) return;

// From this point onwards, `node` is confirmed to be an island component

Expand Down Expand Up @@ -70,7 +70,7 @@ export const rehypeAnalyzeAstroMetadata: RehypePlugin = () => {
});
// Mutate node with additional island attributes
addClientOnlyMetadata(node, matchedImport, resolvedPath);
} else {
} else if (hasClientDirective(node)) {
// Add this component to the metadata
metadata.hydratedComponents.push({
exportName: '*',
Expand All @@ -80,6 +80,15 @@ export const rehypeAnalyzeAstroMetadata: RehypePlugin = () => {
});
// Mutate node with additional island attributes
addClientMetadata(node, matchedImport, resolvedPath);
} else if (hasServerDeferDirective(node)) {
metadata.serverComponents.push({
exportName: '*',
localName: '',
specifier: tagName,
resolvedPath,
});
// Mutate node with additional island attributes
addServerDeferMetadata(node, matchedImport, resolvedPath);
}
});

Expand Down Expand Up @@ -175,6 +184,12 @@ function hasClientDirective(node: MdxJsxFlowElementHast | MdxJsxTextElementHast)
);
}

function hasServerDeferDirective(node: MdxJsxFlowElementHast | MdxJsxTextElementHast) {
return node.attributes.some(
(attr) => attr.type === 'mdxJsxAttribute' && attr.name === 'server:defer',
);
}

function hasClientOnlyDirective(node: MdxJsxFlowElementHast | MdxJsxTextElementHast) {
return node.attributes.some(
(attr) => attr.type === 'mdxJsxAttribute' && attr.name === 'client:only',
Expand Down Expand Up @@ -320,3 +335,38 @@ function addClientOnlyMetadata(

node.name = ClientOnlyPlaceholder;
}

function addServerDeferMetadata(
node: MdxJsxFlowElementHast | MdxJsxTextElementHast,
meta: { path: string; name: string },
resolvedPath: string,
) {
const attributeNames = node.attributes
.map((attr) => (attr.type === 'mdxJsxAttribute' ? attr.name : null))
.filter(Boolean);

if (!attributeNames.includes('server:component-directive')) {
node.attributes.push({
type: 'mdxJsxAttribute',
name: 'server:directive',
value: 'server:defer',
});
}
if (!attributeNames.includes('server:component-path')) {
node.attributes.push({
type: 'mdxJsxAttribute',
name: 'server:component-path',
value: resolvedPath,
});
}
if (!attributeNames.includes('server:component-export')) {
if (meta.name === '*') {
meta.name = node.name!.split('.').slice(1).join('.')!;
}
node.attributes.push({
type: 'mdxJsxAttribute',
name: 'server:component-export',
value: meta.name,
});
}
}
Loading