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
26255a68
Unverified
Commit
26255a68
authored
Aug 20, 2021
by
Janicklas Ralph
Committed by
GitHub
Aug 20, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add data attribute to script component (#28310)
Add data attribute `data-nscript` to the script component
parent
8737ac08
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
11 additions
and
0 deletions
+11
-0
script.tsx
packages/next/client/script.tsx
+3
-0
_document.tsx
packages/next/pages/_document.tsx
+1
-0
index.test.js
test/integration/script-loader/test/index.test.js
+7
-0
No files found.
packages/next/client/script.tsx
View file @
26255a68
...
...
@@ -35,6 +35,7 @@ const loadScript = (props: ScriptProps): void => {
onLoad
=
()
=>
{},
dangerouslySetInnerHTML
,
children
=
''
,
strategy
=
'afterInteractive'
,
onError
,
}
=
props
...
...
@@ -98,6 +99,8 @@ const loadScript = (props: ScriptProps): void => {
el
.
setAttribute
(
attr
,
value
)
}
el
.
setAttribute
(
'data-nscript'
,
strategy
)
document
.
body
.
appendChild
(
el
)
}
...
...
packages/next/pages/_document.tsx
View file @
26255a68
...
...
@@ -84,6 +84,7 @@ function getPreNextScripts(context: HtmlProps, props: OriginProps) {
key=
{
scriptProps
.
src
||
index
}
defer=
{
!
disableOptimizedLoading
}
nonce=
{
props
.
nonce
}
data
-
nscript=
"beforeInteractive"
crossOrigin=
{
props
.
crossOrigin
||
process
.
env
.
__NEXT_CROSS_ORIGIN
}
/>
)
...
...
test/integration/script-loader/test/index.test.js
View file @
26255a68
...
...
@@ -42,12 +42,15 @@ describe('Script Loader', () => {
async
function
test
(
id
)
{
const
script
=
await
browser
.
elementById
(
id
)
const
dataAttr
=
await
script
.
getAttribute
(
'data-nscript'
)
const
endScripts
=
await
browser
.
elementsByCss
(
`#__NEXT_DATA__ ~ #
${
id
}
`
)
// Renders script tag
expect
(
script
).
toBeDefined
()
expect
(
dataAttr
).
toBeDefined
()
// Script is inserted at the end
expect
(
endScripts
.
length
).
toBe
(
1
)
}
...
...
@@ -77,12 +80,15 @@ describe('Script Loader', () => {
async
function
test
(
id
)
{
const
script
=
await
browser
.
elementById
(
id
)
const
dataAttr
=
await
script
.
getAttribute
(
'data-nscript'
)
const
endScripts
=
await
browser
.
elementsByCss
(
`#__NEXT_DATA__ ~ #
${
id
}
`
)
// Renders script tag
expect
(
script
).
toBeDefined
()
expect
(
dataAttr
).
toBeDefined
()
// Script is inserted at the end
expect
(
endScripts
.
length
).
toBe
(
1
)
}
...
...
@@ -105,6 +111,7 @@ describe('Script Loader', () => {
// Renders script tag
expect
(
script
.
length
).
toBe
(
1
)
expect
(
script
.
attr
(
'data-nscript'
)).
toBeDefined
()
// Script is inserted before NextScripts
expect
(
...
...
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