-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
@next: Calling setup()
multiple times leads to orphaned style tag
#321
Comments
It may take some time to dig through this. |
Sorry if my description is awful, certainly let me know and I can try to rephrase if it didn't make sense. Can't believe I also forgot a reproduction. Here's one: https://github.com/rschristian/twind-wmr-setup-bug. Simply run the dev server ( I am still trying to look into it myself, though there's a few bits that I don't understand. Edit: Ah, I'm a fool. A fix I had tried and was sure would work, but didn't, was failing due to the implementation details of |
should be fixed by 0e2aa5c in the next release |
@sastan Manually patched and can confirm, working beautifully! Thanks! I had ended up doing something similar, though forgot to update the PR. Sorry about that. |
Edit: Sorry in advance for this wall of text.
In short, style tags can be become orphans after calling
setup()
multiple times, leading to flashes of unstyled content. This results in a back-and-forth workflow of styles flashing every other refresh.Description / Walkthrough
sheet
passed as a param,setup
will callgetSheet()
twind/packages/twind/src/runtime.ts
Lines 96 to 100 in d7cd293
twind/packages/twind/src/sheets.ts
Lines 4 to 14 in d7cd293
setup
, after sorting its params, will firstly destroy the active sheet instance. None exists at this time, so nothing happenstwind/packages/twind/src/runtime.ts
Line 101 in d7cd293
twind/packages/twind/src/runtime.ts
Line 103 in d7cd293
setup
againsheet
, sogetSheet()
gets called againactive
now exists, so the.destroy()
occurs. This removes the style tag from the document, though the reference to this tag still exists (thesheet
param).sheet
.setup
a third timegetSheet()
active
does exist, but its sheet instance is the orphan. This gets removed again (nothing functionally happens as it's already been removed)This then goes on & on, which each odd-numbered refresh resulting in a page with styles, even-numbered is blank.
Additional
I toyed around with enforcing that
dom
create a new style tag and that "works", but that might not work at scale. Didn't actually run the tests with it, so not sure.twind/packages/twind/src/sheets.ts
Lines 71 to 72 in d7cd293
There's also that problem in that just by trying to destroy the active sheet, Twind doesn't have keep a reference to the Node, but potentially searches for it. We can't simply move theHmm, I actually don't understand what's happening here. Might've written this too soon.getSheet()
after the.destroy()
because of this.The actual user code is this block:
https://github.com/rschristian/twind-wmr/blob/88d5e648cebce03428f7c4a81560dfda87b9b249/src/index.ts#L14-L20
Is there something else I should be using in this situation? The docs indicate that
setup
can be called multiple times, though this seems like it can (will?) lead to stale references like this.Thanks as always for this awesome lib!
The text was updated successfully, but these errors were encountered: