Open mateusfmello opened 5 months ago
Hi @mateusfmello, Thanks for reporting this, and sorry for the late reply. I'd prefer option 2 with a little improvement. We can add an id
or data-vue-recaptcha
to the script we inserted to head.
<script id="vue-recaptcha" src="..."></script>
So when we search for an existing tag, we can use document.querySelector('#vue-recaptcha')
What do you think about this?
Perfect, I think it's efficient.
I'll put it as one of the next tasks to do, as soon as possible I'll implement it and make a pull request
Description
On my website page there are 4 forms, two of which are components within the project, that is, each form/component is instantiated twice.
This is to be expected, as we want the same forms in different locations.
However, vue-recaptcha is instantiating the reCaptcha for each component, and this only needs to be done once, as it can be used globally.
This overloads page loading, even Page Spees Web DEV reports this as a problem, which in fact it is.
Faced with these problems, I turned to the vue recaptcha source code to analyze what might be happening. I noticed that in the
vue-recaptcha/src/script-manager/head.ts
file, the creation of the reCaptcha script tag was assigned inonMounted
, everything was fine until the first execution.I would like to make a PR with an improvement, but I would like to know what the maintainers think about this and two possible solutions.
First option: Add a flag to the Window called
window.vueRecaptchaInserted
and mark it astrue
, using it withinonMounted
in anif return
so that the method is not executed more than once.This is my preferred option because it is very objective, clear and more performant than the second option.
Second option: When running the
onMounted
method, perform a search in the DOM checking whether the TAG already exists, if it does not exist, you must continue executing the method, otherwise the method is terminated.Do you think this is something that needs to be resolved? Do you suggest another solution? Which option do you prefer?
Minimal Reproducible Example
1 - Create a component (MyForm) that uses
<vue-recaptcha />
; 2 - Instantiate this component several times:3 - Open the website and notice that Google reCaptcha is loaded the same number of times as you instantiated
<MyForm />
, for this you need to open theF12
developer tools and go to the network tab.System info