-
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from kirill-konshin/dev
Add support of getStaticProps and getServerSideProps
- Loading branch information
Showing
58 changed files
with
5,392 additions
and
4,826 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"useWorkspaces": true, | ||
"npmClient": "yarn" | ||
} |
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,7 @@ | ||
module.exports = { | ||
server: { | ||
command: 'yarn serve', | ||
debug: true, | ||
launchTimeout: 30000, | ||
}, | ||
}; |
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,6 @@ | ||
const config = require('./jest.config'); | ||
module.exports = { | ||
...config, | ||
preset: 'jest-puppeteer', | ||
coveragePathIgnorePatterns: config.coveragePathIgnorePatterns.concat('./jest-puppeteer.config.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "next-redux-wrapper-configs", | ||
"private": true, | ||
"version": "5.0.0" | ||
"version": "6.0.0" | ||
} |
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,2 @@ | ||
module.exports = require('next-redux-wrapper-configs/jest-puppeteer.config'); | ||
module.exports.server.port = 4000; |
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 @@ | ||
module.exports = require('next-redux-wrapper-configs/jest.config.puppeteer'); |
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,2 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/types/global" /> |
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,51 @@ | ||
{ | ||
"name": "next-redux-wrapper-demo-page", | ||
"private": true, | ||
"version": "6.0.0", | ||
"description": "Demo of redux wrapper for Next.js", | ||
"scripts": { | ||
"clean": "rimraf .next coverage", | ||
"test": "jest", | ||
"start": "next --port=4000", | ||
"build": "next build", | ||
"serve": "next start --port=4000" | ||
}, | ||
"dependencies": { | ||
"jsondiffpatch": "0.4.1", | ||
"next-redux-wrapper": "^6.0.0", | ||
"react": "16.13.1", | ||
"react-dom": "16.13.1", | ||
"react-redux": "7.2.0", | ||
"redux": "4.0.5", | ||
"redux-logger": "3.0.6" | ||
}, | ||
"devDependencies": { | ||
"@types/expect-puppeteer": "4.4.1", | ||
"@types/jest": "25.2.1", | ||
"@types/jest-environment-puppeteer": "4.3.1", | ||
"@types/puppeteer": "2.0.1", | ||
"@types/react": "16.9.35", | ||
"@types/react-dom": "16.9.8", | ||
"@types/react-redux": "7.1.8", | ||
"@types/redux-logger": "3.0.7", | ||
"@types/webpack-env": "1.15.2", | ||
"jest": "26.0.1", | ||
"jest-puppeteer": "4.4.0", | ||
"next": "9.4.0", | ||
"next-redux-wrapper-configs": "^6.0.0", | ||
"puppeteer": "3.0.4", | ||
"rimraf": "3.0.2", | ||
"ts-jest": "25.5.1", | ||
"typescript": "3.8.3" | ||
}, | ||
"author": "Kirill Konshin", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/kirill-konshin/next-redux-wrapper.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/kirill-konshin/next-redux-wrapper/issues" | ||
}, | ||
"homepage": "https://github.com/kirill-konshin/next-redux-wrapper", | ||
"license": "MIT" | ||
} |
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,29 @@ | ||
import {AnyAction} from 'redux'; | ||
import {HYDRATE} from 'next-redux-wrapper'; | ||
import {diff} from 'jsondiffpatch'; | ||
|
||
export interface State { | ||
page: string; | ||
} | ||
|
||
const reducer = (state: State = {page: 'init'}, action: AnyAction) => { | ||
switch (action.type) { | ||
case HYDRATE: | ||
const stateDiff = diff(state, action.payload) as any; | ||
const wasBumpedOnClient = stateDiff.page[0].endsWith('X'); | ||
console.log('HYDRATE action handler', {stateDiff, wasBumpedOnClient}); | ||
return { | ||
...state, | ||
...action.payload, | ||
page: wasBumpedOnClient ? state.page : action.payload.page, | ||
}; | ||
case 'PAGE': | ||
return {...state, page: action.payload}; | ||
case 'BUMP': | ||
return {...state, page: state.page + 'X'}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default reducer; |
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,19 @@ | ||
import {createStore, applyMiddleware} from 'redux'; | ||
import logger from 'redux-logger'; | ||
import {MakeStore, createWrapper, Context} from 'next-redux-wrapper'; | ||
import reducer, {State} from './reducer'; | ||
|
||
export const makeStore: MakeStore<State> = (context: Context) => { | ||
const store = createStore(reducer, applyMiddleware(logger)); | ||
|
||
if (module.hot) { | ||
module.hot.accept('./reducer', () => { | ||
console.log('Replacing reducer'); | ||
store.replaceReducer(require('./reducer').default); | ||
}); | ||
} | ||
|
||
return store; | ||
}; | ||
|
||
export const wrapper = createWrapper<State>(makeStore, {debug: true}); |
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,31 @@ | ||
import React, {Component} from 'react'; | ||
import Link from 'next/link'; | ||
import {NextPageContext} from 'next'; | ||
import {connect} from 'react-redux'; | ||
import {wrapper} from '../components/store'; | ||
import {State} from '../components/reducer'; | ||
|
||
class ErrorPage extends Component<State> { | ||
public static getInitialProps = ({store, pathname}: NextPageContext) => { | ||
console.log('2. Page.getInitialProps uses the store to dispatch things'); | ||
store.dispatch({type: 'PAGE', payload: 'was set in error page ' + pathname}); | ||
}; | ||
|
||
render() { | ||
const {page} = this.props; | ||
return ( | ||
<> | ||
<p> | ||
This is an error page, it also has access to store: <code>{page}</code> | ||
</p> | ||
<nav> | ||
<Link href="/"> | ||
<a>Navigate to index</a> | ||
</Link> | ||
</nav> | ||
</> | ||
); | ||
} | ||
} | ||
|
||
export default wrapper.withRedux(connect((state: State) => state)(ErrorPage)); |
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,35 @@ | ||
import React from 'react'; | ||
import {NextPage} from 'next'; | ||
import Link from 'next/link'; | ||
import {useSelector} from 'react-redux'; | ||
import {wrapper} from '../components/store'; | ||
import {State} from '../components/reducer'; | ||
|
||
export interface ConnectedPageProps { | ||
custom: string; | ||
} | ||
|
||
// Page itself is not connected to Redux Store, it has to render Provider to allow child components to connect to Redux Store | ||
const Page: NextPage<ConnectedPageProps> = ({custom}) => { | ||
const {page} = useSelector<State, State>(state => state); | ||
return ( | ||
<div className="index"> | ||
<pre>{JSON.stringify({page, custom}, null, 2)}</pre> | ||
<Link href="/other"> | ||
<a>Navigate</a> | ||
</Link> | ||
{' | '} | ||
<Link href="/error"> | ||
<a>Navigate to error</a> | ||
</Link> | ||
</div> | ||
); | ||
}; | ||
|
||
export const getServerSideProps = wrapper.getServerSideProps(({store, req}) => { | ||
console.log('2. Page.getServerSideProps uses the store to dispatch things'); | ||
store.dispatch({type: 'PAGE', payload: 'was set in index page ' + req.url}); | ||
return {props: {custom: 'custom'}}; | ||
}); | ||
|
||
export default wrapper.withRedux(Page); |
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,34 @@ | ||
import React from 'react'; | ||
import {NextPage} from 'next'; | ||
import Link from 'next/link'; | ||
import {useSelector, useDispatch} from 'react-redux'; | ||
import {State} from '../components/reducer'; | ||
import {wrapper} from '../components/store'; | ||
|
||
export const getStaticProps = wrapper.getStaticProps(({store, previewData}) => { | ||
console.log('2. Page.getStaticProps uses the store to dispatch things'); | ||
store.dispatch({type: 'PAGE', payload: 'was set in other page ' + JSON.stringify({previewData})}); | ||
}); | ||
|
||
const OtherPage: NextPage<State> = () => { | ||
const {page} = useSelector<State, State>(state => state); | ||
const dispatch = useDispatch(); | ||
const bump = () => dispatch({type: 'BUMP'}); | ||
return ( | ||
<div className="other"> | ||
<p>Using Next.js default prop in a wrapped component.</p> | ||
<pre>{JSON.stringify({page}, null, 2)}</pre> | ||
<nav> | ||
<button onClick={bump}>bump</button> | ||
<Link href="/"> | ||
<a>Navigate to index</a> | ||
</Link> | ||
<Link href="/other2"> | ||
<a>Navigate to other 2</a> | ||
</Link> | ||
</nav> | ||
</div> | ||
); | ||
}; | ||
|
||
export default wrapper.withRedux(OtherPage); |
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,34 @@ | ||
import React from 'react'; | ||
import {NextPage} from 'next'; | ||
import Link from 'next/link'; | ||
import {useSelector, useDispatch} from 'react-redux'; | ||
import {State} from '../components/reducer'; | ||
import {wrapper} from '../components/store'; | ||
|
||
export const getStaticProps = wrapper.getStaticProps(({store, previewData}) => { | ||
console.log('2. Page.getStaticProps uses the store to dispatch things'); | ||
store.dispatch({type: 'PAGE', payload: 'was set in other (SECOND) page ' + JSON.stringify({previewData})}); | ||
}); | ||
|
||
const OtherPage: NextPage<State> = () => { | ||
const {page} = useSelector<State, State>(state => state); | ||
const dispatch = useDispatch(); | ||
const bump = () => dispatch({type: 'BUMP'}); | ||
return ( | ||
<div className="other"> | ||
<p>Using Next.js default prop in a wrapped component.</p> | ||
<pre>{JSON.stringify({page}, null, 2)}</pre> | ||
<nav> | ||
<button onClick={bump}>bump</button> | ||
<Link href="/"> | ||
<a>Navigate to index</a> | ||
</Link> | ||
<Link href="/other"> | ||
<a>Navigate to other</a> | ||
</Link> | ||
</nav> | ||
</div> | ||
); | ||
}; | ||
|
||
export default wrapper.withRedux(OtherPage); |
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,29 @@ | ||
import config from '../jest-puppeteer.config'; | ||
|
||
const openPage = (url = '/') => page.goto(`http://localhost:${config.server.port}${url}`); | ||
|
||
describe('Basic integration', () => { | ||
it('shows the page', async () => { | ||
await openPage(); | ||
|
||
await page.waitForSelector('div.index'); | ||
|
||
await expect(page).toMatch('"page": "was set in index page /"'); | ||
await expect(page).toMatch('"custom": "custom"'); | ||
}); | ||
|
||
it('clicks the button', async () => { | ||
await openPage('/other'); | ||
|
||
await page.waitForSelector('div.other'); | ||
|
||
await expect(page).toMatch('"page": "was set in other page {}"'); | ||
|
||
await expect(page).toClick('a', {text: 'Navigate to index'}); | ||
|
||
await page.waitForSelector('div.index'); | ||
|
||
await expect(page).toMatch('"page": "was set in index page'); | ||
await expect(page).toMatch('"custom": "custom"'); | ||
}); | ||
}); |
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 @@ | ||
{ | ||
"extends": "../configs/tsconfig.json", | ||
"include": [ | ||
"tests", | ||
"src/pages" | ||
], | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"module": "esnext", | ||
"outDir": "lib", | ||
"isolatedModules": true, | ||
"jsx": "preserve" | ||
}, | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
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,2 @@ | ||
module.exports = require('next-redux-wrapper-configs/jest-puppeteer.config'); | ||
module.exports.server.port = 5000; |
Oops, something went wrong.