-
-
Notifications
You must be signed in to change notification settings - Fork 16
Update for SSR environments and Async imports #374
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
Conversation
✅ Deploy Preview for anchor-polyfill ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for anchor-position-wpt canceled.
|
jamesnw
left a comment
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.
Thanks for opening this PR! I'd like to figure out how to get this working in your setup.
As I look through this, I'm wondering if you've tried manually applying the polyfill? Something like this (note the import ends in /fn)-
import polyfill from '@oddbird/css-anchor-positioning/fn';
if (
!("anchorName" in document.documentElement.style) &&
typeof window !== 'undefined'
) {
polyfill()
}This gives you the control to add whatever logic you need to verify that your styles and DOM elements are ready to go before calling polyfill().
On the optional chaining, I'm wondering if you have a minimal reproduction of how to trigger errors in these places. This would be while walking the ASTs for CSS that is present when the polyfill is run, so these changes are suggesting that the AST is somehow malformed? If possible, it would be nice to catch those cases early and exit the polyfill immediately, rather than run the polyfill on empty or invalid ASTs.
Thanks!
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.
This file is only used for running the Web Platform Tests, in which case we can be sure that window is available. I don't think we need to make any changes to this file.
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.
I added it for continuity so the tests and public API are consistent. I can remove it.
|
It looks like that worked. |
|
Glad to hear that! I'll close this PR, but if you have any tips or patterns that others would find useful, feel free to document them here! Thanks for looking into this! |
|
@jamesnw I think we still need to add the check at the very least. The popover polyfill also has it. |
I think it makes sense to add an early return for this. I'm on the fence if it should be in index.ts (so that users of index-fn.ts have full control and responsibility over when the polyfill runs), or in polyfill.ts (to avoid running the polyfill when it is unlikely to succeed). Either way, I'm curious to see if it will impact the tests. Do you want to open a new PR with just that change, or reduce the scope of this PR? Either way is fine with me! Thanks! |
|
I'll start with a small one just for that change. Really, if that works, then I don't really need to worry about the async part of it in my use cases. |
Using this in our component library, we ran into 2 issues:
This PR adds a few defensive checks to ensure that when CSS nodes aren't fully initialized during async imports or in unusual SSR scenarios, the polyfill won't crash but will gracefully handle undefined values.
Related Issue(s)
Reminder to add related issue(s), if available.
Steps to test/reproduce
We have the polyfill loaded as part of a
usePopoverhook that is used in a tooltip component. We are loading the polyfill using the following approach to keep it SSR-safe:This results in console errors because node children cannot be found immediately due to the async nature of the
importfunction and the load timing of the component.Show me