Commit 87610ccf authored by Luis Fernando Alvarez D's avatar Luis Fernando Alvarez D Committed by JJ Kasper

Update with-zones example and Multi Zones documentation (#7513)

* Updated with-zones example to Now V2

* Updated Zones docs

* Update packages/next/README.md
Co-Authored-By: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 0ca80875
......@@ -34,69 +34,46 @@ yarn
With Next.js you can use multiple apps as a single app using it's multi-zones feature.
This is an example showing how to use it.
In this example, we've two apps: 'home' and 'blog'.
We also have a set of rules defined in `rules.json` for the proxy.
Now let's start two of our app using:
```bash
npm run home
npm run blog
# or
yarn home
yarn blog
```
Then start the proxy:
In this example, we've two apps: 'home' and 'blog'. We'll start both apps with [Now](https://zeit.co/now):
We also have a set of builders and routes defined in `now.json`.
```bash
npm run proxy
# or
yarn proxy
now dev
```
Now you can visit http://localhost:9000 and access and develop both apps a single app.
Now you can visit http://localhost:3000 and develop for both apps as a single app.
### Proxy Rules
### Now config
This is the place we define rules for our proxy. Here are the rules(in `rules.json`) available for this app:
`now.json` allows us to create a single dev server for any builders and routes we add to it, with `now dev` we can easily create a dev server for multiple apps without having to deploy or setup anything else:
```json
{
"rules": [
{
"pathname": "/blog",
"dest": "http://localhost:5000"
},
{ "pathname": "/**", "dest": "http://localhost:4000" }
"name": "with-zones",
"version": 2,
"builds": [
{ "src": "blog/next.config.js", "use": "@now/next" },
{ "src": "home/next.config.js", "use": "@now/next" }
],
"routes": [
{ "src": "/blog/_next(.*)", "dest": "blog/_next$1" },
{ "src": "/blog", "dest": "blog/blog" },
{ "src": "(.*)", "dest": "home$1" }
]
}
```
These rules are based on ZEIT now v1 [path alias](https://zeit.co/docs/features/path-aliases) rules and use [`micro-proxy`](https://github.com/zeit/micro-proxy) as the proxy.
The previous file is based in the [@now/next](https://zeit.co/docs/v2/deployments/official-builders/next-js-now-next/) builder and [Now Routes](https://zeit.co/docs/v2/deployments/routes/) from Now V2.
## Special Notes
- All pages should be unique across zones. A page with the same name should not exist in multiple zones. Otherwise, there'll be unexpected behaviour in client side navigation.
- All pages should be unique across zones. A page with the same name should not exist in multiple zones. Otherwise, there'll be unexpected behaviours in client side navigation.
- According to the above example, a page named `blog` should not be exist in the `home` zone.
## Production Deployment
Here's how are going to deploy this application into production.
- Open the `now.json` and `next.config.js` files in both `blog` and `home` directories and change the aliases as you wish.
- Then update `routes` in `home/now.json` accordingly.
- Now deploy both apps:
We only need to run `now`, the same `now.json` used for development will be used for the deployment:
```bash
cd home
now && now alias
cd ../blog
now && now alias
cd ..
now
```
> You can use a domain name of your choice in the above command instead of `with-zones.nextjs.org`.
That's it.
Now you can access the final app via: <https://with-zones.nextjs.org>
const { DEPLOY } = process.env
const alias = 'with-zones-blog.nextjs.org'
module.exports = {
target: 'serverless',
assetPrefix: DEPLOY ? `https://${alias}` : 'http://localhost:5000'
assetPrefix: '/blog'
}
{
"version": 2,
"name": "with-zones-blog",
"alias": "with-zones-blog.nextjs.org",
"build": {
"env": {
"DEPLOY": "true"
}
},
"builds": [{ "src": "package.json", "use": "@now/next" }]
}
......@@ -7,8 +7,8 @@
},
"dependencies": {
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0"
"react": "16.8.6",
"react-dom": "16.8.6"
},
"license": "ISC"
}
const { DEPLOY } = process.env
const alias = 'with-zones.nextjs.org'
module.exports = {
target: 'serverless',
assetPrefix: DEPLOY ? `https://${alias}` : 'http://localhost:4000'
target: 'serverless'
}
{
"version": 2,
"name": "with-zones-home",
"alias": "with-zones.nextjs.org",
"build": {
"env": {
"DEPLOY": "true"
}
},
"builds": [{ "src": "package.json", "use": "@now/next" }],
"routes": [{ "src": "/blog", "dest": "https://with-zones-blog.nextjs.org" }]
}
......@@ -7,8 +7,8 @@
},
"dependencies": {
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0"
"react": "16.8.6",
"react-dom": "16.8.6"
},
"license": "ISC"
}
{
"name": "with-zones",
"version": 2,
"builds": [
{ "src": "blog/next.config.js", "use": "@now/next" },
{ "src": "home/next.config.js", "use": "@now/next" }
],
"routes": [
{ "src": "/blog/_next(.*)", "dest": "blog/_next$1" },
{ "src": "/blog", "dest": "blog/blog" },
{ "src": "(.*)", "dest": "home$1" }
]
}
{
"name": "with-zones",
"version": "1.0.0",
"scripts": {
"home": "cd home && next dev -p 4000",
"home-build": "next build home",
"home-start": "next start home -p 4000",
"blog": "cd blog && next dev -p 5000",
"blog-build": "next build blog",
"blog-start": "next start blog -p 5000",
"proxy": "micro-proxy -r rules-dev.json"
},
"dependencies": {
"micro-proxy": "^1.0.0",
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0"
},
"license": "ISC"
}
{
"rules": [
{ "pathname": "/blog", "dest": "http://localhost:5000" },
{ "pathname": "/**", "dest": "http://localhost:4000" }
]
}
......@@ -2194,44 +2194,46 @@ The `req` and `res` fields of the `context` object passed to `getInitialProps` a
</ul>
</details>
A zone is a single deployment of a Next.js app. Just like that, you can have multiple zones. Then you can merge them as a single app.
A zone is a single deployment of a Next.js app. Just like that, you can have multiple zones and then you can merge them as a single app.
For an example, you can have two zones like this:
- https://docs.my-app.com for serving `/docs/**`
- https://ui.my-app.com for serving all other pages
- An app for serving `/blog/**`
- Another app for serving all other pages
With multi zones support, you can merge both these apps into a single one. Which allows your customers to browse it using a single URL. But you can develop and deploy both apps independently.
With multi zones support, you can merge both these apps into a single one allowing your customers to browse it using a single URL, but you can develop and deploy both apps independently.
> This is exactly the same concept as microservices, but for frontend apps.
> This is exactly the same concept of microservices, but for frontend apps.
### How to define a zone
There are no special zones related APIs. You only need to do following things:
There are no special zones related APIs. You only need to do following:
- Make sure to keep only the pages you need in your app. (For an example, https://ui.my-app.com should not contain pages for `/docs/**`)
- Make sure your app has an [assetPrefix](https://github.com/zeit/next.js#cdn-support-with-asset-prefix). (You can also define the assetPrefix [dynamically](https://github.com/zeit/next.js#dynamic-assetprefix).)
- Make sure to keep only the pages you need in your app, meaning that an app can't have pages from another app, if app `A` has `/blog` then app `B` shouldn't have it too.
- Make sure to add an [assetPrefix](https://github.com/zeit/next.js#cdn-support-with-asset-prefix) to avoid conflicts with static files.
### How to merge them
You can merge zones using any HTTP proxy.
You can use [micro proxy](https://github.com/zeit/micro-proxy) as your local proxy server. It allows you to easily define routing rules like below:
You can use [now dev](https://zeit.co/docs/v2/development/basics) as your local development server. It allows you to easily define routing routes for multiple apps like below:
```json
{
"rules": [
{
"pathname": "/docs**",
"method": ["GET", "POST", "OPTIONS"],
"dest": "https://docs.my-app.com"
},
{ "pathname": "/**", "dest": "https://ui.my-app.com" }
"version": 2,
"builds": [
{ "src": "docs/next.config.js", "use": "@now/next" },
{ "src": "home/next.config.js", "use": "@now/next" }
],
"routes": [
{ "src": "/docs/_next(.*)", "dest": "docs/_next$1" },
{ "src": "/docs(.*)", "dest": "docs/docs$1" },
{ "src": "(.*)", "dest": "home$1" }
]
}
```
For the production deployment, you can use the [path alias](https://zeit.co/docs/features/path-aliases) feature if you are using [ZEIT now](https://zeit.co/now). Otherwise, you can configure your existing proxy server to route HTML pages using a set of rules as shown above.
For the production deployment, you can use the same configuration and run `now` to do the deployment with [ZEIT Now](https://zeit.co/now). Otherwise you can also configure a proxy server to route using a set of routes like the ones above, e.g deploy the docs app to `https://docs.example.com` and the home app to `https://home.example.com` and then add a proxy server for both apps in `https://example.com`.
## Recipes
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment