-
Notifications
You must be signed in to change notification settings - Fork 28k
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/script) onReady 'Undefined' in Dev, Works in Production #39993
Comments
I can reproduce the issue, and I have identified the cause.
It should be a confirmed bug of Next.js. |
I have tried to create a fix for the issue (see #40002), but I am afraid I can't make the test cases pass. There are some fundamental flaws of For now, I could only recommend you load third-party script manually, without using |
Dang. I was hopeful! So do you think there will ever be a fix for this issue or is it just going to be an "it is what it is" thing with next/script. In my example, what would be the recommended way to set this up without next/script so it's more reliable than the setTimeout hack? |
@jacobgraf And actually, the third-party script you included also introduce some race condition! Here I was trying to reproduce and debug your issue. Notice that
|
That's a good catch too! I'll ping them and see if they can fix that on their end too! Any other tips or tricks for getting this fixed in the meantime while I wait for a more permanent fix or is my setTimeout hack the best way for now? |
As you can see above, in development mode, So you can add a <Script
src="https://www.cognitoforms.com/f/seamless.js"
onReady={() => {
if (typeof Cognito !== 'undefined') {
Cognito.mount("216", "#contact-form");
}
}}
/> This will work for now (in both development and production mode) and will continue to work if #40002 is merged. |
Fixes #39993. Before the PR: - `next/script` component mount, `useEffect` for `onReady` executes - The script's cacheKey is not added to `LoadCache`, skip `onReady` - The second `useEffect` for `loadScript` executes - The script's cacheKey is added to `LoadCache` even if it might not fully load yet - Because of React's strict mode, `useEffect` for `onReady` executes again - Since the script's cacheKey is in `LoadCache`, `onReady` is called (even when the script is not loaded yet) - After the script is actually loaded, inside the `script.onload` event handler the `onReady` is called again After the PR: - `next/script` component mount, `useEffect` for `onReady` executes - The script's cacheKey is not added to `LoadCache`, `useEffect` skips `onReady` - The second `useEffect` for `loadScript` executes - The script's cacheKey is added to `LoadCache` only if it is an inline script - Because of React's strict mode, `useEffect` for `onReady` executes again - The script is not yet loaded, its cacheKey is not in `LoadCache`, `useEffect` skips `onReady` again - After the script is actually loaded, inside the `script.onload` event handler the `onReady` is finally called In short, the PR resolves a race condition that only occurs under React strict mode (and makes the `next/script` component more concurrent rendering resilient). ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md`
Your onLoad method had a spelling error. Could this be the cause of the weird behavior? |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
Brave Version 1.42.97 Chromium: 104.0.5112.102 (Official Build) (arm64)
How are you deploying your application? (if relevant)
Vercel
Describe the Bug
When using the onReady method, the external JS is not available and returns undefined when running in dev mode. It works, as expected, in production build (both locally, and on Vercel)
Expected Behavior
External script resources should be loaded and available before onReady fires.
Link to reproduction
https://github.com/jacobgraf/next-script-onready-issue
To Reproduce
Run development build and load the page. You will receive an Unhandled Runtime Error - ReferenceError: Cognito is not defined.
If you run a production build, Cognito is available when onReady fires and the form loads, as expected.
The text was updated successfully, but these errors were encountered: