Skip to content

Commit

Permalink
feat: begin link shortener
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonappah committed Oct 18, 2021
1 parent 6ce9536 commit 5644f63
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
12 changes: 12 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
async rewrites() {
return {
fallback: [
{
source: "/:path*",
destination: `/api/link/?slug=:path*`
}
]
}
}
}
5 changes: 0 additions & 5 deletions pages/api/hello.js

This file was deleted.

25 changes: 25 additions & 0 deletions pages/api/link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// the idea with all of the weird cache stuff is we don't ever have to worry about hitting airtable's rate limits
// if i get a ton of traffic for some reason, while still being able to fetch new data from airtable every
// once in a while so i don't have to rebuild the site every time i want to update shortened links like i did previously.
// the other advantage of running redirects here instead of next.config.js is that i can add some basic/privacy-friendly
// logs of what links were accessed when and where which wasn't an option previously.
// also stale while revalidate is pretty pog, see https://vercel.com/docs/concepts/functions/edge-caching

// idk how to handle 404s yet but that shouldn't be too painful i hope - if i return a 404 does it
// serve the 404 page for me? then i can style that and make it all nice

export default async (req, res) => {
const ALLOWED_METHODS = ["GET", "HEAD"]
if (!ALLOWED_METHODS.includes(req.method)) {
return res.send("Only GET and HEAD methods are supported.")
}

if (req.cookies["_vercel_no_cache"] === 1 || req.query["?_vercel_no_cache"] === 1 || req.headers["Authorization"] || req.headers["Range"]) {
return res.send("sorry, we must get that cash!")
}

// TODO: Airtable stuff

res.setHeader("Cache-Control", "s-maxage=10, stale-while-revalidate")
return res.redirect(308, "https://jasonaa.me")
}

0 comments on commit 5644f63

Please sign in to comment.