How to set up CDN in Next.js?

To setup CDN in Next.js, the developers have to follow the steps given below:

To start, we have to first set up the “assetPrefix” setting and configure our CDN origin to support and resolve the domain that our Next.js is hosted on.

  1. const isProd = process.env.NODE_ENV === ‘production’;  
  2.  module.exports = {  
  3. // You may only need to add assetPrefix in the production.  
  4.  assetPrefix: isProd ? ‘https://cdn.mydomain.com’ : ”  
  5. };  

If the CDN is present on a separate domain, we have to set a configuration option as following:

  1. // next.config.js  
  2. odule.exports = {  
  3. crossOrigin: ‘anonymous’  
  4. ;  

Leave a comment

Your email address will not be published. Required fields are marked *