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

Fixes beforeInteractive scripts failing in custom document #37000

Merged
merged 3 commits into from
May 18, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
import { BuildManifest, getPageFiles } from '../server/get-page-files'
import { cleanAmpPath } from '../server/utils'
import { htmlEscapeJsonString } from '../server/htmlescape'
import Script, { ScriptProps } from '../client/script'
import { ScriptProps } from '../client/script'
import isError from '../lib/is-error'

import { HtmlContext } from '../shared/lib/html-context'
Expand Down Expand Up @@ -605,7 +605,7 @@ export class Head extends Component<
const filteredChildren: ReactNode[] = []

React.Children.forEach(children, (child: any) => {
if (child.type === Script) {
if (typeof child.type === 'function' && child.type.name === 'Script') {
Copy link
Member

Choose a reason for hiding this comment

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

I think the previous check of child.type === Script was correct as that ensures it is the next/script component and not a custom component named Script

The issue seemed to be from the import for next/script in _document being local ../client/script and the other being non-local causing different instances to be loaded. The tests passed because the monorepo version of Next.js is being used and webpack treated both instances the same.

We should be able to add a regression test for this by adding it to an isolated test (test/e2e or test/production)

We can also fix the different instances being loaded by updating this line:

/^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|document|link|image|constants|dynamic)$)|string-hash$)/

to: /^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|document|link|image|constants|dynamic|script)$)|string-hash$)/

Copy link
Collaborator Author

@housseindjirdeh housseindjirdeh May 18, 2022

Choose a reason for hiding this comment

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

Thanks so much clarifying, I wasn't aware that different instances of the same import could have been the issue.

Just fixed it and added a test 👍

if (child.props.strategy === 'beforeInteractive') {
scriptLoader.beforeInteractive = (
scriptLoader.beforeInteractive || []
Expand Down