diff --git a/README.md b/README.md index 99b4502..3a7b8a9 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,14 @@ Universal react application for the chapter website. ## Environment variables -| Name | Default | Description | -|--------------------|--------------------------------------|-----------------------------------------------------------------------------------------------------| -| TAITAN_URL | https://taitan.datasektionen.se | URL to get contents from taitan on | -| RAZZLE_TAITAN_URL | https://taitan.datasektionen.se | URL to get contents from taitan on. **Set during build**. Should probably be the same as TAITAN_URL | -| RAZZLE_CALYPSO_URL | https://calypso.datasektionen.se/api | URL to get news from calypso on. **Set during build** | -| PORT | 3000 | Port to listen on | +| Name | Default | Description | +|--------------------|--------------------------------------|-----------------------------------------------------------------------------------------------------------| +| TAITAN_URL | https://taitan.datasektionen.se | URL to get contents from taitan on | +| RAZZLE_TAITAN_URL | https://taitan.datasektionen.se | URL to get contents from taitan on. **Set during build**. Should probably be the same as TAITAN_URL | +| RAZZLE_CALYPSO_URL | https://calypso.datasektionen.se/api | URL to get news from calypso on. **Set during build** | +| TAITAN_CACHE_TTL | 3600 | Time to keep content from taitan cached in seconds. Tip: Set to 0 if using local taitan & bawang-content. | +| CALYPSO_CACHE_TTL | 30 | Time to keep news from calypso cached in seconds. Tip: Set to 0 if using local calypso. | +| PORT | 3000 | Port to listen on | ## Running Bawang runs on Node v.10.x.x. It doesn't work on later versions (v.12.20.1) diff --git a/src/components/Calypso.js b/src/components/Calypso.js index cdd2d59..f845481 100644 --- a/src/components/Calypso.js +++ b/src/components/Calypso.js @@ -4,6 +4,7 @@ import fetch from 'cross-fetch' import { DataLoader } from './DataLoader' const RAZZLE_CALYPSO_URL = process.env.RAZZLE_CALYPSO_URL || 'https://calypso.datasektionen.se/api' +const CALYPSO_CACHE_TTL = process.env.CALYPSO_CACHE_TTL ? parseInt(process.env.CALYPSO_CACHE_TTL, 10) : 30 const calypsoFetcher = url => fetch(url) @@ -16,7 +17,7 @@ export const Calypso = ({ type, search, children, ttl }) => {({ data, loading, time }) => children(data) } diff --git a/src/components/DataLoader.js b/src/components/DataLoader.js index d9ae711..bc36b07 100644 --- a/src/components/DataLoader.js +++ b/src/components/DataLoader.js @@ -32,17 +32,16 @@ export const DataLoader = withConsumer(class extends Component { const cacheKey = this.props.cacheKey - if(cache[cacheKey]) { - if(cache[cacheKey].loading) + if (cache[cacheKey]) { + if (cache[cacheKey].loading) { this.props.promises.push(new Promise((resolve, reject) => cache[cacheKey].waiting.push(resolve))) - else + } else { this.props.promises.push(Promise.resolve(cache[cacheKey])) + } return } - console.log('fetching', cacheKey) - cache[cacheKey] = { data: {}, cacheKey, @@ -50,8 +49,6 @@ export const DataLoader = withConsumer(class extends Component { waiting: [] } - console.log('Object.keys(cache).length:', Object.keys(cache).length) - this.props.promises.push( this.props .fetcher(cacheKey) @@ -68,7 +65,7 @@ export const DataLoader = withConsumer(class extends Component { setTimeout(() => { delete cache[cacheKey] - }, (this.props.ttl || 60) * 1000) + }, (typeof this.props.ttl === "number" ? this.props.ttl : 60) * 1000) this.forceUpdate() diff --git a/src/components/Taitan.js b/src/components/Taitan.js index 603c649..96ec861 100644 --- a/src/components/Taitan.js +++ b/src/components/Taitan.js @@ -7,6 +7,7 @@ import fetch from 'cross-fetch' import { DataLoader } from './DataLoader' const RAZZLE_TAITAN_URL = process.env.RAZZLE_TAITAN_URL || 'https://taitan.datasektionen.se' +const TAITAN_CACHE_TTL = process.env.TAITAN_CACHE_TTL ? parseInt(process.env.TAITAN_CACHE_TTL, 10) : 60 * 60 const taitanFetcher = url => fetch(url) @@ -26,11 +27,11 @@ const taitanFetcher = url => }) .then(res => ({ status: 200, redirect: false, ...res })) -export const Taitan = ({ pathname, children, ttl }) => +export const Taitan = ({ pathname, children }) => {({ data, loading, error }) => { if (!loading && data && data.redirect)