-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add warning when viewport meta tag is added to _document.js (#13452)
Co-authored-by: Joe Haddad <[email protected]> Co-authored-by: Joe Haddad <[email protected]>
- Loading branch information
1 parent
3623d44
commit 1f5bbb3
Showing
6 changed files
with
135 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Viewport `meta` tags should not be used in `_document.js`'s `<Head>` | ||
|
||
#### Why This Error Occurred | ||
|
||
Adding `<meta name="viewport" ...>` in `pages/_document.js` will lead to unexpected results since it cannot be deduped. | ||
The viewport tag should be handled by `next/head` in `pages/_app.js`. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Set your viewport `meta` tag in `pages/_app.js` instead: | ||
|
||
```jsx | ||
// pages/_app.js | ||
import React from 'react' | ||
import Head from 'next/head' | ||
|
||
function MyApp({ Component, pageProps }) { | ||
return ( | ||
<> | ||
<Head> | ||
<meta name="viewport" content="viewport-fit=cover" /> | ||
</Head> | ||
<Component {...pageProps} /> | ||
</> | ||
) | ||
} | ||
|
||
export default MyApp | ||
``` | ||
|
||
### Useful Links | ||
|
||
- [Issue #13230](https://github.com/zeit/next.js/issues/13230), which led to the creation of this warning. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
test/integration/document-head-warnings/pages/_document.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Document, { Html, Head, Main, NextScript } from 'next/document' | ||
|
||
class MyDocument extends Document { | ||
render() { | ||
return ( | ||
<Html> | ||
<Head crossOrigin="anonymous"> | ||
<title>Check out this title!</title> | ||
<meta name="viewport" content="viewport-fit=cover" /> | ||
</Head> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
) | ||
} | ||
} | ||
|
||
export default MyDocument |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function Hi() { | ||
return ( | ||
<div> | ||
<p>Hello world!</p> | ||
<style jsx>{` | ||
p { | ||
font-size: 16.4px; | ||
} | ||
`}</style> | ||
</div> | ||
) | ||
} | ||
|
||
export default Hi |
43 changes: 43 additions & 0 deletions
43
test/integration/document-head-warnings/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import { renderViaHTTP, findPort, launchApp, killApp } from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 60) | ||
const appDir = join(__dirname, '..') | ||
let output | ||
|
||
describe('Custom Document Head Warnings', () => { | ||
beforeAll(async () => { | ||
const handleOutput = (msg) => { | ||
output += msg | ||
} | ||
const appPort = await findPort() | ||
const app = await launchApp(appDir, appPort, { | ||
onStdout: handleOutput, | ||
onStderr: handleOutput, | ||
}) | ||
await renderViaHTTP(appPort, '/') | ||
await killApp(app) | ||
}) | ||
|
||
describe('development mode', () => { | ||
it('warns when using a <title> in document/head', () => { | ||
expect(output).toMatch( | ||
/.*Warning: <title> should not be used in _document.js's <Head>\..*/ | ||
) | ||
}) | ||
|
||
it('warns when using viewport meta tags in document/head', () => { | ||
expect(output).toMatch( | ||
/.*Warning: viewport meta tags should not be used in _document.js's <Head>\..*/ | ||
) | ||
}) | ||
|
||
it('warns when using a crossOrigin attribute on document/head', () => { | ||
expect(output).toMatch( | ||
/.*Warning: `Head` attribute `crossOrigin` is deprecated\..*/ | ||
) | ||
}) | ||
}) | ||
}) |