Skip to content

Commit

Permalink
Warn hydration directive for Astro components in JSX (#4330)
Browse files Browse the repository at this point in the history
* Warn hydration directive for Astro components in JSX

* Add changeset

* Better comment
  • Loading branch information
bluwy committed Aug 23, 2022
1 parent dc42f2c commit baa2ddd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-adults-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Warn hydration directive for Astro components in JSX
17 changes: 17 additions & 0 deletions packages/astro/src/jsx/babel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PluginObj } from '@babel/core';
import * as t from '@babel/types';
import { pathToFileURL } from 'node:url';
import { HydrationDirectiveProps } from '../runtime/server/hydration.js';
import type { PluginMetadata } from '../vite-plugin-astro/types';

const ClientOnlyPlaceholder = 'astro-client-only';
Expand Down Expand Up @@ -280,6 +281,22 @@ export default function astroJSX(): PluginObj {

const meta = path.getData('import');
if (meta) {
// If JSX is importing an Astro component, e.g. using MDX for templating,
// check Astro node's props and make sure they are valid for an Astro component
if (meta.path.endsWith('.astro')) {
const displayName = getTagName(parentNode);
for (const attr of parentNode.openingElement.attributes) {
if (t.isJSXAttribute(attr)) {
const name = jsxAttributeToString(attr);
if (HydrationDirectiveProps.has(name)) {
// eslint-disable-next-line
console.warn(
`You are attempting to render <${displayName} ${name} />, but ${displayName} is an Astro component. Astro components do not render in the client and should not have a hydration directive. Please use a framework component for client rendering.`
);
}
}
}
}
let resolvedPath: string;
if (meta.path.startsWith('.')) {
const fileURL = pathToFileURL(state.filename!);
Expand Down

0 comments on commit baa2ddd

Please sign in to comment.