1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

support getInitialProps

This commit is contained in:
nkzawa 2016-10-08 19:16:22 +09:00
parent 5a14a7249b
commit 22bf51bbe0
5 changed files with 15 additions and 8 deletions

View file

@ -1,8 +1,8 @@
import { createElement } from 'react' import { createElement } from 'react'
import { render } from 'react-dom' import { render } from 'react-dom'
import evalScript from './eval-script' import evalScript from './eval-script'
import Router from './router'
import HeadManager from './head-manager' import HeadManager from './head-manager'
import Router from '../lib/router'
import DefaultApp from '../lib/app' import DefaultApp from '../lib/app'
const { const {
@ -12,7 +12,7 @@ const {
const App = app ? evalScript(app).default : DefaultApp const App = app ? evalScript(app).default : DefaultApp
const Component = evalScript(component).default const Component = evalScript(component).default
const router = new Router({ Component }) const router = new Router(location.href, { Component })
const headManager = new HeadManager() const headManager = new HeadManager()
const container = document.getElementById('__next') const container = document.getElementById('__next')
const appProps = { Component, props, router, headManager } const appProps = { Component, props, router, headManager }

View file

@ -3,18 +3,24 @@ import evalScript from './eval-script'
import shallowEquals from './shallow-equals' import shallowEquals from './shallow-equals'
export default class Router { export default class Router {
constructor (initialData) { constructor (url, initialData) {
const parsed = parse(url, true);
// represents the current component key // represents the current component key
this.route = toRoute(location.pathname) this.route = toRoute(parsed.pathname)
// set up the component cache (by route keys) // set up the component cache (by route keys)
this.components = { [this.route]: initialData } this.components = { [this.route]: initialData }
this.pathname = parsed.pathname
this.query = parsed.query
this.subscriptions = new Set() this.subscriptions = new Set()
this.componentLoadCancel = null this.componentLoadCancel = null
this.onPopState = this.onPopState.bind(this) this.onPopState = this.onPopState.bind(this)
window.addEventListener('popstate', this.onPopState) if ('undefined' !== typeof window) {
window.addEventListener('popstate', this.onPopState)
}
} }
onPopState (e) { onPopState (e) {

View file

@ -2,14 +2,15 @@ import { relative, resolve } from 'path'
import { createElement } from 'react' import { createElement } from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server' import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import fs from 'mz/fs' import fs from 'mz/fs'
import Router from '../lib/router'
import Document from '../lib/document' import Document from '../lib/document'
import Head from '../lib/head' import Head from '../lib/head'
import App from '../lib/app' import App from '../lib/app'
import { StyleSheetServer } from '../lib/css' import { StyleSheetServer } from '../lib/css'
export async function render (path, req, res, { dir = process.cwd(), dev = false } = {}) { export async function render (path, req, res, { dir = process.cwd(), dev = false } = {}) {
const mod = require(resolve(dir, '.next', 'pages', path)) || {} const mod = require(resolve(dir, '.next', 'pages', path))
const Component = mod.default const Component = mod.default || mod
let props = {} let props = {}
if (Component.getInitialProps) { if (Component.getInitialProps) {
@ -23,7 +24,7 @@ export async function render (path, req, res, { dir = process.cwd(), dev = false
const app = createElement(App, { const app = createElement(App, {
Component, Component,
props, props,
router: {} router: new Router(req.url)
}) })
return renderToString(app) return renderToString(app)