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

Avoid bundling next/script in the server build by default #40013

Merged
merged 2 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions packages/next/client/script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,6 @@ function Script(props: ScriptProps): JSX.Element | null {
return null
}

Object.defineProperty(Script, '__nextScript', { value: true })

export default Script
12 changes: 8 additions & 4 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import type {
DocumentType,
NEXT_DATA,
} from '../shared/lib/utils'
import type { ScriptProps } from '../client/script'

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 isError from '../lib/is-error'

import { HtmlContext } from '../shared/lib/html-context'
Expand Down Expand Up @@ -765,7 +765,10 @@ export class Head extends Component<HeadProps> {
{!hasCanonicalRel && (
<link
rel="canonical"
href={canonicalBase + cleanAmpPath(dangerousAsPath)}
href={
canonicalBase +
require('../server/utils').cleanAmpPath(dangerousAsPath)
}
/>
)}
shuding marked this conversation as resolved.
Show resolved Hide resolved
{/* https://www.ampproject.org/docs/fundamentals/optimize_amp#optimize-the-amp-runtime-loading */}
Expand Down Expand Up @@ -871,7 +874,8 @@ function handleDocumentScriptLoaderItems(
React.Children.forEach(combinedChildren, (child: any) => {
if (!child) return

if (child.type === Script) {
// When using the `next/script` component, register it in script loader.
if (child.type?.__nextScript) {
if (child.props.strategy === 'beforeInteractive') {
scriptLoader.beforeInteractive = (
scriptLoader.beforeInteractive || []
Expand Down