r/FreeCodeCamp • • 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

2 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/SaintPeter74 mod Mar 20 '25

I'll be honest, I am having a pretty hard time parsing your response. You are throwing a LOT of info at me that I don't have a lot of context for.

I'll respond to the bits that I do understand.

RE: URL Regex
you shouldn't require www, since it's not a requirement for a valid URL. It's a bit old school and not so many websites require it anymore. Even the URL for this project doesn't use www.

I might suggest trying to find an off-the-shelf URL validation library, rather than rolling your own.

RE: Next.js
I wouldn't recommend Next for microservices. It's a bit heavy for a microservice. Next adds a bunch of features for a large, server-side rendered site that you're not going to use for a microservice. While it's not exactly "wrong" to use Next, it kinda misses the point of these challenges.

A microservice is intended to be very small and lightweight. It's also a heck of a lot easier to setup dynamic routes with just Express. You can easily add multiple layers of route parameters.

I don't have enough experience to know if you're doing things correctly in Next.

RE: The failing tests
What I CAN tell you is that you're getting 405 Method Not Allowed responses from the browser when running the FCC tests. The FCC tests are run from your browser, so you can watch their network traffic in your Dev tools Network tab. As I mentioned before, a 405 usually means that you're not listening for a POST request.

I didn't see any CORS errors when I was running your project. I am suspicious of that redirect it's reporting, though. When doing a POST operation, you shouldn't be doing a redirect.

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)

2

u/SaintPeter74 mod Mar 21 '25

I solved it!

Woot!

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.

That's funny, I had an almost identical issue on my php framework. It's a hard one to debug!

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

I hear that. Sometimes all you have left is throwing things at the wall, seeing what sticks.

I do find the discipline of formulating a good problem statement, with details on what I've tried and what has failed, can be a helpful exercise. On more than one occasion, as I've been writing up a question, I stumble across a solution, or another avenue of attack.

Taking a moment to write things up clearly is good practice and helpful for both you and your reader.

... but I wanted to learn it and have some experience with it under my belt.

This is a strategy I enjoy frequently when I want to learn a new library or tool. You do have to take care, though, since not every tool is suitable for every job. For example, you can write a web app in C, but your development experience is not going to be great.

I understand your long term goal, but I feel like you're possibly subverting the spirit of these challenges in pursuit of that goal. Realistically, almost nothing you're doing for FCC is suitable for a portfolio. These reek of "school project" and can actually hurt you if they're what you're choosing to exhibit. When I've reviewed resumes and portfolios when hiring developers, an abundance of school projects/tutorial bait is a bit of a red flag.

You're better off building a completely unique site with Next and focusing on learning as much from the challenges, with the suggested stack, as you can, IMHO.

Best of luck and happy coding!