Show HN: I created a URL shortener that can be entirely hosted on GitHub Pages
111–120 of 182 posts
Re: Show HN: I created a URL shortener that can be entirely hosted on GitHub Pages
#112Re: Show HN: I created a URL shortener that can be entirely hosted on GitHub Pages
#113I just found a "bug" that can be abused (in chrome at very least).
URL('http:53') returns https://0.0.0.53 (it casts the int32 number to an ip address, dont' ask why). But window.location.replace behaves differently and treats it as a relative URL.
Re: Show HN: I created a URL shortener that can be entirely hosted on GitHub Pages
#114Re: Show HN: I created a URL shortener that can be entirely hosted on GitHub Pages
#115Interesting, I just did http://nlsn.cf/54 . I just found a "bug" that can be abused (in chrome at very least). URL('http:53') returns https://0.0.0.53 (it casts the int32 number to an ip address, dont' ask why). But window.location.replace behaves differently and treats it as a relative URL.
Re: Show HN: I created a URL shortener that can be entirely hosted on GitHub Pages
#116Using GH issues as DB :P I wonder how long before GitHub will notice and shut it down for abuse.
Re: Show HN: I created a URL shortener that can be entirely hosted on GitHub Pages
#117Interesting, I just did http://nlsn.cf/54 . I just found a "bug" that can be abused (in chrome at very least). URL('http:53') returns https://0.0.0.53 (it casts the int32 number to an ip address, dont' ask why). But window.location.replace behaves differently and treats it as a relative URL.
Oh, wait.
Re: Show HN: I created a URL shortener that can be entirely hosted on GitHub Pages
#118> Unlike many URL shorteners, this one does not need a database and can be entirely hosted on GitHub pages. it does need a database though. it just uses someone else's database
Just like serverless still has servers, this can be reasonably called dbless :)
Re: Show HN: I created a URL shortener that can be entirely hosted on GitHub Pages
#119Yes, this is an interesting technical demonstration of a concept, but it also has some rather serious problems, so that I would strongly recommend that no one actually do things this way.
----
Firstly, the size of the code: I just golfed it for fun, I think this should do for the full document (though I haven’t tested it):
Redirecting...e=new XMLHttpRequest(),e.addEventListener("load",function(){try{if(/^https?:\/\/(?!nlsn\.cf(\/|$))/.test(e=JSON.parse(e.responseText).title))return location.replace(e)}catch(e){}location.replace("/")}),e.open("GET","https://api.github.com/repos/nelsontky/gh-pages-url-shortener-db/issues/"+location.pathname.split("/")[1]),e.send()
100KB in four requests, down to 412 bytes in one request (and that 20 bytes of isn’t even particularly valuable). And it restores support for IE, by using simple regular expression testing instead of `new URL(…)`, which IE never supported. As written, I believe it should work down to IE9, and older could be supported if you replaced the load event handler with an onreadystatechange incantation.(Other general remarks on the HTML:
1. In , the trailing slash is completely ignored by the HTML parser; in fact, I reckon using trailing slashes like this is actively slightly harmful, because it misleads you into thinking it closes elements, as in XML, but it doesn’t.
2. On the script elements, type="text/javascript" is the default and thus not needed.
3. , and wrappings can all be omitted from the source. This one is a bit more subject to taste; I know some like their source to match the parsed document tree, even down to things like writing the out even if there is no thead or tfoot. But I myself always omit the end tags on these, and omit the start tags unless they have attributes, which in practice means I always have the for its lang attribute.
)
----
Secondly, the redirection technique. There are good reasons why you should use HTTP redirects and not JavaScript location.replace for something like this:
1. If you use JavaScript, any users unable to run the JavaScript are left high and dry. This includes people like me that disable JavaScript by default (I because it makes the web so much faster and less annoying; others for privacy reasons and similar), but it would also help people on poor-quality connections, especially if the JavaScript is loaded in a different connection (which will probably not be the case here), because it’s surprisingly common for parts of pages’ resources to simply fail to load sometimes on poor-quality connections.
2. Various tooling like link checkers likewise won’t be able to work with this. As it stands, even link checkers that ran JavaScript would fail on this because you’re also using location.replace to show the “bad link” error. (Another of my pet peeves: doing a redirect to a “not found” page, rather than serving the “not found” page at the original URL with status code 404.)
3. It’s perfectly feasible for the connection to https://nlsn.cf to have succeeded, but the connection to https://api.github.com to fail. When this happens, you’re left trying to decide what to do; and location.replace("/") is a very bad solution, because you’ve now trashed the URL and I can’t even just try reloading the page to see if it works second time round. If you use HTTP redirects, everything will work perfectly in all cases, regardless of which connections succeed or fail.
4. Browsers have redirect loop detection, but you’ve opted out of that. In this thread someone pointed out simple loops and you’ve now tried to work around this by telling it “don’t redirect to the same domain”, but this really isn’t enough: you can easily have A → B → A. If this was done with HTTP redirection, the browser would twig and show an error and stop; but because half of the loop is done with location.replace, this won’t be caught. (Also, there are perfectly legitimate cases for a short URL redirecting to another short URL which redirects to a long URL, which this has now broken.)
----
A more amusing concept that occurs to me is implementing the URL shortener in a service worker, so that you can issue proper HTTP redirects, while doing it only on the client side.
Re: Show HN: I created a URL shortener that can be entirely hosted on GitHub Pages
#120You could also push static HTML pages to the git root which have, e.g., https://news.ycombinator.com/ '" /> in the header.