Skip to content

Commit

Permalink
add ENABLE_CACHE
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Dec 5, 2018
1 parent fcfd642 commit b9d15f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
12 changes: 9 additions & 3 deletions README.md
Expand Up @@ -27,11 +27,13 @@ AIRTABLE_BASE -> *****************
AIRTABLE_TABLE -> URLs
AIRTABLE_SHORT_CODE_FIELD -> Short Code
AIRTABLE_LONG_LINK_FIELD -> Long Link
ENABLE_CACHE -> false
```

> Note: `AIRTABLE_TABLE`, `AIRTABLE_SHORT_CODE_FIELD`, and
> `AIRTABLE_LONG_LINK_FIELD` are showing the default values above. If that's
> what you call your table and fields then you don't need to set those variables
> Note: `AIRTABLE_TABLE`, `AIRTABLE_SHORT_CODE_FIELD`,
> `AIRTABLE_LONG_LINK_FIELD`, and `ENABLE_CACHE` are showing the default values
> above. If that's what you call your table and fields then you don't need to
> set those variables.
> Note also that you can use a `.env` file instead, just don't commit this to
> source control :) (this is useful for local development as `.env` is in the
Expand All @@ -51,3 +53,7 @@ then verify that the redirect works with the custom domain.

Now, go get CloudFlare setup with your custom domain to prevent your function
from being called more than airtable's rate limiting can handle.

If you're not using CloudFlare, then set `ENABLE_CACHE` to `true` so you can get
some caching from Netlify. That always seemed to not work very well for me
though (which is one reason I use CloudFlare instead) so good luck.
19 changes: 12 additions & 7 deletions functions/redirect.js
Expand Up @@ -55,19 +55,24 @@ exports.handler = async event => {
console.log(`> redirecting to ${longLink}`)
const title = `${host}/${code || ''}`
const body = `<html><head><title>${title}</title></head><body><a href="${longLink}">moved here</a></body>`
const cacheHeaders = JSON.parse(getEnv('ENABLE_CACHE', 'false'))
? {
// entirely disable the cache for now until I can find a way
// for the cache to take the query paremeter into consideration
'Cache-Control': 'public, max-age=10080', // 10080 seconds is 1 week
// this is set by the redirect logic because the query string
// is not taken into account by the caching mechanism for some reason
Vary: 'X-Short-Code',
'X-Short-Code': code, // may as well return it just in case I guess?
}
: null

return {
statusCode,
body,
headers: {
Location: longLink,
// entirely disable the cache for now until I can find a way
// for the cache to take the query paremeter into consideration
'Cache-Control': 'public, max-age=10080', // 10080 seconds is 1 week
// this is set by the redirect logic because the query string
// is not taken into account by the caching mechanism for some reason
Vary: 'X-Short-Code',
'X-Short-Code': code, // may as well return it just in case I guess?
...cacheHeaders,
// these headers I got by curling a bit.ly URL
// and just doing what they do.
'Content-Length': String(body.length),
Expand Down

0 comments on commit b9d15f1

Please sign in to comment.