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: skip Balancer script if global one is already created #86

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mixeden
Copy link

@mixeden mixeden commented Nov 13, 2023

Hi, this is a great library, thanks for developing it :)

The problem I stumbled upon (and am fixing in this PR) is as follows.

There are two ways to use this library:

  • You use Balancer without a Provider. In this case, each Balancer has its own script that calculates the layout and does other things.
  • You use a Balancer with a Provider. In this case, they all have one common script that calculates the layout.

Now imagine you have a strict CSP and you need to pass a nonce. You pass the nonce to the Provider and do not pass it to Balancers.

This lib creates an empty script tag for each Balancer in case of global script injection. Since you don't pass the nonce to each Balancer, the CSP doesn't work (as there still is a script for each Balancer).

I fixed this by just not returning any script tag in case of global script injection.
Seems that this PR is associated with #78, but I'm not sure

Copy link

vercel bot commented Nov 13, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-wrap-balancer ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 13, 2023 6:57pm

@@ -71,16 +71,16 @@ const relayout: RelayoutFn = (id, ratio, wrapper) => {
// the function.
if (!wrapper['__wrap_o']) {
if (typeof ResizeObserver !== 'undefined') {
;(wrapper['__wrap_o'] = new ResizeObserver(() => {
; (wrapper['__wrap_o'] = new ResizeObserver(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you remove the miscellaneous changes in this PR? Like this whitespace.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Comment on lines +99 to +101
if (injected) {
return null
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually no sorry this doesn't work at all… it's required to inject the script so that each instance of <Balancer /> is correctly balanced on SSR. Otherwise, you end up with this layout shift (on browsers that do not support text-wrap: balance, i.e. Safari)

ILUzvkAZ.mp4

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.

None yet

2 participants