-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Asset prefix support #2901
Asset prefix support #2901
Changes from all commits
58e1d4e
f468fa4
d166533
33bddf2
310f6e8
f6572c6
aba0fb6
d9ed153
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
import Router from '../lib/router' | ||
|
||
export default () => { | ||
|
@@ -52,6 +52,18 @@ export default () => { | |
} | ||
} | ||
|
||
const { | ||
__NEXT_DATA__: { | ||
assetPrefix | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above comment, a default value might help? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Next takes care of this in: https://github.com/zeit/next.js/blob/fe1b3e89f1d6253726074ee21755422b0c664197/server/config.js#L11 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
} = window | ||
|
||
webpackHotMiddlewareClient.setOptionsAndConnect({ | ||
overlay: false, | ||
reload: true, | ||
path: `${assetPrefix}/_next/webpack-hmr` | ||
}) | ||
|
||
webpackHotMiddlewareClient.subscribe((obj) => { | ||
const fn = handlers[obj.action] | ||
if (fn) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
assetPrefix: '/blog' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => <div>Welcome to the blog page</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
assetPrefix = ''
to use empty string in casewindow.__NEXT_DATA__.assetPrefix
is undefined?