diff --git a/client/on-demand-entries-client.js b/client/on-demand-entries-client.js index 56b1f91f9ea5d..d8c26e84b81af 100644 --- a/client/on-demand-entries-client.js +++ b/client/on-demand-entries-client.js @@ -3,6 +3,12 @@ import Router from '../lib/router' import fetch from 'unfetch' +const { + __NEXT_DATA__: { + assetPrefix + } +} = window + export default () => { Router.ready(() => { Router.router.events.on('routeChangeComplete', ping) @@ -10,7 +16,7 @@ export default () => { async function ping () { try { - const url = `/_next/on-demand-entries-ping?page=${Router.pathname}` + const url = `${assetPrefix}/_next/on-demand-entries-ping?page=${Router.pathname}` const res = await fetch(url, { credentials: 'same-origin' }) diff --git a/client/webpack-hot-middleware-client.js b/client/webpack-hot-middleware-client.js index 10dbf28dc191f..057202c23583b 100644 --- a/client/webpack-hot-middleware-client.js +++ b/client/webpack-hot-middleware-client.js @@ -1,4 +1,4 @@ -import webpackHotMiddlewareClient from 'webpack-hot-middleware/client?overlay=false&reload=true&path=/_next/webpack-hmr' +import webpackHotMiddlewareClient from 'webpack-hot-middleware/client?autoConnect=false' import Router from '../lib/router' export default () => { @@ -52,6 +52,18 @@ export default () => { } } + const { + __NEXT_DATA__: { + assetPrefix + } + } = window + + webpackHotMiddlewareClient.setOptionsAndConnect({ + overlay: false, + reload: true, + path: `${assetPrefix}/_next/webpack-hmr` + }) + webpackHotMiddlewareClient.subscribe((obj) => { const fn = handlers[obj.action] if (fn) { diff --git a/examples/with-app-subroute/next.config.js b/examples/with-app-subroute/next.config.js new file mode 100644 index 0000000000000..46ca28ed7bdb3 --- /dev/null +++ b/examples/with-app-subroute/next.config.js @@ -0,0 +1,3 @@ +module.exports = { + assetPrefix: '/blog' +} diff --git a/examples/with-app-subroute/package.json b/examples/with-app-subroute/package.json new file mode 100644 index 0000000000000..fc1699ebe063a --- /dev/null +++ b/examples/with-app-subroute/package.json @@ -0,0 +1,12 @@ +{ + "dependencies": { + "next": "^3.2.2", + "react": "^15.6.1", + "react-dom": "^15.6.1" + }, + "scripts": { + "dev": "next", + "build": "next build", + "start": "next start" + } +} diff --git a/examples/with-app-subroute/pages/index.js b/examples/with-app-subroute/pages/index.js new file mode 100644 index 0000000000000..247acbc6ef5da --- /dev/null +++ b/examples/with-app-subroute/pages/index.js @@ -0,0 +1 @@ +export default () =>
Welcome to the blog page
diff --git a/examples/with-app-subroute/readme.md b/examples/with-app-subroute/readme.md new file mode 100644 index 0000000000000..a241fc97f6af6 --- /dev/null +++ b/examples/with-app-subroute/readme.md @@ -0,0 +1,31 @@ +[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-app-subroute) + +# With app on subroute + +## How to use + +Download the example [or clone the repo](https://github.com/zeit/next.js): + +```bash +curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/hello-world +cd hello-world +``` + +Install it and run: + +```bash +npm install +npm run dev +``` + +Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)) + +```bash +now +``` + +## The idea behind the example + +This example shows how to run a Next.js application on a subroute, like `/blog` or `/admin`. + +To change the path open `next.config.js` and edit `assetPrefix`. diff --git a/examples/with-app-subroute/server.js b/examples/with-app-subroute/server.js new file mode 100644 index 0000000000000..8ed82d686af1e --- /dev/null +++ b/examples/with-app-subroute/server.js @@ -0,0 +1,27 @@ +const express = require('express') +const next = require('next') + +const port = parseInt(process.env.PORT, 10) || 3000 +const dev = process.env.NODE_ENV !== 'production' +const app = next({ dev }) +const handle = app.getRequestHandler() + +const config = require('./next.config') + +app.prepare() +.then(() => { + const server = express() + + server.get(config.assetPrefix, (req, res) => { + return app.render(req, res, '/', req.query) + }) + + server.use(config.assetPrefix, (req, res) => { + return handle(req, res) + }) + + server.listen(port, (err) => { + if (err) throw err + console.log(`> Ready on http://localhost:${port}`) + }) +}) diff --git a/package.json b/package.json index 04396a754e8e7..2026149740165 100644 --- a/package.json +++ b/package.json @@ -99,7 +99,7 @@ "walk": "^2.3.9", "webpack": "3.3.0", "webpack-dev-middleware": "1.11.0", - "webpack-hot-middleware": "2.18.2", + "webpack-hot-middleware": "2.19.0", "write-file-webpack-plugin": "4.1.0", "xss-filters": "1.2.7" }, diff --git a/server/hot-reloader.js b/server/hot-reloader.js index 3cebfb2823efd..bdee5654f5458 100644 --- a/server/hot-reloader.js +++ b/server/hot-reloader.js @@ -179,7 +179,7 @@ export default class HotReloader { ] let webpackDevMiddlewareConfig = { - publicPath: `/_next/${this.buildId}/webpack/`, + publicPath: `${this.config.assetPrefix}/_next/${this.buildId}/webpack/`, noInfo: true, quiet: true, clientLogLevel: 'warning',