-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Update compile.ts Deals with Hydration directive error #12078
base: main
Are you sure you want to change the base?
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a patch changeset? pnpm changeset
at the root
// Optionally log a warning or error for devs | ||
logger.warn( | ||
null, | ||
`Hydration directive found in ${compileProps.filename}. Astro components should not use client-side rendering.` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rn we show in what component an invalid client directive is found right? Is it also possible to show on what component it happens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from my understanding the error is from the way the component is used. and the file that calls it incurrectly that is logged.
hay, florian-lefebvre its a little late for that... after commiting I deleted it locally. |
No worries! Usually, it's better to keep the repo because we often ask for changes. I'll handle the changeset! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to warn in compile time (which I think is a good thing because we're already doing so in MDX), I think we should warn from here instead:
astro/packages/astro/src/vite-plugin-astro/compile.ts
Lines 27 to 31 in 928dc31
export async function compileAstro({ | |
compileProps, | |
astroFileToCompileMetadata, | |
logger, | |
}: CompileAstroOption): Promise<CompileAstroResult> { |
The transformResult
can contain information like this:
{
"clientOnlyComponents": [],
"hydratedComponents": [
{
"exportName": "default",
"localName": "",
"specifier": "./Foo.astro",
"resolvedPath": "./Foo.astro"
}
]
}
And you can use this information (from clientOnlyComponents
and hydratedComponents
) to identify if there's misused Astro components by checking the .astro
extension in resolvedPath
.
This way we can provide more details on which imported component causes it and we can also remove the runtime checks from
astro/packages/astro/src/runtime/server/render/astro/instance.ts
Lines 75 to 87 in 928dc31
// Issue warnings for invalid props for Astro components | |
function validateComponentProps(props: any, displayName: string) { | |
if (props != null) { | |
for (const prop of Object.keys(props)) { | |
if (prop.startsWith('client:')) { | |
// eslint-disable-next-line | |
console.warn( | |
`You are attempting to render <${displayName} ${prop} />, 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.`, | |
); | |
} | |
} | |
} | |
} |
(NOTE: you can play with the compiled output at https://live-astro-compiler.vercel.app with advanced settings enabled so it displays a Hydration tab) (looks like there's a bug with localName
not showing for default
)
Changes
-The following changes solves issue number 11623.
Closes #11623
-The command npm run build now prints the error message that it printed in Developement.
-Updated compile.ts file in astro\packages\astro\src\vite-plugin-astro\compile.ts line 38-46
before
after
Testing
I recreated the error with the guidence of the issue page. added a Header and called it in the index
recreated both dev and production error. then changed the file compile.ts retested it and now get the error I got in dev mode in productionDocs
I dont belive fixing an error line should need to be in the docs.