-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
feat(scripts): deprecate implicit proxy and $script
#379
Conversation
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'm good with it. This will probably break a few things that is already in place in nuxt scripts tests and playground
Do you have any opinions though? This will be implemented as a non breaking change |
Is it possible to maybe use a build-time flag that is disabled by default (or enabled if you want it non breaking) that would treeshake the old behavior ? Also in your example : script.then(() => {})
script.load(() => {}) Is |
IMO, removing proxy is probably not hurting too much DX as it can be hard to understand how things works under the hood for beginners. Also because it's hard for us to stub everything in SSR and determine what behavior we should do if anything is done in SSR but missing client side. If it's for debugging purpose because we're only getting the proxy object, i think it's possible to explore some Object reflection utilities in dev mode to access to the object and know what is happening + add a documentation aboutit . |
Thanks, I was a bit on the fence so that's useful 🙂 |
Or we could do a breaking change with the composable directly returning the promise maybe ? To avoid users to use |
yes anything previously on |
One thing i didn't think about now that the promise is to level. I don't know what impact this will have on users since a lot will probably be confused with typescript type being a Promise and not being allowed to use |
Yes, this has always been an issue nuxt/scripts#46, the only difference is it can also be top-level now. The I'll investigate some runtime solutions on this |
$script
Background
I'm considering one major API change before the official Nuxt Scripts announcement regarding the proxy API, this will be backwards compatible.
The proxy API provides a good DX for end users IMO and it allows good future-proofing when loading scripts using alternative strategies such as web workers.
However, it introduces behaviour that is hard to predict, and there's no way to know what will happen when you call the function universally across all scripts.
For users who don't consider how the code can work, it would be easy to assume they are interacting with the loaded script. When it fails they may have a difficult time debugging as they'll only have a proxy to inspect.
For something like Google Analytics this isn't a big issue, see:
However, when users try and use the proxy API where there is a return, it can be super fiddly
So we're deprecating this behavior completely and only supporting the previous behavior by accessing the
proxy
value.We can see this implemented using
gtag
as:We can easily type this functionality so the user will always know what's going on.
This also allows us to simplify
useScript
, we can now return the$script
instance directly as is as there are no other data returned on the function.Technical Details
$script
, you should now access the previous script instance functions at the top level.proxy
object.Features
dataLayer.push()
now works without stubbing.instance
on the script instance which holds the reference to theuse()
output.proxy
on the script instance.