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

Set assetPrefix dynamically #3661

Merged
merged 3 commits into from
Feb 2, 2018
Merged

Set assetPrefix dynamically #3661

merged 3 commits into from
Feb 2, 2018

Conversation

arunoda
Copy link
Contributor

@arunoda arunoda commented Feb 2, 2018

Sometimes, it's pretty useful to set the assetPrefix dynamically.
Basically with this, we can turn on and off CDN usage for individual requests.

Here's an example:

const next = require('next')
const micro = require('micro')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
  const server = micro((req, res) => {
    // Add assetPrefix support based on the hostname
    if (req.headers.host === 'my-app.com') {
      app.setAssetPrefix('http://cdn.com/myapp')
    } else {
      app.setAssetPrefix('')
    }

    handleNextRequests(req, res)
  })

  server.listen(port, (err) => {
    if (err) {
      throw err
    }

    console.log(`> Ready on http://localhost:${port}`)
  })
})

Copy link
Member

@timneutkens timneutkens left a comment

Choose a reason for hiding this comment

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

💯

@timneutkens timneutkens merged commit 4c7f6cc into canary Feb 2, 2018
@timneutkens timneutkens deleted the set-asset-prefix-api branch February 2, 2018 14:43
@lock lock bot locked as resolved and limited conversation to collaborators Feb 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants