This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Renames public folder to static * Add next.js pages * Update scripts and adds required libraries for ssr * Add next.js related libraries * Add utils/is-browser helper * Add utils/kebab-case helper * Add isBrowser checks before calling functions * Add .next to the ignored files * Remove macro import * Add all ssr changes * Remove demo file * Replace components/PrivateRoute with PrivatePage * Change to newer system to get styled components on server * Replace window.location with Router.push * Wrap content in PrivatePage component * Remove unused code * Remove Layout and unused code * Update Readme * Fix yarn.lock * Fixed poor rebase :bida:
- Loading branch information
Showing
57 changed files
with
1,593 additions
and
4,444 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["next/babel", "@zeit/next-typescript/babel"] | ||
} |
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
/coverage | ||
|
||
# production | ||
/build | ||
/.next | ||
|
||
# misc | ||
.DS_Store | ||
|
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,3 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} |
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,17 @@ | ||
# SSR | ||
|
||
- [Presentation & Video](https://drive.google.com/drive/u/1/folders/134MBLY7YBzsv2KLG4lEhm6gMtoZQVINF) | ||
|
||
## Homework (Optional) | ||
|
||
- Convert your codebase into SSR using STRV branch as reference | ||
- Remove `react-create-scripts` | ||
- Add `next` and related libraries (`@zeit/next-css`, `@zeit/next-typescript`, `express`, `isomorphic-fetch`, `next-redux-wrapper`, `@types/next`) | ||
- Adjusts scripts section of your `package.js` to work with next instead of `react-create-scripts`, check the one defined in the project for reference. | ||
- Replace `useApi` hook usage on products and products details with `getInitialProps`, you could either set the data in redux or show it | ||
right away as the hook did. | ||
- If you are feeling like a ninja, implement the loading status in case you decide to go with the redux approach. | ||
|
||
## Aditional info | ||
|
||
- https://github.com/zeit/next.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,4 @@ | ||
const withCSS = require('@zeit/next-css') | ||
const withTypescript = require('@zeit/next-typescript') | ||
|
||
module.exports = withTypescript(withCSS()) |
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 @@ | ||
export { default } from '../src/App' |
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,54 @@ | ||
import React from 'react' | ||
import Document, { Head, Main, NextScript } from 'next/document' | ||
import { ServerStyleSheet } from 'styled-components' | ||
|
||
class CustomDocument extends Document { | ||
static async getInitialProps(ctx) { | ||
const sheet = new ServerStyleSheet() | ||
const originalRenderPage = ctx.renderPage | ||
|
||
try { | ||
ctx.renderPage = () => | ||
originalRenderPage({ | ||
enhanceApp: App => props => sheet.collectStyles(<App {...props} />), | ||
}) | ||
|
||
const initialProps = await Document.getInitialProps(ctx) | ||
return { | ||
...initialProps, | ||
styles: ( | ||
<> | ||
{initialProps.styles} | ||
{sheet.getStyleElement()} | ||
</> | ||
), | ||
} | ||
} finally { | ||
sheet.seal() | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<html lang="en"> | ||
<Head> | ||
<meta charSet="utf-8" /> | ||
<link rel="shortcut icon" href="/static/favicon.ico" /> | ||
<meta | ||
name="viewport" | ||
content="width=device-width, initial-scale=1, shrink-to-fit=no" | ||
/> | ||
<meta name="theme-color" content="#000000" /> | ||
<link rel="manifest" href="/static/manifest.json" /> | ||
</Head> | ||
|
||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</html> | ||
) | ||
} | ||
} | ||
|
||
export default CustomDocument |
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,15 @@ | ||
import React from 'react' | ||
import { NotFound } from '../src/pages/NotFound' | ||
|
||
class Error extends React.Component { | ||
static getInitialProps({ res, err }) { | ||
const statusCode = res ? res.statusCode : err ? err.statusCode : null | ||
return { statusCode } | ||
} | ||
|
||
render() { | ||
return <NotFound /> | ||
} | ||
} | ||
|
||
export default Error |
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,3 @@ | ||
import { Account } from '../src/pages/Account' | ||
|
||
export default Account |
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,3 @@ | ||
import { Cart } from '../src/pages/Cart' | ||
|
||
export default Cart |
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,3 @@ | ||
import { ProductList } from '../src/pages/ProductList' | ||
|
||
export default ProductList |
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,3 @@ | ||
import { LogIn } from '../src/pages/LogIn' | ||
|
||
export default LogIn |
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,3 @@ | ||
import { Logout } from '../src/pages/Logout' | ||
|
||
export default Logout |
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,3 @@ | ||
import { ProductDetail } from '../src/pages/ProductDetail' | ||
|
||
export default ProductDetail |
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,3 @@ | ||
import { ProductList } from '../src/pages/ProductList' | ||
|
||
export default ProductList |
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,3 @@ | ||
import { SignUp } from '../src/pages/SignUp' | ||
|
||
export default SignUp |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
/* eslint-env node */ | ||
const express = require('express') | ||
const next = require('next') | ||
|
||
const PORT = process.env.PORT || 3000 | ||
const dev = process.env.NODE_ENV !== 'production' | ||
const app = next({ dev }) | ||
const handle = app.getRequestHandler() | ||
|
||
app | ||
.prepare() | ||
.then(() => { | ||
const server = express() | ||
|
||
server.get('/products/:id/:name', (req, res) => { | ||
return app.render(req, res, '/product', req.params) | ||
}) | ||
|
||
server.get('*', (req, res) => { | ||
return handle(req, res) | ||
}) | ||
|
||
server.listen(PORT, err => { | ||
if (err) { | ||
throw err | ||
} | ||
console.log('> Ready on http://localhost:' + PORT) // eslint-disable-line no-console | ||
}) | ||
}) | ||
.catch(ex => { | ||
console.error(ex.stack) | ||
process.exit(1) | ||
}) |
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,51 +1,46 @@ | ||
import React from 'react' | ||
import { Switch, Route, Redirect } from 'react-router-dom' | ||
import App, { Container } from 'next/app' | ||
import { Provider } from 'react-redux' | ||
import withRedux from 'next-redux-wrapper' | ||
import { ToastContainer, toast } from 'react-toastify' | ||
|
||
import Layout from './components/Layout' | ||
import GlobalStyles from './globalStyles' | ||
import { ProductList } from './pages/ProductList' | ||
import { ProductDetail } from './pages/ProductDetail' | ||
import { Cart } from './pages/Cart' | ||
import { SignUp } from './pages/SignUp' | ||
import { LogIn } from './pages/LogIn' | ||
import { Logout } from './pages/Logout' | ||
import { Account } from './pages/Account' | ||
import { NotFound } from './pages/NotFound' | ||
import { PrivateRoute } from './components/PrivateRoute' | ||
import { ErrorBoundary } from './components/ErrorBoundary' | ||
import { getCustomer } from './utils/customer' | ||
import { configureStore } from './store' | ||
import * as routes from './routes' | ||
|
||
const defaultStore = configureStore({ | ||
customer: getCustomer(), | ||
}) | ||
class MyApp extends App { | ||
static async getInitialProps({ Component, ctx }) { | ||
let pageProps = {} | ||
if (Component.getInitialProps) { | ||
pageProps = await Component.getInitialProps(ctx) | ||
} | ||
|
||
const App = ({ store }) => ( | ||
<Provider store={store || defaultStore}> | ||
<React.Fragment> | ||
<GlobalStyles /> | ||
<ToastContainer position={toast.POSITION.BOTTOM_RIGHT} /> | ||
<ErrorBoundary> | ||
<Switch> | ||
<Route | ||
path={routes.HOMEPAGE} | ||
exact | ||
render={() => <Redirect to={routes.PRODUCT_LIST} />} | ||
/> | ||
<Route path={routes.PRODUCT_LIST} exact component={ProductList} /> | ||
<Route path={routes.PRODUCT_DETAIL} component={ProductDetail} /> | ||
<Route path={routes.CART} component={Cart} /> | ||
<Route path={routes.SIGN_UP} component={SignUp} /> | ||
<Route path={routes.LOGIN} component={LogIn} /> | ||
<Route path={routes.LOGOUT} component={Logout} /> | ||
<PrivateRoute path={routes.ACCOUNT} component={Account} /> | ||
<Route component={NotFound} /> | ||
</Switch> | ||
</ErrorBoundary> | ||
</React.Fragment> | ||
</Provider> | ||
) | ||
return { pageProps } | ||
} | ||
|
||
export { App } | ||
render() { | ||
const { Component, pageProps } = this.props | ||
return ( | ||
<Container> | ||
<Provider store={this.props.store}> | ||
<React.Fragment> | ||
<GlobalStyles /> | ||
<ToastContainer position={toast.POSITION.BOTTOM_RIGHT} /> | ||
<ErrorBoundary> | ||
<Layout> | ||
<Component {...pageProps} /> | ||
</Layout> | ||
</ErrorBoundary> | ||
</React.Fragment> | ||
</Provider> | ||
</Container> | ||
) | ||
} | ||
} | ||
|
||
export default withRedux(configureStore)(MyApp) |
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
Oops, something went wrong.