#### Using Digitalocean Spaces as CDN for Next.js _next directory

I have a Kubernetes cluster on Digitalocean where www.example.com points to my Next.js application. This works as expected, however it serves all the assets from that same pod where my Next.js is running:

https://www.example.com/_next/static/chunks/webpack-16e102404d6e7c36f3ae.js
https://www.example.com/_next/static/chunks/framework-852c1b21255b4351ab3d.js


and so on.

Now I would like to serve these static files from a CDN instead, so I started researching on how to do this. One thing I found is that DigitalOcean offers CDN functionality through their Spaces, and one other thing is the documentation from Next.js here: https://nextjs.org/docs/api-reference/next.config.js/cdn-support-with-asset-prefix

So I set up a DigitalOcean Space, which is now available through https://cdn.example.com and also I followed the instructed from Next.js and modified my next.js.config file like this:

const isProd = process.env.NODE_ENV === 'production';

module.exports = {
// Use the CDN in production and localhost for development.
assetPrefix: isProd ? 'https://cdn.example.com' : '',
future: {
webpack5: true,
},
};


And deployed it. But of course, this doesn’t work – the files that are generated during the build stage never get uploaded to my CDN. So now when I open my site it doesn’t load any of the static files, because these URLs don’t exist:

https://cdn.example.com/_next/static/chunks/webpack-16e102404d6e7c36f3ae.js
https://cdn.example.com/_next/static/chunks/framework-852c1b21255b4351ab3d.js


So now my question is, how do I set this up? As I understood there are two possible ways to do this.

1: is to configure my DigitalOcean Space to point at the _next folder of my pod, the first request will then serve the file still from my pod, but every request after that will then serve the file from the CDN

2: during the build phase in deployment I would have to upload my files to the CDN’s _next folder.

And here is where I am stuck – I have no idea how to do either. For option 1, I tried finding such a setting inside DigitalOcean but couldn’t find anything.

For option 2, this is my current workflow:

1. I make changes to the code
2. I commit the changes to Github
3. Github Actions is configured so that it will automatically build a new Docker image
4. Github Actions then pushes this new Docker image to my registry
5. Github Actions then updates my Kubernetes cluster, tells it to use this new image for my Next.js application

If I would have to make changes to this workflow to upload things to the CDN, where would I have to do it? My Dockerfile is a multistage file (3 stages) and only in the 2nd stage I run the build command.

Source: Docker Questions