Skip to content

fix(render, tailwind): The renderAsync utility and the Tailwind component failing on the edge for next 14 #1079

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

Merged
merged 10 commits into from
Dec 15, 2023

Conversation

gabrielmfern
Copy link
Collaborator

@gabrielmfern gabrielmfern commented Dec 6, 2023

This introduces two changes, one to fix the renderAsync utility to run on the edge,
and the other to fix the Tailwind component also failing on the edge.

The renderAsync fix

After I investigated a bit on Next's code and found that they stub the renderToString
and renderToStaticMarkup functions to avoid their internal use of them because
it seems like it is going to be deprecated.
So to not have any problems with this I added a few tests that stub both these functions
on the renderAsync function's tests to make sure it runs fine, besides also removing
the fallback on the renderToStaticMarkup. I've also tested this
with Next 13 and it works well meaning there is no problem with the removal of renderToStaticMarkup.
I also used the renderToStaticNodeStream instead of renderToPipeableStream since
that function is made for rendering static content like renderToStaticMarkup.

The fix for the Tailwind component

For the Tailwind component due to the strategy of rendering the HTML just to then generate
Tailwind's global styles we used renderToStaticMarkup but that caused problems
on the edge as well and probably on other environments and situations too.

After thinking a lot about this, thinking if I could use the async React rendering functions
for this, and getting to the conclusion I needed to write the simplest we can get away
with mock renderer for React just to get the class names onto tailwind all at once I could.

This render utility just loops through the elements recursively (similarly to what the Tailwind component
already does) and then converts everything into HTML by directly mapping props into attributes,
(converting objects into JSONs as well). This also gave us a small performance boost but not very noticeable

I also wrote a simple unit test that tries out a few things to see if the renderer can take them as well,
like rendering components, dealing with children and fragments.

Closes #1054

… and prioritize the other functions instead of it
this was mostly to fix another issue that was arising after fixing #1054

the problem is that next kind of stubs the renderToString and renderToStaticMarkup
into throwing the errors that they were having, sadly the only way to get around
this was to implement a custom simple method that would render the most stub
HTML possible just for tailwind to generate their classes
…ings will run properly

also add a test to each environment with the legacy stubs that next 14 adds
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.

render + renderAsync not working in the next.js edge
3 participants