-
Notifications
You must be signed in to change notification settings - Fork 30k
[Script] Allow next/script to be placed in _document body
#37894
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ import { NextInstance } from 'test/lib/next-modes/base' | |
| import { BrowserInterface } from 'test/lib/browsers/base' | ||
| import { check } from 'next-test-utils' | ||
|
|
||
| describe('beforeInteractive', () => { | ||
| describe('beforeInteractive in document Head', () => { | ||
| let next: NextInstance | ||
|
|
||
| beforeAll(async () => { | ||
|
|
@@ -31,9 +31,67 @@ describe('beforeInteractive', () => { | |
| ) | ||
| } | ||
| `, | ||
| 'pages/index.js': ` | ||
| 'pages/index.js': ` | ||
| export default function Home() { | ||
| return ( | ||
| <> | ||
| <p>Home page</p> | ||
| </> | ||
| ) | ||
| } | ||
| `, | ||
| }, | ||
| dependencies: { | ||
| react: '17.0.2', | ||
| 'react-dom': '17.0.2', | ||
| }, | ||
| }) | ||
| }) | ||
| afterAll(() => next.destroy()) | ||
|
|
||
| it('Script is injected server-side', async () => { | ||
| let browser: BrowserInterface | ||
|
|
||
| try { | ||
| browser = await webdriver(next.url, '/') | ||
|
|
||
| const script = await browser.eval( | ||
| `document.querySelector('script[data-nscript="beforeInteractive"]')` | ||
| ) | ||
| expect(script).not.toBeNull() | ||
| } finally { | ||
| if (browser) await browser.close() | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| describe('beforeInteractive in document body', () => { | ||
| let next: NextInstance | ||
|
|
||
| beforeAll(async () => { | ||
| next = await createNext({ | ||
| files: { | ||
| 'pages/_document.js': ` | ||
| import { Html, Head, Main, NextScript } from 'next/document' | ||
| import Script from 'next/script' | ||
|
|
||
|
|
||
| export default function Document() { | ||
| return ( | ||
| <Html> | ||
| <Head /> | ||
| <body> | ||
| <Main /> | ||
| <NextScript /> | ||
| <Script | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah the idea behind However, I understand that it can be confusing for users to place a script in CC @janicklas-ralph for his thoughts too.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, I think it does make sense that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a note 🙏 |
||
| src="https://www.google-analytics.com/analytics.js" | ||
| strategy="beforeInteractive" | ||
| /> | ||
| </body> | ||
| </Html> | ||
| ) | ||
| } | ||
| `, | ||
| 'pages/index.js': ` | ||
| export default function Home() { | ||
| return ( | ||
| <> | ||
|
|
@@ -60,6 +118,7 @@ describe('beforeInteractive', () => { | |
| const script = await browser.eval( | ||
| `document.querySelector('script[data-nscript="beforeInteractive"]')` | ||
| ) | ||
|
|
||
| expect(script).not.toBeNull() | ||
| } finally { | ||
| if (browser) await browser.close() | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intended that only Script tags are allow here?
I had my code to check whether a certain env variables set or not before including Script tag and it didn't work. Here my code sample:
Because there were two Script tags so I wrapped them with a React.Fragment tag.
Question:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for spotting this. Not sure about having it work recursively, but It should work when wrapped in at least one level of
<Fragment>since that's the expected behavior for other elements in<Head>.Can you be open a new issue for this? I'll submit a patch to fix, but you can remove the fragment as a workaround for now (sorry for the inconvenience):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#38242