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

Twind and React-Query/Suspense prevent styles from loading immediately #158

Closed
b3nten opened this issue Sep 9, 2022 · 3 comments
Closed
Labels
bug Something isn't working enhancement New feature or request upstream Waiting on upstream updates

Comments

@b3nten
Copy link
Collaborator

b3nten commented Sep 9, 2022

This is a bit of a weird one.

Essentially, the first time you load a page after starting the server, the styles for a component inside of a suspense boundary are delayed when the suspense boundary resolves. In fact, when setting an arbitrary delay inside the fetch call, the styles are delayed by that amount of time. This only occurs on first load - subsequent loading of the page, without restarting the dev server, don't have this issue. I'm not sure the repercussions of having multiple clients hitting the server.

https://github.com/B3nten/twind-suspense

Additionally, the same problem occurs in the working example here: #153

@deckchairlabs deckchairlabs added the bug Something isn't working label Sep 9, 2022
@deckchairlabs
Copy link
Member

deckchairlabs commented Sep 12, 2022

Yeah, so this seems to be related to suspense and the twind stylesheet. When you're suspending, the tw template literal isn't being called during the flush effect for injecting the twind style tag into the head, since that suspense will resolve AFTER the stream has started sending.

You can move the tw call to the outside of the Component for this to work as intended, although ideally we need a way of checking if the twind virtual sheet has new rules added to it, as those suspense boundaries are resolving server side. eg.

const className = tw`text-red-500`
export function Todo({ todo }: { todo: number }) {
  ...
}

@mashaal
Copy link
Member

mashaal commented Nov 2, 2022

It looks like this is fixed in the upcoming version of React. The style tags are hoisted into the head automatically with the new head/inject to stream feature.

Testing in the experimental example atm.

reactjs/rfcs#219

@mashaal mashaal added enhancement New feature or request upstream Waiting on upstream updates labels Nov 3, 2022
@mashaal
Copy link
Member

mashaal commented Nov 4, 2022

Should be fixed with ff26246

@mashaal mashaal closed this as completed Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request upstream Waiting on upstream updates
Projects
None yet
Development

No branches or pull requests

3 participants