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.
- const isProd = process.env.NODE_ENV === ‘production’;
- module.exports = {
- // You may only need to add assetPrefix in the production.
- assetPrefix: isProd ? ‘https://cdn.mydomain.com’ : ”
- };
If the CDN is present on a separate domain, we have to set a configuration option as following:
- // next.config.js
- odule.exports = {
- crossOrigin: ‘anonymous’
- ;