Build a URL Shortening service using Google Apps Script
Tags: github, javascript, googleappsscript, showdev
Published: 2019-10-13 09:31:51

> Originally posted on [script.gs](https://script.gs/url-shortener-powered-by-google-apps-script/)

It all started when I came across Jonathon Broughton's technique to use [Apps Script web apps within embeded ifames in Github Pages](https://github.com/stardotbmp/slack-gas-signup#github-pages), which got my gears turning for I was jonesing to make use of an [undocumented parameter](https://stackoverflow.com/questions/58172084/grab-web-app-link-info-besides-querystring-parameters/58172160#58172160) 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](https://github.com/schoraria911/gas-url-shortener).

![tmt-demo](https://raw.githubusercontent.com/schoraria911/gas-url-shortener/staging/imgs/tmt-demo.gif)

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](https://github.com/rafrex/spa-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 -
- The entire folder on my [Google Drive](https://drive.google.com/drive/folders/1SuHsBLbSYm5dSnp3NeGmUOTnC9NPqEje?usp=sharing)
- A Spreadsheet that acts as the [datasource](https://docs.google.com/spreadsheets/d/1xPs4Ht-3PVBGgdNSxyN3p2F5TtOifgvunrEvYaiu_A4/edit?usp=sharing)
- Finally, [the script](https://script.google.com/d/1wdNu632PfJNv0iCCNLjA-9nARemz7DLcK28Lio6YdVNMP3iEtOUtR4_R/edit?usp=sharing) itself

I'm still working my way through a whole bunch of [caveats](https://github.com/schoraria911/gas-url-shortener/blob/master/README.md#caveats) and from my [bucket list](https://github.com/schoraria911/gas-url-shortener/blob/master/README.md#bucket-list) but I'm more than happy to have [your contributions](https://github.com/schoraria911/gas-url-shortener/blob/master/README.md#license) considered too.

[Major learning](https://github.com/schoraria911/gas-url-shortener/blob/master/README.md#google-login) 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](https://www.producthunt.com/posts/take-me-there-2).