404.html
to the repository, any non-existent URL will be redirected to it. Wait a minute, isn't it the same thing we do on the server so that the beautiful URLs can work? We can do exactly the same thing that we do on the server using JavaScript in 404.html
! (function(){ document.body.className = 'redirecting'; var slug = location.pathname.slice(1); xhr({ src: 'secrets.json', onsuccess: function () { var slugs = JSON.parse(this.responseText); var hash = slugs[slug]; if (hash) { // Redirect var url = hash.indexOf('http') == 0? hash : 'http://dabblet.com/gist/' + hash; $('section.redirecting > p').innerHTML = 'Redirecting to <a href="' + url + '">' + url + '</a>…'; location.href = url; } else { document.body.className = 'error not-found'; } }, onerror: function () { document.body.className = 'error json'; } }); })();
Source: https://habr.com/ru/post/316790/
All Articles