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: allow conditional rendering when RenderScript is enabled #1057

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

MoustaphaDev
Copy link
Member

@MoustaphaDev MoustaphaDev commented Jan 7, 2025

Changes

Conditionally rendered processed scripts were inlined like normal scripts. This made sense before renderScript was introduced as processed scripts (and styles) couldn't be conditionally rendered. Now that processed scripts can be conditionally rendered, we shouldn't inline them like normal scripts.

This PR adds the ability to conditionally render processed scripts

Testing

  • Added a printer test to ensure conditional rendering of scripts is working as expected whether RenderScript is enabled or not

Docs

Copy link

changeset-bot bot commented Jan 7, 2025

🦋 Changeset detected

Latest commit: 3f1f082

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/compiler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

Not sure of the lint CI fail, but the fix here looks great to me. Thanks!

About docs, I think we need to update this part: https://docs.astro.build/en/guides/client-side-scripts/#opting-out-of-processing

Astro will not process your script tags in some situations. In particular, adding type="module" or any attribute other than src to a <script> tag will cause Astro to treat the tag as if it had an is:inline directive. The same will be true when the script is written in a JSX expression.

@MoustaphaDev
Copy link
Member Author

Good call, I'll update docs. The lint action fail is happening in main so it's unrelated.

@sarah11918
Copy link
Member

sarah11918 commented Jan 8, 2025

No problem with the docs PR making this change to be accurate!

BUT to check:

Astro will not process your script tags in some situations. In particular, adding type="module" or any attribute other than src to a <script> tag will cause Astro to treat the tag as if it had an is:inline directive. The same will be true when the script is written in a JSX expression.

  • if we are changing stated behaviour that people relied on, does this mean that some people might have written their scripts with certain expectations that are no longer true, and their project now works differently? Should we maybe add this note about scripts written in JSX expressions changing in the upgrade guide entry for renderScript, given that tons of people haven't upgraded yet and may still be using this guide for a while? That would handle the breaking change at the time it was intended (upgrade to v5 and directRenderScript becoming the default)

This PR adds the ability to conditionally render processed scripts

  • the docs changed don't seem to reference anything about a new option to allow conditional rendering? So, should this changeset should be more marked as a "fix that conditional rendering processed scripts wasn't working as intended" because there is not really some new feature for the docs to describe?

@MoustaphaDev
Copy link
Member Author

if we are changing stated behavior that people relied on, does this mean that some people might have written their scripts with certain expectations that are no longer true, and their project now works differently?

That's a great question! It's possible some folks are probably still relying on the old script behavior, and their sites will behave differently after upgrading if we keep this as a patch release. To make the transition smoother we could go with a minor release instead, then for the next Astro minor, we can make the "breaking" change to the script behavior, and add a note to the changelog so people are aware of the change.

Should we maybe add this note about scripts written in JSX expressions changing in the upgrade guide entry for renderScript, given that tons of people haven't upgraded yet and may still be using this guide for a while? That would handle the breaking change at the time it was intended (upgrade to v5 and directRenderScript becoming the default)

I think adding a note to the upgrade guide is a good idea! Was actually looking for a place to add it, so this is perfect.

the docs changed don't seem to reference anything about a new option to allow conditional rendering? So, should this changeset should be more marked as a "fix that conditional rendering processed scripts wasn't working as intended" because there is not really some new feature for the docs to describe?

That's a good point! This is technically a fix as since v5, it was intended that processed scripts could be rendered conditionally.
(If this was a new feature, it would be a breaking change and would require a major release if not under a flag, i.e Astro 6 😅)

I'll update the changeset to reflect that. Thanks for the feedback! 🙌

@bluwy
Copy link
Member

bluwy commented Jan 9, 2025

Hmm I didn't expect this to be possibly as big of a breaking change, but I think it might still be worth fixing this so we get a consistent conditional rendering behaviour like shown in the issue. I'd still consider this a bug fix, but of the potentially breaking kind. And if it turns out to be quite breaking we could consider reverting the behaviour and queue this for Astro 6. My bad for no thinking this through before 😅

@MoustaphaDev
Copy link
Member Author

MoustaphaDev commented Jan 9, 2025

Agreed! I think it's worth fixing this bug now, and we can mention this in the minor version blog post if we push this as a minor release. That way, those upgrading to next minor can be aware of the change and update their scripts accordingly, and current users are shielded (assuming most folks pin on minor version and that they read release notes 😅). I feel like this kinda breaks semver though, but I think it's the most happy path to fix this bug if we wanna push this before Astro 6.

And yeah, we can always revert if it becomes too big of a problem. Will lookout in support to gauge it.

@bluwy
Copy link
Member

bluwy commented Jan 10, 2025

I'm not sure how we can only limit this to the Astro minor. We have "@astrojs/compiler": "^2.10.3" which would allow minor releases of the compiler. Even if we use ~ and publish that, users who've not upgraded Astro could've also only upgraded @astrojs/compiler still and hit the issue. If we want to limit this as an Astro minor, we likely have to expose a new option from the compiler which we can enable in the Astro minor. I personally am also fine with releasing this PR as is though and check how this impacts people.

@MoustaphaDev
Copy link
Member Author

MoustaphaDev commented Jan 10, 2025

Good catch! I wrongly assumed that we pinned the compiler with ~. Agreed with merging this as is then!

@sarah11918
Copy link
Member

I'd still consider this a bug fix, but of the potentially breaking kind.

Yes, I think that's fine (if a little sneaky? 😅 )

Like I suggested, I think making sure this breaking changes is reflected in the upgrade guide allows this to be considered a fix of a thing we intended to break, just didn't properly break for v5! We should use the changeset to be clear that this is fixing an issue with a previously breaking change to get it to work as intended, so you might actually see the breaking change NOW -- I think that's our story, and we're sticking to it! (And, avoiding the need to wait until v6)

Co-authored-by: Sarah Rainsberger <[email protected]>
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Approving for docs, and the Docs PR is good to merge when this is released, too!

@MoustaphaDev
Copy link
Member Author

Awesome, thanks for your reviews and suggestions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Conditional rendering doesn't work directly on scripts
4 participants