Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Week7 ssr#16

Closed
Zaggen wants to merge 43 commits intoweek5from
week7-ssr
Closed

Week7 ssr#16
Zaggen wants to merge 43 commits intoweek5from
week7-ssr

Conversation

@Zaggen
Copy link
Copy Markdown
Contributor

@Zaggen Zaggen commented Apr 29, 2019

Replaced create-react-app for next.js

@Zaggen Zaggen requested review from dannytce and koss-lebedev April 29, 2019 17:11
@Zaggen Zaggen changed the base branch from master to week5 April 29, 2019 17:18
Copy link
Copy Markdown
Contributor

@prichodko prichodko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Lu 💪

I've found couple of little things, so just look for the comments. However there are two things outside of the codebase:

  1. Rename public folder to static (and correctly implement handler of * requests), otherwise serving of static files won't work.
  2. Maybe it's out of the scope of your lecture, but it would be pretty cool to show, that you can access cookies on the server and return page already with necessary data for authentication (e.g. no blink).

Comment thread pages/_document.js

class CustomDocument extends Document {
render() {
const sheet = new ServerStyleSheet()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I can't really say, if there is difference or not, but according to official example and styled-components website code for example, styles extraction is defined in getInitialProps method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea if it makes a difference either but I could change it just in case

Comment thread server/index.js Outdated
//server.use(compression())

server.get('/products/:id/:name', (req, res) => {
app.render(req, res, '/product', req.params)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a return statement here, to indicate the end of the request.

Comment thread server/index.js Outdated
server.get('*', (req, res) => {
const parsedUrl = parse(req.url, true)
const { query, pathname } = parsedUrl
app.render(req, res, pathname, query)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the handler of all incoming requests you can simply do

// at the top
const app = next({ dev })
const handle = app.getRequestHandler()

// and then
server.get('*', (req, res) => {
   return handle(req, res)
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also necessary for the correct handling of static file serving.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems I removed that, I'll add it back

Comment thread src/pages/ProductDetail/index.js Outdated
</Layout>
)
}
const ProductView = props => {
Copy link
Copy Markdown
Contributor

@prichodko prichodko Apr 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move destructuring straight into arrow function declaration. 😊

@prichodko
Copy link
Copy Markdown
Contributor

Another quite a big issue I see right now, is that you are not taking into consideration that @koss-lebedev during his lecture will move codebase to TypeScript.

Copy link
Copy Markdown
Contributor

@dannytce dannytce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall great job! :) But I agree with Pavel. It would be cool to make it work somehow with Kosta's PR too :)

Comment thread pages/_app.js Outdated
@@ -0,0 +1,35 @@
// @flow
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove it :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, will do

Comment thread server/index.js
@@ -0,0 +1,37 @@
/* eslint-env node */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this either. It's already set in .eslintrc

Comment thread server/index.js Outdated
/* eslint-env node */
const express = require('express')
const next = require('next')
//const compression = require('compression')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get rid off commented code :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was planning on uncommenting it on live demo, but I could type it directly.

Comment thread server/index.js Outdated
if (err) {
throw err
}
console.log('> Ready on http://localhost:' + PORT) // eslint-disable-line
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's be more strict -> eslint-disable-line no-console? :)

Comment thread src/api/api-client.js
@@ -1,15 +1,41 @@
import fetch from 'isomorphic-fetch'
// import config from '../config'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it's commented there? :)

Comment thread src/pages/ProductDetail/index.js Outdated
const ProductView = props => {
const { product } = props
return (
<>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fragmet not needed here

<Link to={node.id}>
<Link
href={`/product?id=${node.id}`}
as={`${PRODUCT_LIST}/${node.id}/${kebabCase(node.name)}`}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this into routes.js to preserve previous logic?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have to rework that, as href is different than as, I didn't want to overcomplicate that logic, it might be easier to follow like this in my opinion.


ProductList.getInitialProps = async props => {
const products = await getProducts()
props.store.dispatch(loadProducts(products))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are dispatching loadProduct, you don't need to have them in mapDispatchToProps, don't you?

Copy link
Copy Markdown
Contributor Author

@Zaggen Zaggen Apr 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't, good catch :)

Comment thread src/store/index.js Outdated
import cart from './cart/reducer'
import customer from './customer/reducer'

const isBrowser = typeof window !== 'undefined'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is is-browser helper, let's use it here :)

Comment thread src/utils/refresh-token.js Outdated
@@ -0,0 +1,11 @@
export const getRefreshToken = () => {
return window.localStorage.getItem('refreshtoken')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be extended with isBrowser() && too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed

@Zaggen
Copy link
Copy Markdown
Contributor Author

Zaggen commented Apr 30, 2019

@prichodko @dannytce Thanks for the feedback and @prichodko good catch with the static folder I completely forgot to change it, I would mention the sign-in via server but only for the account page as it would hinder caching if done on all routes.

Regarding kostas PR, we previously agreed on having this SSR as a separate branch as it would be overwhelming for the students if combined with typescript, but maybe something could be done

@dannytce
Copy link
Copy Markdown
Contributor

dannytce commented May 3, 2019

Feedback implemented in #18

@dannytce dannytce closed this May 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants