Skip to content
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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion client/on-demand-entries-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import Router from '../lib/router'
import fetch from 'unfetch'

const {
__NEXT_DATA__: {
assetPrefix

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 case window.__NEXT_DATA__.assetPrefix is undefined?

}
} = window

export default () => {
Router.ready(() => {
Router.router.events.on('routeChangeComplete', ping)
})

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'
})
Expand Down
14 changes: 13 additions & 1 deletion client/webpack-hot-middleware-client.js
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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the overlay option needs use here.
See:
screen shot 2017-09-10 at 3 47 12 am

(Try to add a syntax error)

import Router from '../lib/router'

export default () => {
Expand Down Expand Up @@ -52,6 +52,18 @@ export default () => {
}
}

const {
__NEXT_DATA__: {
assetPrefix

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above comment, a default value might help?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The 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) {
Expand Down
3 changes: 3 additions & 0 deletions examples/with-app-subroute/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
assetPrefix: '/blog'
}
12 changes: 12 additions & 0 deletions examples/with-app-subroute/package.json
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"
}
}
1 change: 1 addition & 0 deletions examples/with-app-subroute/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <div>Welcome to the blog page</div>
31 changes: 31 additions & 0 deletions examples/with-app-subroute/readme.md
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`.
27 changes: 27 additions & 0 deletions examples/with-app-subroute/server.js
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}`)
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion server/hot-reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down