Skip to content

Commit

Permalink
Rename next/script interface Props to ScriptProps (#26990)
Browse files Browse the repository at this point in the history
This will ensure `next/script` follows the same naming convention as `next/image`. For example:

```js
import Image, { ImageProps } from 'next/image'
import Script, { ScriptProps } from 'next/script'
```

Fixes #26290
  • Loading branch information
styfle authored Jul 7, 2021
1 parent 556216b commit f1e6bc9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions packages/next/client/script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import { requestIdleCallback } from './request-idle-callback'
const ScriptCache = new Map()
const LoadCache = new Set()

export interface Props extends ScriptHTMLAttributes<HTMLScriptElement> {
export interface ScriptProps extends ScriptHTMLAttributes<HTMLScriptElement> {
strategy?: 'afterInteractive' | 'lazyOnload' | 'beforeInteractive'
id?: string
onLoad?: () => void
onError?: () => void
children?: React.ReactNode
}

/**
* @deprecated Use `ScriptProps` instead.
*/
export type Props = ScriptProps

const ignoreProps = [
'onLoad',
'dangerouslySetInnerHTML',
Expand All @@ -23,7 +28,7 @@ const ignoreProps = [
'strategy',
]

const loadScript = (props: Props): void => {
const loadScript = (props: ScriptProps): void => {
const {
src,
id,
Expand Down Expand Up @@ -90,7 +95,7 @@ const loadScript = (props: Props): void => {
document.body.appendChild(el)
}

function handleClientScriptLoad(props: Props) {
function handleClientScriptLoad(props: ScriptProps) {
const { strategy = 'afterInteractive' } = props
if (strategy === 'afterInteractive') {
loadScript(props)
Expand All @@ -101,7 +106,7 @@ function handleClientScriptLoad(props: Props) {
}
}

function loadLazyScript(props: Props) {
function loadLazyScript(props: ScriptProps) {
if (document.readyState === 'complete') {
requestIdleCallback(() => loadScript(props))
} else {
Expand All @@ -111,11 +116,11 @@ function loadLazyScript(props: Props) {
}
}

export function initScriptLoader(scriptLoaderItems: Props[]) {
export function initScriptLoader(scriptLoaderItems: ScriptProps[]) {
scriptLoaderItems.forEach(handleClientScriptLoad)
}

function Script(props: Props): JSX.Element | null {
function Script(props: ScriptProps): JSX.Element | null {
const {
src = '',
onLoad = () => {},
Expand Down
6 changes: 3 additions & 3 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { BuildManifest, getPageFiles } from '../server/get-page-files'
import { cleanAmpPath } from '../server/utils'
import { htmlEscapeJsonString } from '../server/htmlescape'
import Script, { Props as ScriptLoaderProps } from '../client/script'
import Script, { ScriptProps } from '../client/script'

export { DocumentContext, DocumentInitialProps, DocumentProps }

Expand Down Expand Up @@ -76,7 +76,7 @@ function getPreNextScripts(context: DocumentProps, props: OriginProps) {
const { scriptLoader, disableOptimizedLoading } = context

return (scriptLoader.beforeInteractive || []).map(
(file: ScriptLoaderProps, index: number) => {
(file: ScriptProps, index: number) => {
const { strategy, ...scriptProps } = file
return (
<script
Expand Down Expand Up @@ -408,7 +408,7 @@ export class Head extends Component<

handleDocumentScriptLoaderItems(children: React.ReactNode): ReactNode[] {
const { scriptLoader } = this.context
const scriptLoaderItems: ScriptLoaderProps[] = []
const scriptLoaderItems: ScriptProps[] = []
const filteredChildren: ReactNode[] = []

React.Children.forEach(children, (child: any) => {
Expand Down

0 comments on commit f1e6bc9

Please sign in to comment.