Skip to content

Commit

Permalink
Merge branch 'canary' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
iliran11 authored Oct 16, 2019
2 parents 2d837f4 + 02e75cf commit 0b457cb
Show file tree
Hide file tree
Showing 32 changed files with 237 additions and 137 deletions.
6 changes: 3 additions & 3 deletions errors/no-document-title.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Set `<title>` in `pages/_app.js` instead:

```js
// pages/_app.js
import App, { Container } from 'next/app'
import App from 'next/app'
import Head from 'next/head'
import React from 'react'

Expand All @@ -29,12 +29,12 @@ export default class MyApp extends App {
const { Component, pageProps } = this.props

return (
<Container>
<>
<Head>
<title>My new cool app</title>
</Head>
<Component {...pageProps} />
</Container>
</>
)
}
}
Expand Down
5 changes: 3 additions & 2 deletions examples/using-preact/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Hello World example
# Preact example

## How to use

Expand Down Expand Up @@ -43,4 +43,5 @@ This example uses [Preact](https://github.com/developit/preact) instead of React

Here's how we did it:

- Use `next.config.js` to customize our webpack config to support [preact-compat](https://github.com/developit/preact-compat)
- Use `next.config.js` to customize our webpack config by aliasing React to `preact/compat`
- Use `server.js` to make our server use Preact by aliasing React to `preact/compat`
14 changes: 12 additions & 2 deletions examples/using-preact/next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
const withPreact = require('@zeit/next-preact')
module.exports = {
webpack: function (config) {
config.resolve.alias = {
...config.resolve.alias,
react: 'preact/compat',
react$: 'preact/compat',
'react-dom': 'preact/compat',
'react-dom$': 'preact/compat'
}

module.exports = withPreact()
return config
}
}
12 changes: 8 additions & 4 deletions examples/using-preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
"start": "NODE_ENV=production node server.js"
},
"dependencies": {
"@zeit/next-preact": "0.1.0",
"module-alias": "2.2.2",
"next": "latest",
"preact": "8.2.7",
"preact-compat": "3.18.0"
"preact": "10.0.0",
"preact-render-to-string": "5.0.7"
},
"license": "ISC"
"license": "ISC",
"devDependencies": {
"react": "16.10.2",
"react-dom": "16.10.2"
}
}
6 changes: 5 additions & 1 deletion examples/using-preact/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const moduleAlias = require('module-alias')

require('@zeit/next-preact/alias')()
moduleAlias.addAliases({
react: 'preact/compat',
'react-dom': 'preact/compat'
})

const { createServer } = require('http')
const { parse } = require('url')
Expand Down
14 changes: 11 additions & 3 deletions examples/with-noscript/components/Noscript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import React from 'react'
import ReactDOMServer from 'react-dom/server'

export default function Noscript (props) {
const staticMarkup = ReactDOMServer.renderToStaticMarkup(props.children)
// We don't want to send 'react-dom/server' to the client
let ReactDOMServer
if (typeof window === 'undefined') {
ReactDOMServer = require('react-dom/server')
}

export default function Noscript ({ children }) {
if (!ReactDOMServer) {
return null
}
const staticMarkup = ReactDOMServer.renderToStaticMarkup(children)
return <noscript dangerouslySetInnerHTML={{ __html: staticMarkup }} />
}
12 changes: 0 additions & 12 deletions examples/with-noscript/next.config.js

This file was deleted.

3 changes: 1 addition & 2 deletions examples/with-noscript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"dependencies": {
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-lazyload": "^2.2.7"
"react-dom": "^16.7.0"
},
"license": "ISC"
}
40 changes: 10 additions & 30 deletions examples/with-noscript/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
import React from 'react'
import LazyLoad from 'react-lazyload'
import Noscript from '../components/Noscript'

const images = [
'/static/img/reactjs.png',
'/static/img/nextjs.png',
'/static/img/vuejs.png',
'/static/img/angular.jpg'
]
export default function IndexPage () {
return (
<>
<h1>noscript</h1>
<p>Disable JavaScript to see it in action:</p>

class Index extends React.Component {
static getInitialProps (context) {
const { isServer } = context
return { isServer }
}
render () {
return (
<div style={{ textAlign: 'center' }}>
{images.map((item, index) => (
<div key={index}>
<LazyLoad height={700} offset={100}>
<img width={700} height={700} src={item} alt={`image_${index}`} />
</LazyLoad>
<Noscript>
<img width={700} height={700} src={item} alt={`image_${index}`} />
</Noscript>
</div>
))}
</div>
)
}
}
<hr />

export default Index
<Noscript>Noscript is enabled!</Noscript>
</>
)
}
Binary file removed examples/with-noscript/public/static/img/angular.jpg
Binary file not shown.
Binary file removed examples/with-noscript/public/static/img/nextjs.png
Binary file not shown.
Binary file removed examples/with-noscript/public/static/img/reactjs.png
Binary file not shown.
Binary file removed examples/with-noscript/public/static/img/vuejs.png
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/with-react-intl/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const getMessages = locale => {
app.prepare().then(() => {
createServer((req, res) => {
const accept = accepts(req)
const locale = accept.language(accept.languages(supportedLanguages)) || 'en'
const locale = accept.language(supportedLanguages) || 'en'
req.locale = locale
req.localeDataScript = getLocaleDataScript(locale)
req.messages = dev ? {} : getMessages(locale)
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"registry": "https://registry.npmjs.org/"
}
},
"version": "9.1.2-canary.2"
"version": "9.1.2-canary.3"
}
2 changes: 1 addition & 1 deletion packages/create-next-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-next-app",
"version": "9.1.2-canary.2",
"version": "9.1.2-canary.3",
"keywords": [
"react",
"next",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-bundle-analyzer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/bundle-analyzer",
"version": "9.1.2-canary.2",
"version": "9.1.2-canary.3",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-mdx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/mdx",
"version": "9.1.2-canary.2",
"version": "9.1.2-canary.3",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export default async function getBaseWebpackConfig(
// TODO(atcastle): Analyze if other cache groups should be set to 'all' as well
chunks: 'all',
name: 'framework',
// This regex ignores nested copies of framework libraries so they're
// This regex ignores nested copies of framework libraries so they're
// bundled with their issuer.
// https://github.com/zeit/next.js/pull/9012
test: /(?<!node_modules.*)[\\/]node_modules[\\/](react|react-dom|scheduler|prop-types)[\\/]/,
Expand Down
36 changes: 35 additions & 1 deletion packages/next/build/webpack/loaders/next-serverless-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const nextServerlessLoader: loader.Loader = function() {
} else {
return `
import {parse} from 'url'
import {parse as parseQs} from 'querystring'
import {renderToHTML} from 'next/dist/next-server/server/render';
import {sendHTML} from 'next/dist/next-server/server/send-html';
${
Expand Down Expand Up @@ -131,7 +132,40 @@ const nextServerlessLoader: loader.Loader = function() {
? `const params = fromExport && !unstable_getStaticProps ? {} : getRouteMatcher(getRouteRegex("${page}"))(parsedUrl.pathname) || {};`
: `const params = {};`
}
const result = await renderToHTML(req, res, "${page}", Object.assign({}, unstable_getStaticProps ? {} : parsedUrl.query, params, sprData ? { _nextSprData: '1' } : {}), renderOpts)
${
// Temporary work around -- `x-now-route-params` is a platform header
// _only_ set for `Prerender` requests. We should move this logic
// into our builder to ensure we're decoupled. However, this entails
// removing reliance on `req.url` and using `req.query` instead
// (which is needed for "custom routes" anyway).
isDynamicRoute(page)
? `const nowParams = req.headers && req.headers["x-now-route-params"]
? getRouteMatcher(
(function() {
const { re, groups } = getRouteRegex("${page}");
return {
re: {
// Simulate a RegExp match from the \`req.url\` input
exec: str => {
const obj = parseQs(str);
return Object.keys(obj).reduce(
(prev, key) =>
Object.assign(prev, {
[key]: encodeURIComponent(obj[key])
}),
{}
);
}
},
groups
};
})()
)(req.headers["x-now-route-params"])
: null;
`
: `const nowParams = null;`
}
const result = await renderToHTML(req, res, "${page}", Object.assign({}, unstable_getStaticProps ? {} : parsedUrl.query, nowParams ? nowParams : params, sprData ? { _nextSprData: '1' } : {}), renderOpts)
if (fromExport) return { html: result, renderOpts }
return result
Expand Down
2 changes: 1 addition & 1 deletion packages/next/client/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type SingletonRouterBase = {
ready(cb: () => any): void
}

export { Router, NextRouter }
export { Router, RouterContext, NextRouter }

export type SingletonRouter = SingletonRouterBase & NextRouter

Expand Down
16 changes: 4 additions & 12 deletions packages/next/client/with-router.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'
import PropTypes from 'prop-types'
import { NextComponentType, NextPageContext } from '../next-server/lib/utils'
import { NextRouter } from './router'
import { NextRouter, RouterContext } from './router'

export type WithRouterProps = {
router: NextRouter
Expand All @@ -21,19 +20,12 @@ export default function withRouter<
class WithRouteWrapper extends React.Component<ExcludeRouterProps<P>> {
static displayName?: string
static getInitialProps?: any
static contextTypes = {
router: PropTypes.object,
}
static contextType = RouterContext

context!: WithRouterProps
context!: React.ContextType<typeof RouterContext>

render() {
return (
<ComposedComponent
router={this.context.router}
{...this.props as any}
/>
)
return <ComposedComponent router={this.context} {...this.props as any} />
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next/next-server/server/spr-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const calculateRevalidate = (pathname: string): number | false => {
// in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging
const curTime = new Date().getTime()
if (!sprOptions.dev) return curTime
if (sprOptions.dev) return curTime

const { initialRevalidateSeconds } = prerenderManifest.routes[pathname] || {
initialRevalidateSeconds: 1,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next",
"version": "9.1.2-canary.2",
"version": "9.1.2-canary.3",
"description": "The React Framework",
"main": "./dist/server/next.js",
"license": "MIT",
Expand Down
12 changes: 1 addition & 11 deletions packages/next/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, { ErrorInfo } from 'react'
import PropTypes from 'prop-types'
import {
execOnce,
loadGetInitialProps,
AppContextType,
AppInitialProps,
AppPropsType,
} from '../next-server/lib/utils'
import { Router, makePublicRouterInstance } from '../client/router'
import { Router } from '../client/router'

export { AppInitialProps }

Expand All @@ -31,18 +30,9 @@ export default class App<P = {}, CP = {}, S = {}> extends React.Component<
P & AppProps<CP>,
S
> {
static childContextTypes = {
router: PropTypes.object,
}
static origGetInitialProps = appGetInitialProps
static getInitialProps = appGetInitialProps

getChildContext() {
return {
router: makePublicRouterInstance(this.props.router),
}
}

// Kept here for backwards compatibility.
// When someone ended App they could call `super.componentDidCatch`.
// @deprecated This method is no longer needed. Errors are caught at the top level
Expand Down
Loading

0 comments on commit 0b457cb

Please sign in to comment.