Git Product home page Git Product logo

isomorphic-lab's Introduction

Isomorphic Lab

logo

This repo is being used to experiment with writing isomorphic JavaScript apps with React 0.13.X, React Router 1.0.0-betaX and express.

It uses newforms for form display and validation on both ends - this could be done manually with any component which can populate its form fields and display error messages based on its props and state, though.

Functionality

Data fetching

React Router's experimental AsyncProps is used to fetch data, with route components defining a static loadProps(params, callback(err, data)) function, which will be called when their route is matched against the current URL.

Title generation (source)

Route components can define a static title attribute or a synchronous getTitle(props, params) function, which will be called when the route is matched against the current URL.

Any async props which will be passed to the component when it's rendered will be passed as the first first argument to getTitle().

Each handler should only define or return its own portion of the URL. e.g. the top-level component might define the site name as a static title, while a nested blog post component might define a static getTitle(props) which returns the title of the post.

By default, these title parts will be reversed and joined with a mid-dot to form the final title to be included in a <title> element or set on document.title depending on the environment - this is configurable.

Client rehydration

The initial render will make use of AsyncProps.rehydrate() with initial async props provided via a window.__PROPS__ variable when running on the client.

Express middleware (source)

Usage:

var reactRouter = require('./react-router-middleware')

app.use(reactRouter(routes[, options]))

options.title

An object to be passed as options to getTitle() - available options are:

reverse (true) - should title parts be reversed into most-specific-first order before joining?

join (' · ') - string to be used to join title parts.

defaultTitle ('(undefined)') - default title to use if none of the matched route components provide title parts.

Response handling

In addition to performing the common functionality described above, the express middleware handles the following scenarios:

Successful rendering

For a regular, successful Handler render, a 'react' view will be rendered with the following locals:

title - generated document title

html - rendered component output

props - props which were used to render the component, serialised to a JSON string.

Non-GET requests

Non-GET request bodies will be set on the Router's location state as a body property.

The request method will also be available as a method property.

Redirecting

If transition.to() is used in an onEnter() hook, this will be turned into an HTTP 303 Redirect response...

Re-rendering

...unless the redirect location has been given some state. In this case, the middleware will create a new redirect location and run routing on it.

This can be used to render another route back to the user as a response, e.g. rendering a form with user input and validation errors in response to a POST request which contained an invalid body.

Errors

If an error is detected at any time, the middleware will call its next() callback with the error to allow whatever error handling middleware is configured elsewhere to pick it up.

MIT Licensed

isomorphic-lab's People

Contributors

insin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

isomorphic-lab's Issues

404 when resource on server not found

Hi, I got this fetchData method in my component:

fetchData(params, cb) {
            superagent
                .get(`${API_URL}/users/` + params.username)
                .accept('json')
                .set('Api', 'v1')
                .end(function(err, res) {
                        cb(err, res && res.body);
                })
        },

How can I display a 404 message when the superagent returns a 404 from request?

Thanks.

How to run locally?

Hey,

I'm trying to run your project (which looks awesome!) locally but neither npm install nor npm run bundle works for me?

Thank you!

EPEERINVALID

on npm install

npm ERR! node v4.1.1
npm ERR! npm  v2.14.4
npm ERR! code EPEERINVALID

npm ERR! peerinvalid The package [email protected] does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer [email protected] wants react@>=0.13.0
npm ERR! peerinvalid Peer [email protected] wants react@^15.0.0-rc.2

AsyncProps error: Cannot read property 'componentsArray' of undefined

Hey, thanks for your ongoing work in this repo! It's been a huge help to me so far.

I'm using the latest version with AsyncProps to scaffold a new app, and am running into the same issue described here: remix-run/react-router#1511

Did you run across anything similar when setting this up?

My first thought was that it might have something to do with the way my Routes load their components:

<Route component={Main}>
    <Route path="/" component={Projects}/>
  </Route>

loadProps is called on Main and returns a value, which is then not accessible in Projects. Maybe I'm not passing it down correctly?

Would love to hear if this looks familiar. Thanks!

npm start

you have in pkg.json
script -> star like "start": "node lib/server"
but lib/server not exists

Error on adding a Thing

Hi, this is great stuff.

I get a strange error when trying to add a thing:

Error cleaning Form.name:connect ECONNREFUSED
Error: connect ECONNREFUSED
    at errnoException (net.js:904:11)
    at Object.afterConnect [as oncomplete] (net.js:895:19)
POST /api/things 500 3.103 ms - -

Any ideas?

Use in production?

I like your implementation for it's simplicity. I was evaluating use of a Flux style architecture, and just kept feeling like I was adding so much boilerplate, and many implementations did not support an isomorphic implementation.

The app I am building is primarily a content browsing site, that will fetch data from a Wordpress backed API. We want it to have SPA feel, animation, and functionality.

With all that said, have you deployed your implementation to production? As the app grows have you found any issues that I should be aware of? Has this project moved beyond just experimentation and could I feel comfortable using it for a customer facing deployment?

React 0.14 breaks the app

Current package.json configuration triggers latest packages installation, which breaks the app on npm install stage already.

npm ERR! peerinvalid The package [email protected] does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer [email protected] wants react@>=0.13.0

form reverts to browser default validation upon submit

I forced a validation error by adding a price greater than 100. I saw the custom validation error. Cool. Then I submitted the form with the price still greater than 100. I also got a validation error. Cool. What's not cool is that the validation error from the submission was a browser default validation error instead of a custom error.
isomorphic-form-error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.