Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
Cleanup middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
swernerx committed Feb 17, 2017
1 parent e6bb9a0 commit 4ebf2ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 91 deletions.
15 changes: 3 additions & 12 deletions src/app/views/About.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react"
import Helmet from "react-helmet"
import { connect } from "react-redux"
import markdown from "markdown-in-js"
import { IntlProvider, FormattedDate, FormattedMessage, FormattedRelative } from "react-intl"
import { FormattedDate, FormattedMessage, FormattedRelative } from "react-intl"
import { addDays } from "date-fns"

import Styles from "./About.css"
Expand All @@ -16,28 +16,19 @@ function handleOldMethodCall() {
// nothing to do
}

const localMessages = {

}


class About extends React.Component {
componentDidMount() {
// Good strategy to load relevant data: wait after rendering
// so that the user sees the empty state. We can't really wait
// in render sequence for any data coming in.
if (this.props.value == null) {
this.props.load()
}
return this.props.value == null ? this.props.load() : null
}

fetchData()
{
// Load data on all preparation steps e.g. on SSR and also on rehydration on client
// when intially loading this route.
if (this.props.value == null) {
return this.props.load()
}
return this.props.value == null ? this.props.load() : null
}

renderMarkdown() {
Expand Down
123 changes: 44 additions & 79 deletions src/server/createUniversalMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { renderToString } from "react-dom/server"
import { StaticRouter } from "react-router"
import Helmet from "react-helmet"
import { ApolloProvider, getDataFromTree } from "react-apollo"
// import { withAsyncComponents } from "react-async-component"

import Measure from "./Measure"
import renderPage from "./renderPage"
Expand Down Expand Up @@ -74,94 +73,60 @@ function renderFull({ request, response, nonce, Root, apolloClient, reduxStore,
</StaticRouter>
)

/*measure.start("wrap-async")
withAsyncComponents(WrappedRoot).then((wrappedResult) =>
{
measure.stop("wrap-async")
const {
// The result includes a decorated version of your app
// that will have the async components initialised for
// the renderToString call.
appWithAsyncComponents,
// This state object represents the async components that
// were rendered by the server. We will need to send
// this back to the client, attaching it to the window
// object so that the client can rehydrate the application
// to the expected state and avoid React checksum issues.
state,
// This is the identifier you should use when attaching
// the state to the "window" object.
STATE_IDENTIFIER
} = wrappedResult
*/

var appWithAsyncComponents = WrappedRoot

// Create the application react element.
renderToStringWithData(
appWithAsyncComponents,
measure
).then((renderedApp) => {
const reduxState = reduxStore.getState()

// Render the app to a string.
measure.start("render-page")
const html = renderPage({
// Provide the full rendered App react element.
renderedApp,

// Provide the redux store state, this will be bound to the window.APP_STATE
// so that we can rehydrate the state on the client.
initialState: reduxState,

// codeSplitState: state,
//STATE_IDENTIFIER,

// Nonce which allows us to safely declare inline scripts.
nonce,
// Create the application react element.
renderToStringWithData(
WrappedRoot,
measure
).then((renderedApp) => {
const reduxState = reduxStore.getState()

// Running this gets all the helmet properties (e.g. headers/scripts/title etc)
// that need to be included within our html. It's based on the rendered app.
// @see https://github.com/nfl/react-helmet
helmet: Helmet.rewind(),
// Render the app to a string.
measure.start("render-page")
const html = renderPage({
// Provide the full rendered App react element.
renderedApp,

// Send detected language and region for HTML tag info
language,
region
})
measure.stop("render-page")
// Provide the redux store state, this will be bound to the window.APP_STATE
// so that we can rehydrate the state on the client.
initialState: reduxState,

// Nonce which allows us to safely declare inline scripts.
nonce,

console.log("Server: Routing Context:", routingContext)
console.log("Server: Sending Page...")
// Running this gets all the helmet properties (e.g. headers/scripts/title etc)
// that need to be included within our html. It's based on the rendered app.
// @see https://github.com/nfl/react-helmet
helmet: Helmet.rewind(),

/* eslint-disable no-magic-numbers */
// Send detected language and region for HTML tag info
language,
region
})
measure.stop("render-page")

// Check if the render result contains a redirect, if so we need to set
// the specific status and redirect header and end the response.
if (routingContext.url) {
response.status(302).setHeader("Location", routingContext.url)
response.end()
return
}
console.log("Server: Routing Context:", routingContext)
console.log("Server: Sending Page...")

// If the renderedResult contains a "missed" match then we set a 404 code.
// Our App component will handle the rendering of an Error404 view.
// Otherwise everything is all good and we send a 200 OK status.
response.status(routingContext.missed ? 404 : 200).send(html)
/* eslint-disable no-magic-numbers */

// Print measure results
// measure.print()
}).catch((error) => {
console.error("Server: Error during producing response:", error)
})
// Check if the render result contains a redirect, if so we need to set
// the specific status and redirect header and end the response.
if (routingContext.url) {
response.status(302).setHeader("Location", routingContext.url)
response.end()
return
}

/*
// If the renderedResult contains a "missed" match then we set a 404 code.
// Our App component will handle the rendering of an Error404 view.
// Otherwise everything is all good and we send a 200 OK status.
response.status(routingContext.missed ? 404 : 200).send(html)

// Print measure results
// measure.print()
}).catch((error) => {
console.error("Server: Error wrapping application for code splitting:", error)
console.error("Server: Error during producing response:", error)
})
*/
}

/**
Expand Down

0 comments on commit 4ebf2ac

Please sign in to comment.