Using a custom server.js for serving uploaded images from the next public folder after build.
Unanswered
Masai Lion posted this in #help-forum
Masai LionOP
Hi there! I'm building my custom Headless CMS with next js for learning mainly and hosting my content for my future portfolio website. 2 days ago I noticed that next js doesn't serve static assets after build time in the public folder. It was stated in the docs and in my media directory of my project I've build an upload feature where I'm automatically compressing images(currently supporting only pngs,jpgs and gifs) and formting them to webps (gifs remaining the same). My backend creates 3 separate files small/medium/large and for the gifs - original ( I don't want to use cloudenary or any form of CDN for images). So far i have no issues other then in dev mode the hot reload doesn't work so I figured out ill use next dev for that 😄 ( I haven't setup ws properly to handle that in my server.js). My N 1 Question is is this gonna affect anything in production since so far I have no issues on my radar and testing went really good with 0 bugs. I haven't used a custom server before( Actually I did used one with socket.io for a meme game that I made with next js for some friends and myself). So please point out any cons that might be there in my approach, coz I'm afraid of a big rewrite of my Media section in the near future( atm It supports a lot of features and can link a given image to multiple content created by the user and vice versa ).
2 Replies
as long as you have read the docs page of custom server about the drawbacks of using it then you might not have any surprises: https://nextjs.org/docs/pages/building-your-application/configuring/custom-server
using a custom server is not very recommended so it would be better to try better alternatives before, like storing your dynamic content in services like S3
using a custom server is not very recommended so it would be better to try better alternatives before, like storing your dynamic content in services like S3
Masai LionOP
Yep I've already did that. Since I'm building a headless cms atm and after reading the docs I don't think the SEO part is an issue for me. Using the custom server I had no issues with my current auth or my existing backend. My plan is to host the app on a digital ocean or on a hosting provider close to them just to have more controll over the server and the environment (used to it since in my work we almost self host everything). For the sites that will consume the content on the other hand I want to go with vercel/aws. I hope I don't get into any edge situation with the custom server.js in the future since for now on local env everything is working fine (I'm also able to consume the content and the images hosted/created from my cms ). Ps: ty for the fast reply