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:
parent
5a14a7249b
commit
22bf51bbe0
|
@ -1,8 +1,8 @@
|
|||
import { createElement } from 'react'
|
||||
import { render } from 'react-dom'
|
||||
import evalScript from './eval-script'
|
||||
import Router from './router'
|
||||
import HeadManager from './head-manager'
|
||||
import Router from '../lib/router'
|
||||
import DefaultApp from '../lib/app'
|
||||
|
||||
const {
|
||||
|
@ -12,7 +12,7 @@ const {
|
|||
const App = app ? evalScript(app).default : DefaultApp
|
||||
const Component = evalScript(component).default
|
||||
|
||||
const router = new Router({ Component })
|
||||
const router = new Router(location.href, { Component })
|
||||
const headManager = new HeadManager()
|
||||
const container = document.getElementById('__next')
|
||||
const appProps = { Component, props, router, headManager }
|
||||
|
|
|
@ -3,18 +3,24 @@ import evalScript from './eval-script'
|
|||
import shallowEquals from './shallow-equals'
|
||||
|
||||
export default class Router {
|
||||
constructor (initialData) {
|
||||
constructor (url, initialData) {
|
||||
const parsed = parse(url, true);
|
||||
|
||||
// represents the current component key
|
||||
this.route = toRoute(location.pathname)
|
||||
this.route = toRoute(parsed.pathname)
|
||||
|
||||
// set up the component cache (by route keys)
|
||||
this.components = { [this.route]: initialData }
|
||||
|
||||
this.pathname = parsed.pathname
|
||||
this.query = parsed.query
|
||||
this.subscriptions = new Set()
|
||||
this.componentLoadCancel = null
|
||||
this.onPopState = this.onPopState.bind(this)
|
||||
|
||||
window.addEventListener('popstate', this.onPopState)
|
||||
if ('undefined' !== typeof window) {
|
||||
window.addEventListener('popstate', this.onPopState)
|
||||
}
|
||||
}
|
||||
|
||||
onPopState (e) {
|
|
@ -2,14 +2,15 @@ import { relative, resolve } from 'path'
|
|||
import { createElement } from 'react'
|
||||
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
||||
import fs from 'mz/fs'
|
||||
import Router from '../lib/router'
|
||||
import Document from '../lib/document'
|
||||
import Head from '../lib/head'
|
||||
import App from '../lib/app'
|
||||
import { StyleSheetServer } from '../lib/css'
|
||||
|
||||
export async function render (path, req, res, { dir = process.cwd(), dev = false } = {}) {
|
||||
const mod = require(resolve(dir, '.next', 'pages', path)) || {}
|
||||
const Component = mod.default
|
||||
const mod = require(resolve(dir, '.next', 'pages', path))
|
||||
const Component = mod.default || mod
|
||||
|
||||
let props = {}
|
||||
if (Component.getInitialProps) {
|
||||
|
@ -23,7 +24,7 @@ export async function render (path, req, res, { dir = process.cwd(), dev = false
|
|||
const app = createElement(App, {
|
||||
Component,
|
||||
props,
|
||||
router: {}
|
||||
router: new Router(req.url)
|
||||
})
|
||||
|
||||
return renderToString(app)
|
||||
|
|
Loading…
Reference in a new issue