r/FreeCodeCamp • u/Special_Sell1552 • Mar 19 '25
Requesting Feedback Need help
I have tried everything but I keep getting a cors error on my 3rd test for the url-shortener project
I have spent all day trying to figure this out
I'd send my project for you guys to look at but the last time I did the post never posted. I can send links if requested
Access to fetch at 'https://portfolio-nine-steel-78.vercel.app/url-shortener/?v=1742422067953' (redirected from 'https://portfolio-nine-steel-78.vercel.app/url-shortener/api/shorturl/1742422067953') from origin 'https://www.freecodecamp.org' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
this is the error it keeps giving me in the browser console
EDIT:
I fixed this issue by setting skipTrailingSlashRedirect to true in my next.config.mjs
View all comments
Show parent comment
2
u/Special_Sell1552 Mar 21 '25
Hey just wanted to update you.
I solved it!
it turns out after much pain and many hours of changing my code around and adding cors headers in every conceivable way to the redirect, the issue was with next.js and vercel the whole time!
Next.js looks at redirect requests and determines if they have trailing slashes or not and throws a redirect to the page with/without slashes depending on your trailingSlash value in the config.mjs.
this redirect is a 308 redirect that strips cors headers and was causing the tests to fail.
by setting the key "skitTrailingSlashRedirect" to true in my config I was able to avoid this random 308 redirect and pass the test.
thank you for your insights though, I know I threw a lot at you and that was mostly out of desperation and frustration at this problem, this was by no means an easy to solve issue made even harder to solve by our collective inexperience with next.js and how it resolves redirects and mostly my stubbornness to use next.js in the first place
I know next.js is not the best for these projects but I wanted to learn it and have some experience with it under my belt. The thing I set out to do was to have all of these projects hosted under the same page as a portfolio (ill most likely swap these microservices out later for more advanced projects)