original code in `/lib/router/router.js`
```
urlIsNew (pathname, query) {
return this.pathname !== pathname || !shallowEquals(query, this.query)
}
```
the urlIsNew compare `this.pathname` to an argument `pathname`
the invokers:
```
// If asked to change the current URL we should reload the current page
// (not location.reload() but reload getInitialProps and other Next.js stuffs)
// We also need to set the method = replaceState always
// as this should not go into the history (That's how browsers work)
if (!this.urlIsNew(asPathname, asQuery)) {
method = 'replaceState'
}
```
the parameter here is `asPathname` destructured from `asPath`
so here is a problem when we reuse a single page rendered in two asPaths
pages/a.js
```
<>
<Link href='/a'><a>goto a</a></Link>
<Link href='/a' as='/b'><a>goto b</a></Link>
</>
```
If we navigate to page /a, then click 'goto b', actually the history is replaced, not pushed.
It is expected that history could be correctly pushed and popped as long as the browser url is changed.
It’s an inconsistent result, users should use ctx instead. At a later time we’ll normalize the properties passed into _app.js its getInitialprops to be consistent with pages.
This PR fixes the buggy `Head.propTypes` here:
https://github.com/zeit/next.js/blob/v8.0.0-canary.3/packages/next-server/lib/head.js#L107
Currently, `Head.propTypes` allows one child node like this:
```jsx
import Head from 'next/head'
// …
<Head>
<title>Title</title>
</Head>
```
But more than one child node mistakenly causes a prop type error like this:
```jsx
<Head>
<title>Title</title>
<meta name="description" content="Description." />
</Head>
```
```
Warning: Failed prop type: Invalid prop `children` supplied to `Head`.
```
It looks like :
```
Pages sizes after gzip:
┌ / (196 B)
├ /_app (11.5 kB)
├ /_error (4.44 kB)
├ /blog (196 B)
└ /blog/page (195 B)
```
(style inspired from now-cli : https://github.com/zeit/now-cli/blob/canary/src/util/output/builds.js)
I'll add dynamic chunks in a separate PR.
@timneutkens Do you want to keep `_app` and `_error` or filter them out ? I think it's a good idea to keep them, because `_app` can get pretty large and it would encourage code splitting in that case.
I wrote a [script](https://github.com/j0lv3r4/dependency-version-updater) to update dependencies recursively in `package.json` files, e.g.:
```
$ node index.js --path="./examples" --dependencies="react=^16.7.0,react-dom=^16.7.0"
```
This PR contains the result against the examples folder.
I changed the version to the following files:
- [x] - examples/with-next-css/package.json
- [x] - examples/with-draft-js/package.json
- [x] - examples/custom-server-polka/package.json
- [x] - examples/with-cerebral/package.json
- [x] - examples/with-zones/package.json
- [x] - examples/with-universal-configuration-runtime/package.json
- [x] - examples/with-apollo/package.json
- [x] - examples/with-higher-order-component/package.json
- [x] - examples/with-hashed-statics/package.json
- [x] - examples/with-pkg/package.json
- [x] - examples/with-jest/package.json
- [x] - examples/with-glamorous/package.json
- [x] - examples/with-custom-reverse-proxy/package.json
- [ ] - examples/with-emotion/package.json
- [x] - examples/with-styled-jsx-scss/package.json
- [x] - examples/with-styled-jsx-plugins/package.json
`with-emotion/package.json` already has the latest, so I guess it's other packabe. BUT I think we need to update this example with the latest version of `emotion` since it changed a little bit (for better).
This PR aims at replacing next-server/lib/event-emitter.js by mitt.
Fix https://github.com/zeit/next.js/issues/4908
event-emitter.js is ~400 bytes gzipped vs mitt is 200 bytes
Extends on #5927, instead of `.default` we'll expose `.render` which is semantically more correct / mirrors the naming of the custom server API.
I've updated the spec in #5927 to reflect this change.
(copied from #5927):
```js
const http = require('http')
const page = require('./.next/serverless/about.js')
const server = new http.Server((req, res) => page.render(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
The current reasonml needs an example of how to do getInitialProps. tmepple posted the only known way of doing it in [this comment](https://github.com/zeit/next.js/issues/4202#issuecomment-439175214). It is unlikely reasonml users are going to discover that comment or figure it out on their own so having it in an example is critical.
Also, dependencies updated. After updating dependencies, I get this error:
```
ModuleBuildError: Module build failed (from ./node_modules/next/dist/build/webpack/loaders/next-babel-loader.js):
[1] Error: Cannot find module '@babel/core'
[1] babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.
```
Seems like @babel/core is required as peer dependency per [this comment](https://github.com/babel/gulp-babel/issues/124#issuecomment-326853222) so I added it.
The way to do getInitialProps has changed in 7.0.2 canary so this also has a comment in the code about what change is required to get that working.
Saw a reply on the original pull request that the WebSocket using a random port broke their set up so I added a `--websocket` or `-w` argument similar to the `-p` argument to allow manually setting this port also.
Fixes#5845
Implement tslint for core files
**What is this?**
Implements tslint for both next and next-server, but keeps standardjs/eslint for the .js files that are still there, we're gradually migrating to Typescript.
**How does it work?**
Before every commit (pre-commit) we execute the following `tslint` command:
`tslint -c tslint.json 'packages/**/*.ts`
**TSLint Rules**
In order to avoid as much changes as possible I marked some rules as false. This way we can improve the linter but making sure this step will not break things. (see tslint.json)
**Note**
After merging this PR, you'll need to update your dependencies since it adds tslint to package.json
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
Explains in details how the "with-universal-configuration" example works and rename it to "with-universal-configuration-build-time". Changing the example name makes the purpose of the example clear.
The "env-config.js" file introduce one more sample of variable usage that instantiates an immediate value of the local environment variable. This makes it even clearer how build-time variable configuration works. The "index.js" page makes explicit the use of these configured environment variables.
The universal configuration confusion happens when the value of the environment variable is used directly in the application causing an effect in server-side but not on the client side.