-
Notifications
You must be signed in to change notification settings - Fork 7
added _app.js and _document.js and set up getInitialProps #106
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 |
---|---|---|
@@ -1,26 +1,27 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es6": true, | ||
"jest": true | ||
"env": { | ||
"browser": true, | ||
"es6": true, | ||
"jest": true | ||
}, | ||
"extends": ["airbnb", "prettier"], | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parser": "babel-eslint", | ||
"parserOptions": { | ||
"ecmaFeatures": { | ||
"jsx": true | ||
}, | ||
"extends": ["airbnb", "prettier"], | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parser": "babel-eslint", | ||
"parserOptions": { | ||
"ecmaFeatures": { | ||
"jsx": true | ||
}, | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"react" | ||
], | ||
"rules": { | ||
"react/prop-types": ["error"] | ||
} | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"plugins": ["react"], | ||
"rules": { | ||
"react/prop-types": ["error"], | ||
"react/jsx-props-no-spreading": ["warn"], | ||
"react/forbid-prop-types": ["warn"], | ||
"no-unused-vars": ["warn"] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ | |
], | ||
"coveragePathIgnorePatterns": [ | ||
"<rootDir>/build/", | ||
"<rootDir>/default/src/pages/_document.jsx", | ||
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. removed since it is basic default code with only changes to head, but items in the head are tested in separate component tests. 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. It shouldn't be ignored from code coverage |
||
"<rootDir>/out/", | ||
"<rootDir>/.next/", | ||
"<rootDir>/node_modules/", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ | |
"collectCoverageFrom": ["**/*.{js,jsx,ts,tsx}"], | ||
"coveragePathIgnorePatterns": [ | ||
"<rootDir>/build/", | ||
"<rootDir>/src/pages/_document.jsx", | ||
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. Shouldn't be ignored - not a good example to set. If you look at the client-internal repo forked from this, I believe I have an example of testing the _app.jsx or _document.jsx |
||
"<rootDir>/out/", | ||
"<rootDir>/.next/", | ||
"<rootDir>/node_modules/", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
{ | ||
"presets": ["@babel/preset-env","next/babel"] | ||
"presets": [ | ||
[ | ||
"next/babel", | ||
{ | ||
"preset-env": { "targets": { "node": "current" } } | ||
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. this was required to be able to extend a class in document.js 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. Fixes #127? |
||
} | ||
] | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ | |
}, | ||
"plugins": ["react"], | ||
"rules": { | ||
"react/prop-types": ["error"] | ||
"react/prop-types": ["error"], | ||
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. Please revert these and use comments for single-line excludes |
||
"react/jsx-props-no-spreading": ["warn"], | ||
"react/forbid-prop-types": ["warn"], | ||
"no-unused-vars": ["warn"] | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import Head from 'next/head' | ||
|
||
const PageTitle = ({ title }) => { | ||
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. change to title since that is the one thing that we may want to change from components outside document.js 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. Is the key really necessary? I don't think eslint-react nor react is going to issue a warning here since it's wrapped in the element |
||
return ( | ||
<Head> | ||
<title key="title">{title}</title> | ||
</Head> | ||
) | ||
} | ||
|
||
PageTitle.propTypes = { | ||
title: PropTypes.string.isRequired | ||
} | ||
|
||
export default PageTitle |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,30 @@ | ||
import React from 'react' | ||
import ShallowRenderer from 'react-test-renderer/shallow' | ||
import GlobalHead from '.' | ||
import PageTitle from '.' | ||
|
||
describe('GlobalHead', () => { | ||
describe('PageTitle', () => { | ||
// Test uses ShallowRenderer.render() instead of TestRenderer.create() | ||
// because <GlobalHead> encapsulates NextJS <Head> which in turn uses | ||
// because <PageTitle> encapsulates NextJS <Head> which in turn uses | ||
// React <Helmet> and Helmet doesn't show up in a normal component render. | ||
it('creates a <head> element suitable for global usage across all pages', () => { | ||
const renderer = new ShallowRenderer(); | ||
renderer.render( | ||
<GlobalHead title="mock title for test" /> | ||
) | ||
const renderer = new ShallowRenderer() | ||
renderer.render(<PageTitle title="mock title for test" />) | ||
const result = renderer.getRenderOutput() | ||
expect(result).toMatchSnapshot() | ||
}) | ||
it('populates the specified page title', () => { | ||
const title = "mockTitle" | ||
const renderer = new ShallowRenderer(); | ||
renderer.render( | ||
<GlobalHead title={title} /> | ||
) | ||
const title = 'mockTitle' | ||
const renderer = new ShallowRenderer() | ||
renderer.render(<PageTitle title={title} />) | ||
const result = renderer.getRenderOutput() | ||
// Expect a React module with a populated <title> | ||
expect(result.props.children).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
type: 'title', | ||
props: { | ||
children: title | ||
} | ||
}) | ||
]) | ||
expect.objectContaining({ | ||
type: 'title', | ||
props: { | ||
children: title | ||
} | ||
}) | ||
) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`PageTitle creates a <head> element suitable for global usage across all pages 1`] = ` | ||
<Head> | ||
<title> | ||
mock title for test | ||
</title> | ||
</Head> | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './PageTitle' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* _document is only rendered on the server side and not on the client side | ||
* Event handlers like onClick can't be added to this file | ||
*/ | ||
import React from 'react' | ||
import Document, { Html, Head, Main, NextScript } from 'next/document' | ||
import Flavicon from '../components/molecules/Favicon' | ||
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. Flavicon? Is that a typo? |
||
import Manifest from '../components/molecules/Manifest' | ||
|
||
class MyDocument extends Document { | ||
static async getInitialProps(ctx) { | ||
const initialProps = await Document.getInitialProps(ctx) | ||
return { ...initialProps } | ||
} | ||
|
||
render() { | ||
return ( | ||
<Html lang="en"> | ||
<Head> | ||
{/* HEADER ELEMENTS THAT GO HERE: | ||
* meta tags, global scripts, global stylesheets, rel links, etc. | ||
* Tracking scripts like Google Analytics (try https://github.com/react-ga/react-ga) | ||
*/} | ||
<Flavicon /> | ||
<Manifest /> | ||
</Head> | ||
<body> | ||
{/* NOSCRIPT: place them inside a new <noscript> tag here */} | ||
<Main /> | ||
<NextScript /> | ||
{/* FOOTER: If your footer will not change based on api calls, place here, otherwise place in _app.js */} | ||
</body> | ||
</Html> | ||
) | ||
} | ||
} | ||
|
||
export default MyDocument |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
import React from 'react' | ||
import Link from 'next/link' | ||
import GlobalHead from '../../components/organisms/GlobalHead' | ||
import PageTitle from '../../components/organisms/PageTitle' | ||
|
||
const Home = () => { | ||
return ( | ||
<> | ||
<GlobalHead title="%%APPNAME%%" /> | ||
<PageTitle title="%%APPNAME%%" /> | ||
<p>Welcome to the boilerplate React app using NextJS!</p> | ||
<p> | ||
Try navigating to | ||
|
||
<Link href="/example/second-page">another page</Link> | ||
|
||
and observe the URL changing | ||
</p> | ||
</> | ||
) | ||
} | ||
|
||
// estlin-disable-next-line | ||
Home.getInitialProps = async context => { | ||
/* Here you can make your pre-fetch for this page, it's a pre-rendering step | ||
* only available for pages. | ||
* Useful parameters in context object: req, res, query (passed from 4th render argument) | ||
* It expects an object with props to be returned | ||
*/ | ||
return {} | ||
} | ||
|
||
export default Home |
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.
Can you revert these rule changes and use comments to exclude single lines please?