Skip to content

Commit

Permalink
Fix cache sharing between frontend and backend
Browse files Browse the repository at this point in the history
  • Loading branch information
foodelevator committed May 24, 2024
1 parent 412563e commit eeec4aa
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/components/Taitan.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import { HTTPError } from '../HTTPError'
import fetch from 'cross-fetch'

import { DataLoader } from './DataLoader'
import { TAITAN_URL } from '../utility/env'
import { TAITAN_URL, FRONTEND_TAITAN_URL } from '../utility/env'

const TAITAN_CACHE_TTL = process.env.TAITAN_CACHE_TTL ? parseInt(process.env.TAITAN_CACHE_TTL, 10) : 60 * 60

const taitanFetcher = url =>
fetch(url)
/**
* @param {string} cacheKey
*/
function taitanFetcher(cacheKey) {
// We're using the frontend url as the cache key when running on the backend as well, even though
// we're not fetching using it, since the cache created by the backend is sent to the frontend.
const url = cacheKey.startsWith(FRONTEND_TAITAN_URL)
? cacheKey.replace(FRONTEND_TAITAN_URL, TAITAN_URL)
: cacheKey;
return fetch(url)
.then(res => {
const redirected = res.url !== url // node-fetch doesnt have the res.redirected property
if (redirected) {
Expand All @@ -26,10 +34,11 @@ const taitanFetcher = url =>
}
})
.then(res => ({ status: 200, redirect: false, ...res }));
};

export const Taitan = ({ pathname, children }) =>
<DataLoader
cacheKey={TAITAN_URL + pathname}
cacheKey={FRONTEND_TAITAN_URL + pathname}
fetcher={taitanFetcher}
ttl={TAITAN_CACHE_TTL}
>
Expand Down

0 comments on commit eeec4aa

Please sign in to comment.