Unverified Commit f1e6bc9e authored by Steven's avatar Steven Committed by GitHub

Rename `next/script` interface Props to ScriptProps (#26990)

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 
parent 556216b1
...@@ -7,7 +7,7 @@ import { requestIdleCallback } from './request-idle-callback' ...@@ -7,7 +7,7 @@ import { requestIdleCallback } from './request-idle-callback'
const ScriptCache = new Map() const ScriptCache = new Map()
const LoadCache = new Set() const LoadCache = new Set()
export interface Props extends ScriptHTMLAttributes<HTMLScriptElement> { export interface ScriptProps extends ScriptHTMLAttributes<HTMLScriptElement> {
strategy?: 'afterInteractive' | 'lazyOnload' | 'beforeInteractive' strategy?: 'afterInteractive' | 'lazyOnload' | 'beforeInteractive'
id?: string id?: string
onLoad?: () => void onLoad?: () => void
...@@ -15,6 +15,11 @@ export interface Props extends ScriptHTMLAttributes<HTMLScriptElement> { ...@@ -15,6 +15,11 @@ export interface Props extends ScriptHTMLAttributes<HTMLScriptElement> {
children?: React.ReactNode children?: React.ReactNode
} }
/**
* @deprecated Use `ScriptProps` instead.
*/
export type Props = ScriptProps
const ignoreProps = [ const ignoreProps = [
'onLoad', 'onLoad',
'dangerouslySetInnerHTML', 'dangerouslySetInnerHTML',
...@@ -23,7 +28,7 @@ const ignoreProps = [ ...@@ -23,7 +28,7 @@ const ignoreProps = [
'strategy', 'strategy',
] ]
const loadScript = (props: Props): void => { const loadScript = (props: ScriptProps): void => {
const { const {
src, src,
id, id,
...@@ -90,7 +95,7 @@ const loadScript = (props: Props): void => { ...@@ -90,7 +95,7 @@ const loadScript = (props: Props): void => {
document.body.appendChild(el) document.body.appendChild(el)
} }
function handleClientScriptLoad(props: Props) { function handleClientScriptLoad(props: ScriptProps) {
const { strategy = 'afterInteractive' } = props const { strategy = 'afterInteractive' } = props
if (strategy === 'afterInteractive') { if (strategy === 'afterInteractive') {
loadScript(props) loadScript(props)
...@@ -101,7 +106,7 @@ function handleClientScriptLoad(props: Props) { ...@@ -101,7 +106,7 @@ function handleClientScriptLoad(props: Props) {
} }
} }
function loadLazyScript(props: Props) { function loadLazyScript(props: ScriptProps) {
if (document.readyState === 'complete') { if (document.readyState === 'complete') {
requestIdleCallback(() => loadScript(props)) requestIdleCallback(() => loadScript(props))
} else { } else {
...@@ -111,11 +116,11 @@ function loadLazyScript(props: Props) { ...@@ -111,11 +116,11 @@ function loadLazyScript(props: Props) {
} }
} }
export function initScriptLoader(scriptLoaderItems: Props[]) { export function initScriptLoader(scriptLoaderItems: ScriptProps[]) {
scriptLoaderItems.forEach(handleClientScriptLoad) scriptLoaderItems.forEach(handleClientScriptLoad)
} }
function Script(props: Props): JSX.Element | null { function Script(props: ScriptProps): JSX.Element | null {
const { const {
src = '', src = '',
onLoad = () => {}, onLoad = () => {},
......
...@@ -14,7 +14,7 @@ import { ...@@ -14,7 +14,7 @@ import {
import { BuildManifest, getPageFiles } from '../server/get-page-files' import { BuildManifest, getPageFiles } from '../server/get-page-files'
import { cleanAmpPath } from '../server/utils' import { cleanAmpPath } from '../server/utils'
import { htmlEscapeJsonString } from '../server/htmlescape' import { htmlEscapeJsonString } from '../server/htmlescape'
import Script, { Props as ScriptLoaderProps } from '../client/script' import Script, { ScriptProps } from '../client/script'
export { DocumentContext, DocumentInitialProps, DocumentProps } export { DocumentContext, DocumentInitialProps, DocumentProps }
...@@ -76,7 +76,7 @@ function getPreNextScripts(context: DocumentProps, props: OriginProps) { ...@@ -76,7 +76,7 @@ function getPreNextScripts(context: DocumentProps, props: OriginProps) {
const { scriptLoader, disableOptimizedLoading } = context const { scriptLoader, disableOptimizedLoading } = context
return (scriptLoader.beforeInteractive || []).map( return (scriptLoader.beforeInteractive || []).map(
(file: ScriptLoaderProps, index: number) => { (file: ScriptProps, index: number) => {
const { strategy, ...scriptProps } = file const { strategy, ...scriptProps } = file
return ( return (
<script <script
...@@ -408,7 +408,7 @@ export class Head extends Component< ...@@ -408,7 +408,7 @@ export class Head extends Component<
handleDocumentScriptLoaderItems(children: React.ReactNode): ReactNode[] { handleDocumentScriptLoaderItems(children: React.ReactNode): ReactNode[] {
const { scriptLoader } = this.context const { scriptLoader } = this.context
const scriptLoaderItems: ScriptLoaderProps[] = [] const scriptLoaderItems: ScriptProps[] = []
const filteredChildren: ReactNode[] = [] const filteredChildren: ReactNode[] = []
React.Children.forEach(children, (child: any) => { React.Children.forEach(children, (child: any) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment