Skip to content

Commit

Permalink
πŸ› Prevent race condition between delete and update requests #1998 (#2002
Browse files Browse the repository at this point in the history
)

* πŸ› Prevent race condition between delete and update requests #1998

* πŸ› Add missing promise wrapper #1998
  • Loading branch information
fernandolucchesi authored Nov 29, 2023
1 parent 6fff13c commit 05db5c9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 77 deletions.
30 changes: 16 additions & 14 deletions search/IndexSanityContent/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@ export const indexEvents = (language: Language) => (docId: string) => {
const updateAlgolia = flow(indexName, E.map(flow(update, ap(indexSettings))))
const removeIndexFromAlgolia = flow(indexName, E.map(remove))

type RemoveAndMapType = (pages: Event[]) => EventIndex[]
const removeAndMap: RemoveAndMapType = (pages) => {
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
)
type RemoveAndMapType = (pages: Event[]) => Promise<EventIndex[]>
const removeAndMap: RemoveAndMapType = async (pages) => {
await Promise.all(
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
),
)
return pipe(pages.map(mapData))
}

return pipe(
getSanityClient(),
TE.fromEither,
TE.chainW(fetchData(language, docId)),
TE.map(removeAndMap),
TE.chainW((pages) => TE.fromTask(() => removeAndMap(pages))),
TE.chainW((data) => pipe(updateAlgolia(), E.ap(E.of(data)), TE.fromEither)),
TE.flatten,
T.map(E.fold(console.error, console.log)),
Expand Down
31 changes: 17 additions & 14 deletions search/IndexSanityContent/localNews/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@ export const indexLocalNews = (language: Language) => (docId: string) => {
const updateAlgolia = flow(indexName, E.map(flow(update, ap(indexSettings))))
const removeIndexFromAlgolia = flow(indexName, E.map(remove))

type RemoveAndMapType = (pages: LocalNewsArticle[]) => NewsIndex[]
const removeAndMap: RemoveAndMapType = (pages) => {
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
)
type RemoveAndMapType = (pages: LocalNewsArticle[]) => Promise<NewsIndex[]>
const removeAndMap: RemoveAndMapType = async (pages) => {
await Promise.all(
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
),
)

return pipe(pages.map(mapData), flatten)
}

return pipe(
getSanityClient(),
TE.fromEither,
TE.chainW(fetchData(language, docId)),
TE.map(removeAndMap),
TE.chainW((pages) => TE.fromTask(() => removeAndMap(pages))),
TE.chainW((data) => pipe(updateAlgolia(), E.ap(E.of(data)), TE.fromEither)),
TE.flatten,
T.map(E.fold(console.error, console.log)),
Expand Down
30 changes: 16 additions & 14 deletions search/IndexSanityContent/magazine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@ export const indexMagazine = (language: Language) => (docId: string) => {
const updateAlgolia = flow(indexName, E.map(flow(update, ap(indexSettings))))
const removeIndexFromAlgolia = flow(indexName, E.map(remove))

type RemoveAndMapType = (pages: MagazineArticle[]) => MagazineIndex[]
const removeAndMap: RemoveAndMapType = (pages) => {
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
)
type RemoveAndMapType = (pages: MagazineArticle[]) => Promise<MagazineIndex[]>
const removeAndMap: RemoveAndMapType = async (pages) => {
await Promise.all(
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
),
)
return pipe(pages.map(mapData), flatten)
}
return pipe(
getSanityClient(),
TE.fromEither,
TE.chainW(fetchData(language, docId)),
TE.map(removeAndMap),
TE.chainW((pages) => TE.fromTask(() => removeAndMap(pages))),
TE.chainW((data) => pipe(updateAlgolia(), E.ap(E.of(data)), TE.fromEither)),
TE.flatten,
T.map(E.fold(console.error, console.log)),
Expand Down
31 changes: 17 additions & 14 deletions search/IndexSanityContent/news/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@ export const indexNews = (language: Language) => (docId: string) => {
const updateAlgolia = flow(indexName, E.map(flow(update, ap(indexSettings))))
const removeIndexFromAlgolia = flow(indexName, E.map(remove))

type RemoveAndMapType = (pages: NewsArticle[]) => NewsIndex[]
const removeAndMap: RemoveAndMapType = (pages) => {
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
)
type RemoveAndMapType = (pages: NewsArticle[]) => Promise<NewsIndex[]>

const removeAndMap: RemoveAndMapType = async (pages) => {
await Promise.all(
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
),
)
return pipe(pages.map(mapData), flatten)
}

return pipe(
getSanityClient(),
TE.fromEither,
TE.chainW(fetchData(language, docId)),
TE.map(removeAndMap),
TE.chainW((pages) => TE.fromTask(() => removeAndMap(pages))),
TE.chainW((data) => pipe(updateAlgolia(), E.ap(E.of(data)), TE.fromEither)),
TE.flatten,
T.map(E.fold(console.error, console.log)),
Expand Down
32 changes: 18 additions & 14 deletions search/IndexSanityContent/topic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@ export const indexTopic = (language: Language) => (docId: string) => {
const updateAlgolia = flow(indexName, E.map(flow(update, ap(indexSettings))))
const removeIndexFromAlgolia = flow(indexName, E.map(remove))

type RemoveAndMapType = (pages: TopicPage[]) => TopicIndex[]
const removeAndMap: RemoveAndMapType = (pages) => {
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
)
type RemoveAndMapType = (pages: TopicPage[]) => Promise<TopicIndex[]>

const removeAndMap: RemoveAndMapType = async (pages) => {
await Promise.all(
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
),
)

return pipe(pages.map(mapData), flatten)
}

return pipe(
getSanityClient(),
TE.fromEither,
TE.chainW(fetchData(language, docId)),
TE.map(removeAndMap),
TE.chainW((pages) => TE.fromTask(() => removeAndMap(pages))),
TE.chainW((data) => pipe(updateAlgolia(), E.ap(E.of(data)), TE.fromEither)),
TE.flatten,
T.map(E.fold(console.error, console.log)),
Expand Down
8 changes: 1 addition & 7 deletions web/pages/magazine/index.global.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { GetServerSideProps } from 'next'
import { InstantSearchSSRProvider } from 'react-instantsearch'
import { getServerState } from 'react-instantsearch'
import { InstantSearchSSRProvider, getServerState } from 'react-instantsearch'
import type { AppProps } from 'next/app'
import { IntlProvider } from 'react-intl'
import Footer from '../../pageComponents/shared/Footer'
Expand Down Expand Up @@ -67,11 +66,6 @@ MagazineIndex.getLayout = (page: AppProps) => {
}

export const getServerSideProps: GetServerSideProps = async ({ req, preview = false, locale = 'en' }) => {
// For the time being, let's just give 404 for satellites
// We will also return 404 if the locale is not English.
// This is a hack and and we should improve this at some point
// See https://github.com/vercel/next.js/discussions/18485

if (locale !== 'en') {
return {
notFound: true,
Expand Down

0 comments on commit 05db5c9

Please sign in to comment.