diff --git a/LICENSE b/LICENSE.md similarity index 96% rename from LICENSE rename to LICENSE.md index 991620e1..69b61253 100644 --- a/LICENSE +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 Zeit, Inc. developers +Copyright (c) 2016 Zeit, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 2b4be066..b489b792 100644 --- a/README.md +++ b/README.md @@ -190,13 +190,17 @@ import React from 'react' export default class Error extends React.Component { static getInitialProps ({ res, xhr }) { - const statusCode = res ? res.statusCode : xhr.status + const statusCode = res ? res.statusCode : (xhr ? xhr.status : null) return { statusCode } } render () { return ( -

An error { this.props.statusCode } occurred

+

{ + this.props.statusCode + ? `An error ${this.props.statusCode} occurred on server` + : 'An error occurred on client' + ]

) } } diff --git a/client/next-dev.js b/client/next-dev.js index 823107bb..f46e8855 100644 --- a/client/next-dev.js +++ b/client/next-dev.js @@ -1,7 +1,6 @@ import 'react-hot-loader/patch' -import './webpack-dev-client?http://localhost:3030' import * as next from './next' - -module.exports = next +import { requireModule } from '../lib/eval-script' window.next = next +module.exports = requireModule diff --git a/client/next.js b/client/next.js index 6c8429d0..00333dd1 100644 --- a/client/next.js +++ b/client/next.js @@ -4,19 +4,33 @@ import HeadManager from './head-manager' import { rehydrate } from '../lib/css' import Router from '../lib/router' import App from '../lib/app' -import evalScript from '../lib/eval-script' +import evalScript, { requireModule } from '../lib/eval-script' const { - __NEXT_DATA__: { component, props, ids, err } + __NEXT_DATA__: { component, errorComponent, props, ids, err } } = window -const Component = evalScript(component).default +document.addEventListener('DOMContentLoaded', () => { + const Component = evalScript(component).default + const ErrorComponent = evalScript(errorComponent).default -export const router = new Router(window.location.href, { Component, ctx: { err } }) + const router = new Router(window.location.href, { + Component, + ErrorComponent, + ctx: { err } + }) -const headManager = new HeadManager() -const container = document.getElementById('__next') -const appProps = { Component, props, router, headManager } + // This it to support error handling in the dev time with hot code reload. + if (window.next) { + window.next.router = router + } -rehydrate(ids) -render(createElement(App, appProps), container) + const headManager = new HeadManager() + const container = document.getElementById('__next') + const appProps = { Component, props, router, headManager } + + rehydrate(ids) + render(createElement(App, appProps), container) +}) + +module.exports = requireModule diff --git a/client/webpack-dev-client/index.js b/client/webpack-dev-client/index.js deleted file mode 100644 index 94c46564..00000000 --- a/client/webpack-dev-client/index.js +++ /dev/null @@ -1,167 +0,0 @@ -/* global __resourceQuery, next */ - -// Based on 'webpack-dev-server/client' - -import url from 'url' -import stripAnsi from 'strip-ansi' -import socket from './socket' - -function getCurrentScriptSource () { - // `document.currentScript` is the most accurate way to find the current script, - // but is not supported in all browsers. - if (document.currentScript) { - return document.currentScript.getAttribute('src') - } - // Fall back to getting all scripts in the document. - const scriptElements = document.scripts || [] - const currentScript = scriptElements[scriptElements.length - 1] - if (currentScript) { - return currentScript.getAttribute('src') - } - // Fail as there was no script to use. - throw new Error('[WDS] Failed to get current script source') -} - -let urlParts -if (typeof __resourceQuery === 'string' && __resourceQuery) { - // If this bundle is inlined, use the resource query to get the correct url. - urlParts = url.parse(__resourceQuery.substr(1)) -} else { - // Else, get the url from the