At #jamstackconf 2020, Netlfiy announced a preview of Edge Handlers which will allow you to run code directly on the edge server to handle things like custom authentication patterns, localization and personalization.
https://www.netlify.com/products/edge/edge-handlers/
https://www.youtube.com/watch?v=D44n8YVb5iI
I had previously written some utilities to encode information about a shared url (ala bit.ly) than when decoded would extract metadta about the share (and sharer), pass that information on to Segment for analytics tracking and then redirect to the shared target url.
It's not great, because the encoding uses JWT (I wanted something that could be signed so not tampered with), but that means the link is a little long ... but the issue was that because I was using Netlify redirects as well as Netlify functions to do a second redirect, the behavior was not ideal.
However. I think that by moving the logic to the Edge, we can eliminate one (or maybe both?) redirects and have a much cleaner experience.
There is an activity that has some location on the web.
When the React app fetches the activity, it enriches it via a Netlify function with is ShareLink getShareLink.js
/
It is somewhat short in that the path is just /a
.
This ingects some valuable tracking and analytics info like:
- Who shared it (iss: userId)
- The shared link to get tracked (url: canonicalUrl)
And campaign info about the share
utm_source: utmSource,
utm_medium: utmMedium,
utm_content: utmContent,
utm_campaign: utmCampaign,
We'll JWT encode this using a secret.
When the link is visited
const shareLink = `https://${env.SHARE_SUBDOMAIN}.${env.SHARE_HOST}/a?u=${encodedUrl}&ts=${ts}`;
The analyze.js
function running on some Netlify app at that location will be run having been redirected to
via a Netlify redirect:
# Functions
/a /.netlify/functions/analyze 200
That function then:
- decodes using the same JWT secret that was used to encode
- extractes the sahre analytics info
- sends that to Segment to track
- redirects to the shared url
Not out yet, but I thinik can move this to the edge and perhaps even further enrich the analytics more with some Netlify header info (geography, etc).
I don't yet know what edhe headers are provided by Netlify, altough the video about includes some geo-based headers for City/Region/Country.
Ideally, the redirect to the handler might get rid of a second redirect becasuse done all on Edge ... and be less clunky?
Yeah, no idea if this is GDPR-ok or whatever.
In fact, ths wasn't put into production due to two reasons:
- long urls caused some issues with some social platforms
- the 2 redirects also caused some issues with resolving the link
Hence the odd 307 HTTP status code.
( am looking at you LinkedIn ;) )