DEV Community

Cover image for Build a URL Shortening service using Google Apps Script
Sourabh Choraria
Sourabh Choraria

Posted on • Updated on

Build a URL Shortening service using Google Apps Script

Originally posted on script.gs

It all started when I came across Jonathon Broughton's technique to use Apps Script web apps within embeded ifames in Github Pages, which got my gears turning for I was jonesing to make use of an undocumented parameter within Apps Script's doGet function - pathInfo; which in the end, blew up on my face and I rolled back to using queryString instead.

You can access the working demo on (https://tmt.pw) and refer the entire codebase (with documented instructions) on my github repository.

tmt-demo

The architecture is rather not that of a conventional URL shortening and redirection service because my methodology does not throw a 301/302 permanent redirect status but instead, uses JavaScript's window.open to have a new URL loaded on the same browser window (using _top), based on the slug.

Midway through the development I get to know that Github Pages serves a 404 if it doesn't recognise the path or have a resource already allocated against it in the project repository and that's what led me to plug Rafael Pedicini's Single Page Apps for GitHub Pages - I doubt I'd have been able to host my service on Github Pages (for free), had it not been for this ingenious technique to deploy single page apps.

Access to the original resources can be found here -

I'm still working my way through a whole bunch of caveats and from my bucket list but I'm more than happy to have your contributions considered too.

Major learning from all of this was that in order to use e.pathInfo, we'd need to append the script URL and in doing so, end up having Apps Script prompt user to login using their Google accounts (not particularly to allow script permissions but just because - we don't know yet).

Overall, it has been a humbling experience for having to publish a tool of my own 😊 - also available on Product Hunt.

Top comments (0)