Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
b859c5bd
Unverified
Commit
b859c5bd
authored
May 12, 2021
by
Janicklas Ralph
Committed by
GitHub
May 12, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor scriptloader option names (#25012)
parent
b43e996e
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
46 additions
and
67 deletions
+46
-67
experimental-script.tsx
packages/next/client/experimental-script.tsx
+9
-17
utils.ts
packages/next/next-server/lib/utils.ts
+1
-1
_document.tsx
packages/next/pages/_document.tsx
+6
-18
_document.js
test/integration/script-loader/pages/_document.js
+9
-9
index.js
test/integration/script-loader/pages/index.js
+2
-3
page1.js
test/integration/script-loader/pages/page1.js
+3
-3
page3.js
test/integration/script-loader/pages/page3.js
+3
-3
index.test.js
test/integration/script-loader/test/index.test.js
+13
-13
No files found.
packages/next/client/experimental-script.tsx
View file @
b859c5bd
...
...
@@ -8,12 +8,11 @@ const ScriptCache = new Map()
const
LoadCache
=
new
Set
()
export
interface
Props
extends
ScriptHTMLAttributes
<
HTMLScriptElement
>
{
strategy
?:
'afterInteracti
on'
|
'lazy'
|
'beforeInteraction
'
strategy
?:
'afterInteracti
ve'
|
'lazyOnload'
|
'beforeInteractive
'
id
?:
string
onLoad
?:
()
=>
void
onError
?:
()
=>
void
children
?:
React
.
ReactNode
preload
?:
boolean
}
const
ignoreProps
=
[
...
...
@@ -22,7 +21,6 @@ const ignoreProps = [
'children'
,
'onError'
,
'strategy'
,
'preload'
,
]
const
loadScript
=
(
props
:
Props
):
void
=>
{
...
...
@@ -93,10 +91,10 @@ const loadScript = (props: Props): void => {
}
function
handleClientScriptLoad
(
props
:
Props
)
{
const
{
strategy
=
'afterInteracti
on
'
}
=
props
if
(
strategy
===
'afterInteracti
on
'
)
{
const
{
strategy
=
'afterInteracti
ve
'
}
=
props
if
(
strategy
===
'afterInteracti
ve
'
)
{
loadScript
(
props
)
}
else
if
(
strategy
===
'lazy'
)
{
}
else
if
(
strategy
===
'lazy
Onload
'
)
{
window
.
addEventListener
(
'load'
,
()
=>
{
requestIdleCallback
(()
=>
loadScript
(
props
))
})
...
...
@@ -122,9 +120,8 @@ function Script(props: Props): JSX.Element | null {
src
=
''
,
onLoad
=
()
=>
{},
dangerouslySetInnerHTML
,
strategy
=
'afterInteracti
on
'
,
strategy
=
'afterInteracti
ve
'
,
onError
,
preload
=
false
,
...
restProps
}
=
props
...
...
@@ -132,9 +129,9 @@ function Script(props: Props): JSX.Element | null {
const
{
updateScripts
,
scripts
}
=
useContext
(
HeadManagerContext
)
useEffect
(()
=>
{
if
(
strategy
===
'afterInteracti
on
'
)
{
if
(
strategy
===
'afterInteracti
ve
'
)
{
loadScript
(
props
)
}
else
if
(
strategy
===
'lazy'
)
{
}
else
if
(
strategy
===
'lazy
Onload
'
)
{
loadLazyScript
(
props
)
}
},
[
props
,
strategy
])
...
...
@@ -143,14 +140,9 @@ function Script(props: Props): JSX.Element | null {
return
null
}
if
(
strategy
===
'afterInteraction'
)
{
if
(
updateScripts
&&
preload
)
{
scripts
.
afterInteraction
=
(
scripts
.
afterInteraction
||
[]).
concat
([
src
])
updateScripts
(
scripts
)
}
}
else
if
(
strategy
===
'beforeInteraction'
)
{
if
(
strategy
===
'beforeInteractive'
)
{
if
(
updateScripts
)
{
scripts
.
beforeInteracti
on
=
(
scripts
.
beforeInteraction
||
[]).
concat
([
scripts
.
beforeInteracti
ve
=
(
scripts
.
beforeInteractive
||
[]).
concat
([
{
src
,
onLoad
,
...
...
packages/next/next-server/lib/utils.ts
View file @
b859c5bd
...
...
@@ -191,7 +191,7 @@ export type DocumentProps = DocumentInitialProps & {
unstable_runtimeJS
?:
false
unstable_JsPreload
?:
false
devOnlyCacheBusterQueryString
:
string
scriptLoader
:
{
afterInteracti
on
?:
string
[];
beforeInteraction
?:
any
[]
}
scriptLoader
:
{
afterInteracti
ve
?:
string
[];
beforeInteractive
?:
any
[]
}
locale
?:
string
}
...
...
packages/next/pages/_document.tsx
View file @
b859c5bd
...
...
@@ -256,7 +256,7 @@ export class Head extends Component<
})
return
[
...(
scriptLoader
.
beforeInteracti
on
||
[]).
map
((
file
)
=>
(
...(
scriptLoader
.
beforeInteracti
ve
||
[]).
map
((
file
)
=>
(
<
link
key=
{
file
.
src
}
nonce=
{
this
.
props
.
nonce
}
...
...
@@ -282,18 +282,6 @@ export class Head extends Component<
}
/>
)),
...(
scriptLoader
.
afterInteraction
||
[]).
map
((
file
:
string
)
=>
(
<
link
key=
{
file
}
nonce=
{
this
.
props
.
nonce
}
rel=
"preload"
href=
{
file
}
as=
"script"
crossOrigin=
{
this
.
props
.
crossOrigin
||
process
.
env
.
__NEXT_CROSS_ORIGIN
}
/>
)),
]
}
...
...
@@ -304,9 +292,9 @@ export class Head extends Component<
React
.
Children
.
forEach
(
children
,
(
child
:
any
)
=>
{
if
(
child
.
type
===
Script
)
{
if
(
child
.
props
.
strategy
===
'beforeInteracti
on
'
)
{
scriptLoader
.
beforeInteracti
on
=
(
scriptLoader
.
beforeInteracti
on
||
[]
if
(
child
.
props
.
strategy
===
'beforeInteracti
ve
'
)
{
scriptLoader
.
beforeInteracti
ve
=
(
scriptLoader
.
beforeInteracti
ve
||
[]
).
concat
([
{
...
child
.
props
,
...
...
@@ -314,7 +302,7 @@ export class Head extends Component<
])
return
}
else
if
(
[
'lazy
'
,
'afterInteraction
'
].
includes
(
child
.
props
.
strategy
)
[
'lazy
Onload'
,
'afterInteractive
'
].
includes
(
child
.
props
.
strategy
)
)
{
scriptLoaderItems
.
push
(
child
.
props
)
return
...
...
@@ -668,7 +656,7 @@ export class NextScript extends Component<OriginProps> {
getPreNextScripts
()
{
const
{
scriptLoader
}
=
this
.
context
return
(
scriptLoader
.
beforeInteracti
on
||
[]).
map
(
return
(
scriptLoader
.
beforeInteracti
ve
||
[]).
map
(
(
file
:
ScriptLoaderProps
)
=>
{
const
{
strategy
,
...
props
}
=
file
return
(
...
...
test/integration/script-loader/pages/_document.js
View file @
b859c5bd
...
...
@@ -21,14 +21,14 @@ export default class MyDocument extends Document {
href
=
"https://fonts.googleapis.com/css?family=Voces"
/>
<
Script
id
=
"documentAfterInteracti
on
"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=documentAfterInteracti
on
"
strategy
=
"afterInteracti
on
"
id
=
"documentAfterInteracti
ve
"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=documentAfterInteracti
ve
"
strategy
=
"afterInteracti
ve
"
><
/Script
>
<
Script
id
=
"documentLazy"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=documentLazy"
strategy
=
"lazy"
id
=
"documentLazy
Onload
"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=documentLazy
Onload
"
strategy
=
"lazy
Onload
"
><
/Script
>
<
Script
id
=
"documentBlock"
...
...
@@ -36,9 +36,9 @@ export default class MyDocument extends Document {
strategy
=
"dangerouslyBlockRendering"
><
/Script
>
<
Script
id
=
"documentBeforeInteracti
on
"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=documentBeforeInteracti
on
"
strategy
=
"beforeInteracti
on
"
id
=
"documentBeforeInteracti
ve
"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=documentBeforeInteracti
ve
"
strategy
=
"beforeInteracti
ve
"
><
/Script
>
<
/Head
>
<
body
>
...
...
test/integration/script-loader/pages/index.js
View file @
b859c5bd
...
...
@@ -4,9 +4,8 @@ const Page = () => {
return
(
<
div
class
=
"container"
>
<
Script
id
=
"scriptAfterInteraction"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptAfterInteraction"
preload
id
=
"scriptAfterInteractive"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptAfterInteractive"
><
/Script
>
<
div
>
index
<
/div
>
<
/div
>
...
...
test/integration/script-loader/pages/page1.js
View file @
b859c5bd
...
...
@@ -4,9 +4,9 @@ const Page = () => {
return
(
<
div
class
=
"container"
>
<
Script
id
=
"scriptBeforeInteracti
on
"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptBeforeInteracti
on
"
strategy
=
"beforeInteracti
on
"
id
=
"scriptBeforeInteracti
ve
"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptBeforeInteracti
ve
"
strategy
=
"beforeInteracti
ve
"
><
/Script
>
<
div
>
page1
<
/div
>
<
/div
>
...
...
test/integration/script-loader/pages/page3.js
View file @
b859c5bd
...
...
@@ -11,9 +11,9 @@ const Page = () => {
})`
}
<
/Script
>
<
Script
id
=
"scriptLazy"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptLazy"
strategy
=
"lazy"
id
=
"scriptLazy
Onload
"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptLazy
Onload
"
strategy
=
"lazy
Onload
"
><
/Script
>
<
div
>
page3
<
/div
>
<
/div
>
...
...
test/integration/script-loader/test/index.test.js
View file @
b859c5bd
...
...
@@ -34,7 +34,7 @@ describe('Script Loader', () => {
stopApp
(
server
)
})
it
(
'priority afterInteracti
on
'
,
async
()
=>
{
it
(
'priority afterInteracti
ve
'
,
async
()
=>
{
let
browser
try
{
browser
=
await
webdriver
(
appPort
,
'/'
)
...
...
@@ -58,16 +58,16 @@ describe('Script Loader', () => {
expect
(
endPreloads
.
length
).
toBe
(
0
)
}
// afterInteracti
on
script in page
await
test
(
'scriptAfterInteracti
on
'
)
// afterInteracti
on
script in _document
await
test
(
'documentAfterInteracti
on
'
)
// afterInteracti
ve
script in page
await
test
(
'scriptAfterInteracti
ve
'
)
// afterInteracti
ve
script in _document
await
test
(
'documentAfterInteracti
ve
'
)
}
finally
{
if
(
browser
)
await
browser
.
close
()
}
})
it
(
'priority lazy'
,
async
()
=>
{
it
(
'priority lazy
Onload
'
,
async
()
=>
{
let
browser
try
{
browser
=
await
webdriver
(
appPort
,
'/page3'
)
...
...
@@ -87,16 +87,16 @@ describe('Script Loader', () => {
expect
(
endScripts
.
length
).
toBe
(
0
)
}
//
Lazy
script in page
await
test
(
'scriptLazy'
)
//
Lazy
script in _document
await
test
(
'documentLazy'
)
//
lazyOnload
script in page
await
test
(
'scriptLazy
Onload
'
)
//
lazyOnload
script in _document
await
test
(
'documentLazy
Onload
'
)
}
finally
{
if
(
browser
)
await
browser
.
close
()
}
})
it
(
'priority beforeInteracti
on
'
,
async
()
=>
{
it
(
'priority beforeInteracti
ve
'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/page1'
)
const
$
=
cheerio
.
load
(
html
)
...
...
@@ -130,8 +130,8 @@ describe('Script Loader', () => {
).
toBeGreaterThan
(
0
)
}
test
(
'scriptBeforeInteracti
on
'
)
test
(
'documentBeforeInteracti
on
'
)
test
(
'scriptBeforeInteracti
ve
'
)
test
(
'documentBeforeInteracti
ve
'
)
})
it
(
'onload fires correctly'
,
async
()
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment