Skip to content

Commit

Permalink
Force CMS updates to appear
Browse files Browse the repository at this point in the history
I've been unable to work out what's happening, but pages from the CMS haven't been updated. The revalidation request is made, but it uses the cached copy again rather than revalidating. I'm unsure if this is related to the recent changes to how content is read from the CMS, but it seems likely. If not, there might be a wider problem (as a lot of content uses this technique). To force through the changes, this commit removes the stale caching at the cost of a small performance/reliability regression.

Refs 201a9d4, https://prereview.slack.com/archives/CR6B5804W/p1734390769283349?thread_ts=1734382300.226219&cid=CR6B5804W
  • Loading branch information
thewilkybarkid committed Dec 18, 2024
1 parent 85fd6e3 commit 3a0730d
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 75 deletions.
7 changes: 2 additions & 5 deletions src/ghost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { Context, Data, Effect, flow, identity, Match, Schema } from 'effect'
import type * as F from 'fetch-fp-ts'
import * as E from 'fp-ts/lib/Either.js'
import * as R from 'fp-ts/lib/Reader.js'
import * as RTE from 'fp-ts/lib/ReaderTaskEither.js'
import type * as RTE from 'fp-ts/lib/ReaderTaskEither.js'
import { pipe } from 'fp-ts/lib/function.js'
import { StatusCodes } from 'http-status-codes'
import { URL } from 'url'
import { type SleepEnv, revalidateIfStale, useStaleCache } from './fetch.js'
import { type Html, rawHtml, sanitizeHtml } from './html.js'

export interface GhostApiEnv {
Expand All @@ -32,7 +31,7 @@ const GhostPageSchema = Schema.Struct({

export const getPage = (
id: string,
): RTE.ReaderTaskEither<GhostApiEnv & F.FetchEnv & SleepEnv, 'not-found' | 'unavailable', Html> =>
): RTE.ReaderTaskEither<GhostApiEnv & F.FetchEnv, 'not-found' | 'unavailable', Html> =>
pipe(
R.asks(
(env: GhostApiEnv & F.FetchEnv) => () =>
Expand All @@ -49,8 +48,6 @@ export const getPage = (
Effect.runPromise,
),
),
RTE.local(revalidateIfStale<F.FetchEnv & GhostApiEnv & SleepEnv>()),
RTE.local(useStaleCache()),
)

export class GhostApi extends Context.Tag('GhostApi')<GhostApi, { key: string }>() {}
Expand Down
5 changes: 2 additions & 3 deletions test/clubs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Status } from 'hyper-ts'
import * as _ from '../src/clubs.js'
import { clubsMatch } from '../src/routes.js'
import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('clubs', () => {
test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 })])('when the page can be loaded', async key => {
Expand All @@ -18,7 +17,7 @@ describe('clubs', () => {
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)

const actual = await _.clubs({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.clubs({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand All @@ -43,7 +42,7 @@ describe('clubs', () => {
response,
)

const actual = await _.clubs({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.clubs({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand Down
5 changes: 2 additions & 3 deletions test/code-of-conduct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Status } from 'hyper-ts'
import * as _ from '../src/code-of-conduct.js'
import { codeOfConductMatch } from '../src/routes.js'
import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('codeOfConduct', () => {
test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 })])('when the page can be loaded', async key => {
Expand All @@ -18,7 +17,7 @@ describe('codeOfConduct', () => {
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)

const actual = await _.codeOfConduct({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.codeOfConduct({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand All @@ -43,7 +42,7 @@ describe('codeOfConduct', () => {
response,
)

const actual = await _.codeOfConduct({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.codeOfConduct({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand Down
5 changes: 2 additions & 3 deletions test/edia-statement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Status } from 'hyper-ts'
import * as _ from '../src/edia-statement.js'
import { ediaStatementMatch } from '../src/routes.js'
import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('ediaStatement', () => {
test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 })])('when the page can be loaded', async key => {
Expand All @@ -18,7 +17,7 @@ describe('ediaStatement', () => {
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)

const actual = await _.ediaStatement({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.ediaStatement({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand All @@ -43,7 +42,7 @@ describe('ediaStatement', () => {
response,
)

const actual = await _.ediaStatement({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.ediaStatement({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand Down
5 changes: 2 additions & 3 deletions test/funding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Status } from 'hyper-ts'
import * as _ from '../src/funding.js'
import { fundingMatch } from '../src/routes.js'
import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('funding', () => {
test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 })])('when the page can be loaded', async key => {
Expand All @@ -18,7 +17,7 @@ describe('funding', () => {
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)

const actual = await _.funding({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.funding({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand All @@ -43,7 +42,7 @@ describe('funding', () => {
response,
)

const actual = await _.funding({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.funding({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand Down
40 changes: 0 additions & 40 deletions test/ghost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { test } from '@fast-check/jest'
import { describe, expect } from '@jest/globals'
import fetchMock from 'fetch-mock'
import * as E from 'fp-ts/lib/Either.js'
import * as T from 'fp-ts/lib/Task.js'
import { Status } from 'hyper-ts'
import * as _ from '../src/ghost.js'
import { rawHtml } from '../src/html.js'
import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('getPage', () => {
test.prop([
Expand All @@ -23,7 +21,6 @@ describe('getPage', () => {
{ body: { pages: [{ html: html.toString() }] } },
),
ghostApi: { key },
sleep: shouldNotBeCalled,
})()

expect(actual).toStrictEqual(E.right(html))
Expand All @@ -47,7 +44,6 @@ describe('getPage', () => {
},
),
ghostApi: { key },
sleep: shouldNotBeCalled,
})()

expect(actual).toStrictEqual(
Expand Down Expand Up @@ -77,7 +73,6 @@ describe('getPage', () => {
},
),
ghostApi: { key },
sleep: shouldNotBeCalled,
})()

expect(actual).toStrictEqual(
Expand Down Expand Up @@ -107,42 +102,11 @@ describe('getPage', () => {
},
),
ghostApi: { key },
sleep: shouldNotBeCalled,
})()

expect(actual).toStrictEqual(E.right(rawHtml('<a href="https://donorbox.org/prereview" class="button">Donate</a>')))
})

test.prop([
fc.string({ unit: fc.alphanumeric(), minLength: 1 }),
fc.string({ unit: fc.alphanumeric(), minLength: 1 }),
fc.sanitisedHtml(),
])("revalidates the page if it's stale", async (id, key, html) => {
const fetch = fetchMock
.sandbox()
.getOnce(
(requestUrl, { cache }) =>
requestUrl === `https://content.prereview.org/ghost/api/content/pages/${id}?key=${key}` &&
cache === 'force-cache',
{ body: { pages: [{ html: html.toString() }] }, headers: { 'X-Local-Cache-Status': 'stale' } },
)
.getOnce(
(requestUrl, { cache }) =>
requestUrl === `https://content.prereview.org/ghost/api/content/pages/${id}?key=${key}` &&
cache === 'no-cache',
{ throws: new Error('Network error') },
)

const actual = await _.getPage(id)({
fetch,
ghostApi: { key },
sleep: () => T.of(undefined),
})()

expect(actual).toStrictEqual(E.right(html))
expect(fetch.done()).toBeTruthy()
})

test.prop([
fc.string({ unit: fc.alphanumeric(), minLength: 1 }),
fc.string({ unit: fc.alphanumeric(), minLength: 1 }),
Expand All @@ -155,7 +119,6 @@ describe('getPage', () => {
const actual = await _.getPage(id)({
fetch,
ghostApi: { key },
sleep: shouldNotBeCalled,
})()

expect(actual).toStrictEqual(E.left('unavailable'))
Expand All @@ -173,7 +136,6 @@ describe('getPage', () => {
const actual = await _.getPage(id)({
fetch,
ghostApi: { key },
sleep: shouldNotBeCalled,
})()

expect(actual).toStrictEqual(E.left('not-found'))
Expand All @@ -191,7 +153,6 @@ describe('getPage', () => {
const actual = await _.getPage(id)({
fetch,
ghostApi: { key },
sleep: shouldNotBeCalled,
})()

expect(actual).toStrictEqual(E.left('unavailable'))
Expand All @@ -206,7 +167,6 @@ describe('getPage', () => {
const actual = await _.getPage(id)({
fetch: () => Promise.reject(error),
ghostApi: { key },
sleep: shouldNotBeCalled,
})()

expect(actual).toStrictEqual(E.left('unavailable'))
Expand Down
5 changes: 2 additions & 3 deletions test/how-to-use.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Status } from 'hyper-ts'
import * as _ from '../src/how-to-use.js'
import { howToUseMatch } from '../src/routes.js'
import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('howToUse', () => {
test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 })])('when the page can be loaded', async key => {
Expand All @@ -18,7 +17,7 @@ describe('howToUse', () => {
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)

const actual = await _.howToUse({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.howToUse({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand All @@ -43,7 +42,7 @@ describe('howToUse', () => {
response,
)

const actual = await _.howToUse({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.howToUse({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand Down
5 changes: 2 additions & 3 deletions test/live-reviews.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Status } from 'hyper-ts'
import * as _ from '../src/live-reviews.js'
import { liveReviewsMatch } from '../src/routes.js'
import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('liveReviews', () => {
test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 })])('when the page can be loaded', async key => {
Expand All @@ -18,7 +17,7 @@ describe('liveReviews', () => {
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)

const actual = await _.liveReviews({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.liveReviews({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand All @@ -43,7 +42,7 @@ describe('liveReviews', () => {
response,
)

const actual = await _.liveReviews({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.liveReviews({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand Down
5 changes: 2 additions & 3 deletions test/people.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Status } from 'hyper-ts'
import * as _ from '../src/people.js'
import { peopleMatch } from '../src/routes.js'
import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('people', () => {
test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 })])('when the page can be loaded', async key => {
Expand All @@ -18,7 +17,7 @@ describe('people', () => {
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)

const actual = await _.people({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.people({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand All @@ -43,7 +42,7 @@ describe('people', () => {
response,
)

const actual = await _.people({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.people({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand Down
5 changes: 2 additions & 3 deletions test/privacy-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Status } from 'hyper-ts'
import * as _ from '../src/privacy-policy.js'
import { privacyPolicyMatch } from '../src/routes.js'
import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('privacyPolicy', () => {
test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 })])('when the page can be loaded', async key => {
Expand All @@ -18,7 +17,7 @@ describe('privacyPolicy', () => {
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)

const actual = await _.privacyPolicy({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.privacyPolicy({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand All @@ -43,7 +42,7 @@ describe('privacyPolicy', () => {
response,
)

const actual = await _.privacyPolicy({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.privacyPolicy({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand Down
5 changes: 2 additions & 3 deletions test/resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Status } from 'hyper-ts'
import * as _ from '../src/resources.js'
import { resourcesMatch } from '../src/routes.js'
import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('resources', () => {
test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 })])('when the page can be loaded', async key => {
Expand All @@ -18,7 +17,7 @@ describe('resources', () => {
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)

const actual = await _.resources({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.resources({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand All @@ -43,7 +42,7 @@ describe('resources', () => {
response,
)

const actual = await _.resources({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.resources({ fetch, ghostApi: { key } })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand Down
Loading

0 comments on commit 3a0730d

Please sign in to comment.