Next.js Middleware & Cors | Nextjs 13 tutorial

Поделиться
HTML-код
  • Опубликовано: 18 сен 2024

Комментарии • 114

  • @deepyslow
    @deepyslow Год назад +7

    Stumbled into this channel 2 days ago! It's ridiculous the amount of stuff I have learned already. What a great teacher.

  • @mohitcodeswell
    @mohitcodeswell Год назад +9

    Awesome content Dave! Carry on these videos and please focus on the real world as much as you can.💯

  • @shayanhdry6224
    @shayanhdry6224 11 месяцев назад +1

    Thank you Dave I was afraid of App router but thanks to you I understand it and understand the Docs better

  • @estevanulian
    @estevanulian 2 месяца назад

    That's what I call high-quality content! Thank you.

  • @sunnymittal1906
    @sunnymittal1906 Год назад +1

    Thanks, Dave! I'm basically a master in React but am just getting acquainted with Next 13 and this was super helpful. Many thanks! New subscriber :)

  • @cwd05011
    @cwd05011 Год назад +2

    Thank you for all of your videos. Your kind and detailed explanations make it really easy for me to understand the topics well.

  • @Tyheir
    @Tyheir Год назад

    Needed to migrate from express to next. Was completely lost with my cors errors, u saved me a lot more hours of headaches thank you

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад

      Great to hear!

    • @Tyheir
      @Tyheir Год назад

      @@DaveGrayTeachesCode I spoke too soon. I’m trying a POST request from the client and the server recognizes it as options so never executes the post method, do you have a video on this?

  • @ozzyfromspace
    @ozzyfromspace Год назад +1

    ngl this video took an unexpected turn (meant in a GOOD way). This was very informative and I learned a ton. Thanks, Dave!

  • @rossanobr
    @rossanobr Год назад

    I don't now how to thank you for this video. I live in a very poor place and I would never have enought money to pay a class with the content of your channel. Thaank you

  • @NSDrowned
    @NSDrowned Год назад +1

    Great content! Exactly what I needed to know about how to apply cors without a library 👍

  • @NOTHING-en2ue
    @NOTHING-en2ue Год назад

    i still can't believe that you put these tutorials for free, thanks a lot ❤

  • @andresgutgon
    @andresgutgon Год назад +1

    This helped me to understand better CORS in NextJS. Thanks!

  • @maurocamerini90
    @maurocamerini90 7 месяцев назад

    The only solution that worked, i were so happy when i saw the console log from the middleware!!! Thank you so much!

  • @mattburgess5697
    @mattburgess5697 Год назад +2

    I fundamentally don’t get this middleware approach. I’ve seen dozens of examples and they all do the same things: apply ONE middleware. But this isn’t how middleware works. It’s typically a stack. The API routes need CORS. The authorised routes need auth middleware. Language detection.
    How you would handle this in this pattern is a complete mystery to me.

  • @frostyfreezemovies
    @frostyfreezemovies Год назад +1

    Hello and welcome sir ❤️ i m from non cs background working as a full stack dev bcoz of dave sir ❤️...!

  • @ofirlevy3682
    @ofirlevy3682 Год назад +1

    Thanks,like always amazing content.
    Please do session on Auth front and back with Next

  • @TJMcCarty
    @TJMcCarty Год назад +1

    in production, my PUT requests don't work if the origin is not the local site. It never gets past OPTIONS in the middleware. So a public PUT API endpoint isn't working for me. Have you been able to get that to work for you?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад

      There are some bugs depending on the version you have installed. If you are using 13.3, go back to 13.2.4 and see if this issue persists.

    • @TJMcCarty
      @TJMcCarty Год назад

      @@DaveGrayTeachesCode Thanks for the response. I'm using 13.2.4 and it's still not working. I've added this to NextResponse:
      return NextResponse.json({ messages: updatedMessages }, {
      status: 200,
      headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'OPTIONS, PUT',
      'Access-Control-Allow-Headers': 'Content-Type, Authorization',
      } })
      It isn't working for me. It's very depressing lol I'm probably going to have to rebuild everything in pages/api. I built a ton of stuff using route handlers in development and everything worked with postman. But in production it's not working for me

    • @tomnewton3191
      @tomnewton3191 Год назад

      @@TJMcCarty I haven't played around with it too much but did you get this working? I am trying to POST to an api endpoint I created and I am also ecountering the OPTIONS issue in the middleware.

    • @TJMcCarty
      @TJMcCarty Год назад +1

      @@tomnewton3191 This video fixed my issues: ruclips.net/video/yRJd_tlHu9I/видео.html

  • @TravinskiyVladislav
    @TravinskiyVladislav Год назад +1

    Hello and welcome... Thank you

  • @user-gm3lg8gp3m
    @user-gm3lg8gp3m 2 месяца назад

    Amazing! Thank you man!

  • @AustrianTutorialHD
    @AustrianTutorialHD Год назад +1

    Great video, you explained everything extremely good! But I don't see why anyone would want to limit their APIs globally nad not User specific? How could I use the RateLimiter but only Limit on IP, so that when a user used all his request he must wait and other users (with different IPs) can still access the API?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад +1

      Other npm packages for rate limiting support what you want.

    • @AustrianTutorialHD
      @AustrianTutorialHD Год назад

      @@DaveGrayTeachesCode Thanks, you are right, rate-limiter-flexible for example is perfect for my usecase

  • @jorgegarnicablanco8136
    @jorgegarnicablanco8136 Год назад

    thanks a lot for this tutorial Dave!

  • @DrewGillen
    @DrewGillen Год назад +1

    Great video, you are a good teacher!

  • @justiceessiel6123
    @justiceessiel6123 Год назад

    you made mention of the RateLimiter having a dependency that is would not work in the edge Runtime. How do I know the dependencies that wont work on the Edge Runtime. Wish I can get a little more context on the Edge Runtime as well

  •  10 месяцев назад

    Could You make a tutorial about image uploading using a pre-signed URL to Digital Ocean Spaces? It involves "fighting" "No 'Access-Control-Allow-Origin' header is present on the requested resource" problem on PUT request.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  10 месяцев назад

      Sounds like you are facing a CORS issue. That must be set server side. Here's a quick explainer: ruclips.net/user/shorts1iOeoRCUD4M

    •  10 месяцев назад

      All I can set on serverside is Origin(mine is *) methods(all enabled), allowed headers(mine *) and time (mine 3600)@@DaveGrayTeachesCode
      After fighting 2 days on this problem and trying out about 50 different solutions from everywhere - I know what CORS is and that the server is not responding with the required header on PUT requests though preflight has it. All points are on DO side and there's not much I can configure there. I am wondering if some 100k people should face the same problem.
      I also have next.js config for API path which I don't understand well, but allows all domains.

    •  10 месяцев назад

      Maybe You can try - tell if You succeed.

    •  10 месяцев назад

      ok. I overcame that cors problem, now this in the presigned url
      SignatureDoesNotMatch
      tx00000e5a133325dcf2020-00655de893-471ab797-ams3c
      471ab797-ams3c-ams3-zg03
      Access keys are correct and rechecked and I also regenerated them, because someone told that that worked for him. Tried both fetch and axios for REST, even deleted Authorisation from PUT request. I also synced time, and changed timezones - nothing helps.
      @@DaveGrayTeachesCode

  • @vittoriomorellini1939
    @vittoriomorellini1939 Год назад

    I appreciate a lot this lesson. A perfect explanation of waht is a middleware, this concept I can apply to every language as every architecture. Very good the explanation about cors how to manage the error with a new NextResponse. Thank you Dave!!

  • @lukeskywalker7029
    @lukeskywalker7029 Год назад

    Thx for the limiter feature! Super good :)

  • @ASCIIaday
    @ASCIIaday Год назад

    Hi Dave I have a doubt. you have written ratelimiting logic in hello/route.ts instead of middleware.ts which doesn't denonstrate use of midleware.ts. Is it the right way to do it? or should it be written in middleware.ts

  • @user-kc3fo1tp4e
    @user-kc3fo1tp4e 7 месяцев назад

    hi i try your code but the origin in middleware return nothing. It's normal ? I try also when my app is deploy on my domain but return alway nothing

  • @youknowwho4994
    @youknowwho4994 Год назад

    Kudos Dave! Great NextJS series. Do you have any plans to cover parallel routes and intercepting routes?.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад

      Thank you! Good question - not sure if I want to include those in the beginners series but I will cover them at some point.

  • @randomtalks5253
    @randomtalks5253 Год назад

    Loving ur way of explaining. Can u please elaborate if its bad practice (efficiency wise) to use fetch (instead of DB /SQL calls) within API routes coz this way you are using double server side requests PLUS another DB call which the fetch makes internally!
    Thx

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад

      It just depends on where you are getting data from. If you are getting data from another API, you will likely fetch the data. If you are getting data from a database, you probably won't use fetch.

  • @romachamp10
    @romachamp10 Год назад

    Your courses are awesome. It would be so nice to see React native course on this channel)

  • @ctown4life
    @ctown4life Год назад

    Thanks for the great info, super helpful. For the RateLimiter are the tokensPerInterval shared between all requests or are they per user (or device)? Looking around docs and haven't found the answer yet. I'm assuming it's the former.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад

      In this example, if I remember right, they are all shared. There is are other npm packages that will track per individual.

  • @AchourMeguenni
    @AchourMeguenni 10 месяцев назад

    how can i setup two middlewares for two different paths ?

  • @mywaycoding6413
    @mywaycoding6413 Год назад +1

    Great Work Sir we very thankful to you .....

  • @yusufturhan2845
    @yusufturhan2845 10 месяцев назад

    Hello, my next js version is 13.0.5. There is no src folder. Where should I add my middleware file?

  • @gautekillfr5344
    @gautekillfr5344 Год назад

    C'était top, merci pour l'actu ! 👌

  • @czancdy144
    @czancdy144 Год назад +1

    Hello dave sir, it would be great if you could make just one video for ssr in nextjs stable version with rtk query and typescript, just the setup and cocepts because i just could not find any documentation or anything, (also next-redux-wrapper)

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад +2

      Not going backwards as Next.js is moving forward quickly. These beta features will be the stable version soon.

  • @hooyah
    @hooyah Год назад

    i have try limiter and deployed in vercel. and this limiter count is global, not specif by ip address.
    i try limiter for /api/hello
    and i hit the endpoint in diferent network. my mobile data and wifi. but the count is not isolated by ip address.
    first hit in laptop remain counter is 2, then i hit in mobile, the remain counter is 1.
    sorry my english bad. i hope you understand. and how i can limited the request for diferent ip address?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад

      You can try this package instead: www.npmjs.com/package/express-rate-limit ...it does limit requests based on IP.

  • @aurelienchevillotte2874
    @aurelienchevillotte2874 Год назад

    Great Video Dave ! Is it also possible to use npm cors dependencie in Next-Js if we prefer to deal with it in case of we want more than same-origin only (which is already integrated in Next) ?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад +1

      In this video I show how to set up your API to support more than same origin. The current cors package that was designed for node and Express does not work with the new route handlers to the best of my knowledge. This video shows you how to accomplish the same without it.

  • @gregthomas5887
    @gregthomas5887 Год назад

    Good day Dave. For some reason, these regexp and conditionals work perfect in development but misbehave in production. Have you ever encountered any similar situations?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад

      Good question! Answer: no, but things change quickly. I believe I highlighted the beta docs having a TODO under middleware. See if the docs have now updated?

  • @damiantriebl1747
    @damiantriebl1747 Год назад

    have you the idea to apply RTK to Next? in server and client components?

  • @pallyweb
    @pallyweb Год назад

    Keep great work up. 🥳 . How you are getting so great and fast auto completion in VS Code ?

  • @paulvickers8059
    @paulvickers8059 6 месяцев назад

    Does this solution for cors still work. Just I'm still having the blocked by cors policy

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  6 месяцев назад

      Responding properly to an options request will always fix CORS

    • @paulvickers8059
      @paulvickers8059 6 месяцев назад

      @@DaveGrayTeachesCode so should the code in the video still work in a current Nextjs app?

    • @paulvickers8059
      @paulvickers8059 6 месяцев назад

      I've spend hours and hours trying to fix this. I'll go through the video again if this is a solution

  • @madansrivastava5069
    @madansrivastava5069 Год назад

    I will watch it later, came here just to like it

  • @ismendoza
    @ismendoza Год назад

    There isn't nothing about CSRF in all Nextjs Documentation, I'd like to know how nextjs protect against csrf attack

  • @alsherifkhalaf7385
    @alsherifkhalaf7385 Год назад +1

    Big Thanks ❤

  • @pintokatendejonathan1740
    @pintokatendejonathan1740 Год назад +1

    Has NextJS become another programming language?

  • @rohulahadi3990
    @rohulahadi3990 Год назад

    Thank You Dave

  • @MusaMuhammedOlayinka
    @MusaMuhammedOlayinka Год назад +1

    Thanks sir ❤

  • @jhsu8447
    @jhsu8447 11 месяцев назад

    Wonerful tutorial !!!😃😃😃

  • @mdbadhon5849
    @mdbadhon5849 Год назад

    Sir please create next-auth full tutorial ❤

  • @ZahidulIslam-mp2yr
    @ZahidulIslam-mp2yr Год назад

    Sir, When do we get video on next Auth including role based protected route?

  • @frontend_ko
    @frontend_ko Год назад

    When will versiom 13 be more popular than version 12? Is it worth studying version 12 right now?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад

      When they take most features out of beta. I would only learn 12 if your job gives you a project with it.

  • @user-te2pe5qm8o
    @user-te2pe5qm8o Год назад

    How can I build anything good without Dave?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад +1

      Thank you, but you must. Building your own projects is part of the journey. 🚀

    • @user-te2pe5qm8o
      @user-te2pe5qm8o Год назад

      ​@@DaveGrayTeachesCode Already working on it.
      I've escaped tutorial hell for a while, recently I do this just to relax or search for help.
      I am trying to use Next to make a showcase project for potential customs or jobs. when finished I will show you.

  • @Froglol06
    @Froglol06 Год назад

    Hello how to manage multiple middleware?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад

      I showed how to apply more than one = multiple. You could add more.

  • @alexanderkomanov4151
    @alexanderkomanov4151 Год назад

    Thanks again!

  • @jesseemana9598
    @jesseemana9598 Год назад +2

    coding bootcamps hate him

  • @linuxstudy1
    @linuxstudy1 Год назад

    very usefull tutorial thankyou

  • @uv_codes
    @uv_codes Год назад

    How to use cors from npm?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад

      Cors from npm is for Node/Express apps. I show how to do the exact same thing without needing that package. Watch the video.

  • @kacperkepinski4990
    @kacperkepinski4990 9 месяцев назад

    there is no middleware file in your github

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  9 месяцев назад

      Yes, there is. Open the lesson 9 folder. Then open the src folder. Ta-da! 😃

  • @AmodeusR
    @AmodeusR Год назад

    it doesn't seem practical at all to create APIs with Next, It needs to type too much basic stuff like headers. I hope they change it eventually.

  • @AntNZ
    @AntNZ Год назад

    Another awesome video Dave. Please please please follow up with some Next-Auth :)

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Год назад +2

      I plan to cover auth sometime after this fundamentals series.

    • @AntNZ
      @AntNZ Год назад

      @@DaveGrayTeachesCode 🤓❤️