Mitch Dev
Mitch Dev
  • 3
  • 81 296
Creating an SPA Router in Vanilla JS
In this video, I'll show a simple solution to routing for a vanilla JavaScript single-page application.
Demo showing this in action: 59tyx.csb.app/
Check out the code on GitHub: github.com/mitchwadair/vanilla-spa-router
Переглядів: 80 698

Відео

Friction in 2D Physics - SFramework Devlog 1
Переглядів 5074 роки тому
Hello! Welcome to SFramework devlog 1! In this devlog, I will implement friction in the SFramework 2D physics engine. Check out the project on GitHub: github.com/mitchwadair/sidescroller-framework Download SFramework: github.com/mitchwadair/sidescroller-framework/releases Learn about SFramework: github.com/mitchwadair/sidescroller-framework/tree/master/doc Need to catch up on SFramework devlogs...
Triangle Colliders - SFramework Devlog 0
Переглядів 1274 роки тому
Hello! Welcome to the first development log video for SFramework! I'm pretty new to this so any feedback is appreciated. Check out the project on GitHub: github.com/mitchwadair/sidescroller-framework

КОМЕНТАРІ

  • @scubahick
    @scubahick Місяць тому

    how about loading scripts when loading those other html files? I am having a problem with setting innerHTML and the JS not working with fetched HTML files.

    • @Divyanshusinghal-ll6tp
      @Divyanshusinghal-ll6tp 8 днів тому

      create a script element pass the script location into src and append in the body using js where fetch api logic is present

  • @nikilshetty6719
    @nikilshetty6719 Місяць тому

    Vanilla js routing

  • @jdmagazine1
    @jdmagazine1 2 місяці тому

    THIS HELP SO MUCH! THANK'S

  • @clipmart1
    @clipmart1 2 місяці тому

    Load big html css it goes slow

  • @nemanjatrivic9505
    @nemanjatrivic9505 4 місяці тому

    How to have dynamic urls, like /blogs/1 for example?

  • @spondoolie6450
    @spondoolie6450 5 місяців тому

    To anyone trying to dynamically load javascript files to the different pages ..... read below This video is awesome and the router works as intended, but when I was trying to get an element by ID (for example) of part of the html that hadn't been loaded yet I couldn't get it to work. I tried multiple things and finally found a really good solution I think. I'm linking my router.js file on my current project so you can see what it looks like, but basically each "page" I'm routing to is in a subdirectory with a css and js file. The <link> tag works for the css so that part is fine, but the <script> tag wouldn't work. I tried putting the code directly in the tag (it's rendered via innerHTML so that isn't allowed). I tried dynamically inserting it into a <script> tag on the index.html page but that didn't work either. The solution here is that I build a new <script> tag with the correct src and then replace the one currently on index.html. If you use appendChild it works too, but you'll have 30 <script> tags before you know it. Also, you can add script.type = "module" on the handleLocation function in router.js so that you can use imports, but then I found that it only renders once and then never again. github.com/fughesi/BloomingGeckos.com/blob/main/client/router.js

  • @AliveDeejay
    @AliveDeejay 5 місяців тому

    This 3 minute clip just saved me from learning Angular, React, Node.js and TypeScript. I know there are quite a bit differences, but for my purpose this is exactly what i needed, thanks! :D

    • @reze_dev
      @reze_dev 4 місяці тому

      I came here for the same reason and use htmx or alpinejs for reactivity

    • @Daniel_WR_Hart
      @Daniel_WR_Hart 2 дні тому

      TypeScript is still really good to learn, and it's a lot easier than any framework

  • @dwchau.254
    @dwchau.254 6 місяців тому

    can u create a dynamic route in vanilla js ? example: /detail/:id --> show id in screen

  • @haidang3806
    @haidang3806 7 місяців тому

    Thanks a lot

  • @severn-master
    @severn-master 7 місяців тому

    surprisingly, i found a very clear usecase of vanilla js for routing from non indian youtuber

  • @vibhanshuranjanthakur891
    @vibhanshuranjanthakur891 7 місяців тому

    Can I run this as a static website from S3 or does it need a server to render files?

  • @kamutabung511
    @kamutabung511 7 місяців тому

    awesome, simple and clear solution

  • @muhamad1247
    @muhamad1247 8 місяців тому

    Yeah bro its work thankyou

  • @VictorsTravelvLog
    @VictorsTravelvLog 9 місяців тому

    very nice! though your example does not demonstrate the advantages of client-side routing over server-side routing? fetch still grabs the entire contents of the 3 pages, every time a link is clicked?

  • @frontend_ko
    @frontend_ko 10 місяців тому

    thank you very much

  • @juancmuyou
    @juancmuyou 10 місяців тому

    How i can use ejs instead html for SPA?

  • @taiethkl
    @taiethkl 11 місяців тому

    Thank you

  • @clevermissfox
    @clevermissfox 11 місяців тому

    Very concise! I am confused where you’re fetching the html for each page from and how there are two const route variables initialized? Assume it has something to do with async/await concepts I haven’t learned yet 😂

  • @rahff99
    @rahff99 Рік тому

    window.event is deprecated, I don't found any alternative.

  • @life8261
    @life8261 Рік тому

    Great video, Mitch Dev. Thank you for such a nice and simple video! It's much better to get everything you need for less than 4 minutes than watching my professor's 1.5h lecture which would give me no idea what is going on.

  • @ВалераВласенков-ю2о

    still cant get why do we need route function Looks like ive got why we need route function. So if we have <a></a> buttons then we just set click event on them and call route function in callback function, right?

  • @hartono9583
    @hartono9583 Рік тому

    how if in html run javascript fuction and fetch api? like product page?? please tutorial?

  • @maximumcockage6503
    @maximumcockage6503 Рік тому

    Fetch is too slow. Just use template literals and innerHTML

  • @Surush_Cyrus
    @Surush_Cyrus Рік тому

    Exactly what I was looking for, works great, you the man!

  • @cCcesSsarRr
    @cCcesSsarRr Рік тому

    Heeelp!!, How can I use it with parameters?

  • @a-z-n-b-a-i
    @a-z-n-b-a-i Рік тому

    thanks for the tutorial. unfortunately on refresh it throws a "Cannot GET" message

  • @juquanmao3969
    @juquanmao3969 Рік тому

    Thanks for your video. From your code, I can see that handleLocation function is mannuallly triggered in route() function. where it should be triggered by pushState().

    • @juquanmao3969
      @juquanmao3969 Рік тому

      Here is a modified .js which works for me: const route = (event) => { event = event || window.event; event.preventDefault(); // pushState method to update the browser's URL during page navigation. window.history.pushState({}, "", event.target.href); // To trigger window.onPoPState window.history.pushState({}, "", event.target.href); history.back() }; const routes = { 404: "/pages/404.html", "/": "/pages/index.html", "/about": "/pages/about.html", "/lorem": "/pages/lorem.html", }; window.onpopstate = () => { const path = window.location.pathname; const route = routes[path] || routes[404]; fetch(route) .then((res) =>res.text()) .then (html => document.getElementById("main-page").innerHTML = html) .catch(error =>document.getElementById("main-page").innerHTML = 'unexpected error') };

  • @abahiduh3246
    @abahiduh3246 Рік тому

    is just that u need an extended knowledge of js to do this.... thank you very much

  • @abahiduh3246
    @abahiduh3246 Рік тому

    wow! Wonderfu

  • @Mobilemaniaplays
    @Mobilemaniaplays Рік тому

    Making the clone of Spotify using only vanilla js , this is my favourite video , thanks bro, love from India

  • @sespis2r
    @sespis2r Рік тому

    I wanted かめりあ - Routing SPA what is this youtube

  • @MarquisDeSang
    @MarquisDeSang Рік тому

    no matter what I do, it will navigate to the other page loosing everything.

  • @whoIsV
    @whoIsV Рік тому

    What does window.route = route do?

  • @Nodsaibot
    @Nodsaibot Рік тому

    that URL scheme will cause 404 errors, you need to rewrite all requests to the index or redirect 404s to the index, or just use Hashes instead

    • @lexmachina8961
      @lexmachina8961 Рік тому

      can you give a snippet to show how to redirect to the index based on the code provided? Thanks

  • @nefed-L
    @nefed-L Рік тому

    Great tutorial! Thanks!

  • @JavadRezaii
    @JavadRezaii Рік тому

    I did it all , links are working but when I add '/about' to my website url it says page not found 404 ( not my own 404 page )

  • @2005bgva
    @2005bgva Рік тому

    Hi Mitch, thanks for this video. Maybe could you do the same with a vanilla js created with vite?

  • @renatolins4670
    @renatolins4670 Рік тому

    This tutorial is just awesome, but in my case, when I refreshe the page in any of the routes (that is not the home), it just doesnt load.. I see the message: "Cannot GET"

  • @neilgibs6673
    @neilgibs6673 Рік тому

    This vidéo is great ,direct and clear . Thank's it helped a lot

  • @GingLouie
    @GingLouie 2 роки тому

    Such a straightforward implementation of vanilla JS routing. THANKS! i had already looked at several tutorials attempting the same. They were across the board clunky and, at times, arcane (at least to me). You saved me many hours of frustration and aggravation.

  • @mauriciolaratro
    @mauriciolaratro 2 роки тому

    Hello friend, I tried to use this method for a SPA and it worked perfectly, but when I want to use images as "a", the "oneclick" does not work and it is only possible to access the "a" by clicking around the img. But it doesn't work if you click inside it. Any advice on how to solve this? I have very little idea of js, so I'm stuck with this problem.

  • @kangsan2014
    @kangsan2014 2 роки тому

    How can we write a localhost without any framework or packages in vanilla Javascript? I can nowhere on the internet on how to do that?

  • @developerGorilla
    @developerGorilla 2 роки тому

    is window.route = route; neccessary? If neccessary, what for?

    • @LoneWolf-ey1hq
      @LoneWolf-ey1hq Рік тому

      This is something I still struggle to figure out.

    • @nefed-L
      @nefed-L Рік тому

      I removed it because found somewhere on the internet that it is a bad practice (creating global variables, through assigning them as global object/document properties).

    • @davidestrada3791
      @davidestrada3791 3 місяці тому

      I removed it but it causes the page to reload. Not sure how this is working

    • @Daniel_WR_Hart
      @Daniel_WR_Hart 5 годин тому

      I'm guessing it's only necessary if his JS was a module, but since it's not, the route function should be globally accessible anyways

  • @mauriciolaratro
    @mauriciolaratro 2 роки тому

    Thank you for this great contribution, simple and perfect explanation.

  • @arimos3136
    @arimos3136 2 роки тому

    Thx.

  • @didierbelle3723
    @didierbelle3723 2 роки тому

    This is an awesome video. Thank You.👏

  • @lorenz6746
    @lorenz6746 2 роки тому

    is scrolling to an id still posible?

  • @nikoing
    @nikoing 2 роки тому

    It's so nice, clear and perfect this video. However, I would like to know what will happen if we refresh the page ? i.e « If I'm on (about page) then I refresh, it will go back (to index page) ? »

  • @TheBeastHaerup
    @TheBeastHaerup 2 роки тому

    Edit: Solved - if you are using webpack, remember to set historyApiFallback: true, in the devServer Hey Mitch, how can I serve the right page to the URL on page refresh? For example if I navigate to /blog and refresh the page I would get a 404. I have set things up with webpack and all seems to work, just not on page refresh. Cheers

  • @prometheus5770
    @prometheus5770 2 роки тому

    that's beautiful