├── styles └── styles.scss ├── public ├── favicon.ico └── vercel.svg ├── next.config.js ├── next-env.d.ts ├── pages ├── _app.tsx ├── api │ └── hello.ts ├── profile.tsx ├── product.tsx └── index.tsx ├── .gitignore ├── tsconfig.json ├── app └── store │ ├── index.ts │ └── slices │ ├── product.ts │ └── profile.ts ├── package.json ├── README.md └── .eslintrc.json /styles/styles.scss: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmswi/nextjs-starter/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = { 3 | reactStrictMode: true, 4 | } 5 | 6 | module.exports = nextConfig 7 | -------------------------------------------------------------------------------- /next-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | 4 | // NOTE: This file should not be edited 5 | // see https://nextjs.org/docs/basic-features/typescript for more information. 6 | -------------------------------------------------------------------------------- /pages/_app.tsx: -------------------------------------------------------------------------------- 1 | import '../styles/styles.scss'; 2 | import type { AppProps } from 'next/app'; 3 | import { wrapper } from 'app/store'; 4 | 5 | function MyApp({ Component, pageProps }: AppProps) { 6 | return ; 7 | } 8 | 9 | export default wrapper.withRedux(MyApp); -------------------------------------------------------------------------------- /pages/api/hello.ts: -------------------------------------------------------------------------------- 1 | // Next.js API route support: https://nextjs.org/docs/api-routes/introduction 2 | import type { NextApiRequest, NextApiResponse } from 'next'; 3 | 4 | type Data = { 5 | name: string 6 | } 7 | 8 | export default function handler( 9 | req: NextApiRequest, 10 | res: NextApiResponse 11 | ) { 12 | res.status(200).json({ name: 'John Doe' }); 13 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env.local 29 | .env.development.local 30 | .env.test.local 31 | .env.production.local 32 | 33 | # vercel 34 | .vercel 35 | 36 | # typescript 37 | *.tsbuildinfo 38 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "target": "es5", 5 | "lib": [ 6 | "dom", 7 | "dom.iterable", 8 | "esnext" 9 | ], 10 | "allowJs": true, 11 | "skipLibCheck": true, 12 | "strict": true, 13 | "forceConsistentCasingInFileNames": true, 14 | "noEmit": true, 15 | "esModuleInterop": true, 16 | "module": "esnext", 17 | "moduleResolution": "node", 18 | "resolveJsonModule": true, 19 | "isolatedModules": true, 20 | "jsx": "preserve", 21 | "incremental": true 22 | }, 23 | "include": [ 24 | "next-env.d.ts", 25 | "**/*.ts", 26 | "**/*.tsx" 27 | ], 28 | "exclude": [ 29 | "node_modules" 30 | ] 31 | } -------------------------------------------------------------------------------- /app/store/index.ts: -------------------------------------------------------------------------------- 1 | import { configureStore, ThunkAction } from '@reduxjs/toolkit'; 2 | import { createWrapper } from 'next-redux-wrapper'; 3 | import { Action } from 'redux'; 4 | import profileReducer from './slices/profile'; 5 | import productReducer from './slices/product'; 6 | 7 | const makeStore = () => configureStore({ 8 | reducer: { 9 | profile: profileReducer, 10 | product: productReducer 11 | }, 12 | devTools: true 13 | }); 14 | 15 | export type AppStore = ReturnType; 16 | export type AppState = ReturnType; 17 | export type AppThunk = ThunkAction; 18 | 19 | export const wrapper = createWrapper(makeStore); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "next-starter", 3 | "private": true, 4 | "scripts": { 5 | "dev": "next dev", 6 | "build": "next build", 7 | "start": "next start", 8 | "lint": "next lint --fix" 9 | }, 10 | "dependencies": { 11 | "@reduxjs/toolkit": "^1.8.5", 12 | "next": "12.2.5", 13 | "next-redux-wrapper": "^7.0.5", 14 | "react": "^18.2.0", 15 | "react-dom": "^18.2.0", 16 | "react-redux": "^8.0.2", 17 | "sass": "^1.54.5" 18 | }, 19 | "devDependencies": { 20 | "@types/node": "18.7.13", 21 | "@types/react": "18.0.17", 22 | "@types/react-dom": "^18.0.6", 23 | "@typescript-eslint/eslint-plugin": "^5.35.1", 24 | "@typescript-eslint/parser": "^5.35.1", 25 | "eslint": "8.22.0", 26 | "eslint-config-next": "12.2.5", 27 | "typescript": "^4.8.2" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /public/vercel.svg: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /app/store/slices/product.ts: -------------------------------------------------------------------------------- 1 | import { createSlice } from '@reduxjs/toolkit'; 2 | import { HYDRATE } from 'next-redux-wrapper'; 3 | import { AppState, AppThunk } from '..'; 4 | 5 | export const ProductSlice = createSlice({ 6 | name: 'product', 7 | 8 | initialState: { 9 | name: null 10 | }, 11 | 12 | reducers: { 13 | setProductData: (state, action) => { 14 | state.name = action.payload; 15 | } 16 | }, 17 | 18 | extraReducers: { 19 | [HYDRATE]: (state, action) => { 20 | console.log('HYDRATE', action.payload); 21 | 22 | if (!action.payload.product.name) { 23 | return state; 24 | } 25 | 26 | state.name = action.payload.product.name; 27 | } 28 | } 29 | }); 30 | 31 | export const { setProductData } = ProductSlice.actions; 32 | 33 | export const selectProduct = (state: AppState) => state.product; 34 | 35 | export const fetchProduct = 36 | (): AppThunk => 37 | async dispatch => { 38 | const timeoutPromise = (timeout: number) => new Promise(resolve => setTimeout(resolve, timeout)); 39 | 40 | await timeoutPromise(1000); 41 | 42 | dispatch( 43 | setProductData('BA DUM DA THUNK') 44 | ); 45 | }; 46 | 47 | 48 | export default ProductSlice.reducer; -------------------------------------------------------------------------------- /app/store/slices/profile.ts: -------------------------------------------------------------------------------- 1 | import { createSlice } from '@reduxjs/toolkit'; 2 | import { HYDRATE } from 'next-redux-wrapper'; 3 | import { AppState, AppThunk } from '..'; 4 | 5 | export const ProfileSlice = createSlice({ 6 | name: 'profile', 7 | 8 | initialState: { 9 | name: null 10 | }, 11 | 12 | reducers: { 13 | setProfileData: (state, action) => { 14 | state.name = action.payload; 15 | } 16 | }, 17 | 18 | extraReducers: { 19 | [HYDRATE]: (state, action) => { 20 | console.log('HYDRATE', action.payload); 21 | 22 | if (!action.payload.profile.name) { 23 | return state; 24 | } 25 | 26 | state.name = action.payload.profile.name; 27 | } 28 | } 29 | }); 30 | 31 | export const { setProfileData } = ProfileSlice.actions; 32 | 33 | export const selectProfile = (state: AppState) => state.profile; 34 | 35 | export const fetchProfile = 36 | (): AppThunk => 37 | async dispatch => { 38 | const timeoutPromise = (timeout: number) => new Promise(resolve => setTimeout(resolve, timeout)); 39 | 40 | await timeoutPromise(200); 41 | 42 | dispatch( 43 | setProfileData('name from thunk') 44 | ); 45 | }; 46 | 47 | 48 | export default ProfileSlice.reducer; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). 2 | 3 | ## Getting Started 4 | 5 | First, run the development server: 6 | 7 | ```bash 8 | npm run dev 9 | # or 10 | yarn dev 11 | ``` 12 | 13 | Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. 14 | 15 | You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file. 16 | 17 | [API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`. 18 | 19 | The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages. 20 | 21 | ## Learn More 22 | 23 | To learn more about Next.js, take a look at the following resources: 24 | 25 | - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. 26 | - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. 27 | 28 | You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! 29 | 30 | ## Deploy on Vercel 31 | 32 | The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. 33 | 34 | Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. 35 | -------------------------------------------------------------------------------- /pages/profile.tsx: -------------------------------------------------------------------------------- 1 | import type { NextPage } from 'next'; 2 | import Head from 'next/head'; 3 | import Link from 'next/link'; 4 | import { useSelector } from 'react-redux'; 5 | 6 | import { wrapper } from 'app/store'; 7 | import { selectProfile } from 'app/store/slices/profile'; 8 | import { selectProduct } from 'app/store/slices/product'; 9 | 10 | const Profile: NextPage = (props: any) => { 11 | const profile = useSelector(selectProfile) as any; 12 | const product = useSelector(selectProduct) as any; 13 | 14 | return ( 15 |
16 | 17 | Create Next App 18 | 19 | 20 | 21 | 22 |
23 |

24 | Your Featuring Code, {profile?.name} 25 |

26 |

27 | Go to 28 | Home 29 | {' | '} 30 | 31 | Product 32 | 33 |

34 | 35 |

36 | Hello, {props.profileData}! 37 |

38 | 39 | {product?.name ?
Product history: {product.name}
: <>} 40 |
41 |
42 | ); 43 | }; 44 | 45 | export const getServerSideProps = wrapper.getServerSideProps(store => async ({ query }) => { 46 | console.log('store state on the server before dispatch', store.getState()); 47 | const profileData = query.data || 'profile data'; 48 | // http://localhost:3000/profile?data='some-data' 49 | 50 | return { 51 | props: { 52 | profileData 53 | } 54 | }; 55 | }); 56 | 57 | export default Profile; 58 | -------------------------------------------------------------------------------- /pages/product.tsx: -------------------------------------------------------------------------------- 1 | import type { NextPage } from 'next'; 2 | import Head from 'next/head'; 3 | import Link from 'next/link'; 4 | import { connect } from 'react-redux'; 5 | 6 | import { AppState, wrapper } from 'app/store'; 7 | import { fetchProduct } from 'app/store/slices/product'; 8 | 9 | 10 | const Product: NextPage = (props: any) => { 11 | const { product, profile } = props; 12 | 13 | return ( 14 |
15 | 16 | Create Next App 17 | 18 | 19 | 20 | 21 |
22 |

23 | Product name: {product?.name} 24 |

25 |

Profile: {profile?.name}

26 |

27 | Go to 28 | Home page 29 | {' | '} 30 | 31 | Profile 32 | 33 |

34 | 35 |

36 | Product, {props.productData}! 37 |

38 |
39 |
40 | ); 41 | }; 42 | 43 | export const getServerSideProps = wrapper.getServerSideProps(store => async ({ query }) => { 44 | console.log('store state on the server before dispatch', store.getState()); 45 | const productData = query.data || 'page data'; 46 | // http://localhost:3000/product?data='some-data' 47 | await store.dispatch(fetchProduct()); 48 | console.log('store state on the server after dispatch', store.getState()); 49 | 50 | return { 51 | props: { 52 | productData 53 | } 54 | }; 55 | }); 56 | 57 | const mapStateToProps = (state: AppState) => ({ 58 | profile: state.profile, 59 | product: state.product 60 | }); 61 | 62 | export default connect(mapStateToProps)(Product); 63 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "@typescript-eslint/parser", 3 | "extends": [ 4 | "next/core-web-vitals", 5 | "eslint:recommended", 6 | "plugin:@typescript-eslint/recommended" 7 | ], 8 | "rules": { 9 | "comma-dangle": [ 10 | "error", 11 | "never" 12 | ], 13 | "max-len": [ 14 | "error", 15 | 180 16 | ], 17 | "one-var": 0, 18 | "semi": [ 19 | "error", 20 | "always" 21 | ], 22 | "key-spacing": [ 23 | "error", 24 | { 25 | "align": "colon" 26 | } 27 | ], 28 | "eqeqeq": "error", 29 | "no-dupe-keys": "error", 30 | "no-duplicate-case": "error", 31 | "no-unused-vars": "off", 32 | "prefer-const": "error", 33 | "@typescript-eslint/no-unused-vars": "error", 34 | "@typescript-eslint/no-explicit-any": "off", 35 | "block-spacing": "error", 36 | "brace-style": "error", 37 | "comma-spacing": "error", 38 | "comma-style": "error", 39 | "computed-property-spacing": "error", 40 | "indent": [ 41 | "error", 42 | 2, 43 | { 44 | "SwitchCase": 1 45 | } 46 | ], 47 | "keyword-spacing": "error", 48 | "no-multiple-empty-lines": "error", 49 | "no-trailing-spaces": "error", 50 | "quotes": [ 51 | "error", 52 | "single" 53 | ], 54 | "space-infix-ops": "error", 55 | "space-before-blocks": "error", 56 | "object-curly-spacing": [ 57 | "error", 58 | "always" 59 | ], 60 | "import/extensions": [ 61 | "error", 62 | "ignorePackages", 63 | { 64 | "js": "never", 65 | "jsx": "never", 66 | "ts": "never", 67 | "tsx": "never" 68 | } 69 | ] 70 | }, 71 | "settings": { 72 | "import/resolver": { 73 | "node": { 74 | "extensions": [ 75 | ".js", 76 | ".jsx", 77 | ".ts", 78 | ".tsx" 79 | ] 80 | } 81 | } 82 | } 83 | } -------------------------------------------------------------------------------- /pages/index.tsx: -------------------------------------------------------------------------------- 1 | import { wrapper } from 'app/store'; 2 | import { selectProfile, setProfileData } from 'app/store/slices/profile'; 3 | import type { NextPage } from 'next'; 4 | import Head from 'next/head'; 5 | import Image from 'next/image'; 6 | import Link from 'next/link'; 7 | import { useSelector } from 'react-redux'; 8 | import styles from '../styles/components/Home.module.scss'; 9 | 10 | const Home: NextPage = (props: any) => { 11 | const profile = useSelector(selectProfile); 12 | 13 | return ( 14 | 81 | ); 82 | }; 83 | 84 | export const getServerSideProps = wrapper.getServerSideProps(store => async ({ query }) => { 85 | console.log('store state on the server before dispatch', store.getState()); 86 | store.dispatch(setProfileData('mihai')); 87 | console.log('store state on the server after dispatch', store.getState()); 88 | 89 | const data = query.data || 'default data'; 90 | // http://localhost:3000?data='some-data' 91 | 92 | return { 93 | props: { 94 | data 95 | } // will be passed to the page component as props 96 | }; 97 | }); 98 | 99 | export default Home; 100 | --------------------------------------------------------------------------------