-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-source-url): implement base64/LQIP image loading
- Loading branch information
1 parent
d2f1c32
commit 26cb571
Showing
7 changed files
with
111 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { pipe } from 'fp-ts/lib/function'; | ||
import * as TE from 'fp-ts/lib/TaskEither'; | ||
import { GatsbyCache } from 'gatsby'; | ||
import { withCache } from '../cache'; | ||
import { fetch } from '../utils'; | ||
|
||
export const buildBase64URL = (contentType: string, base64: string): string => | ||
`data:${contentType};base64,${base64}`; | ||
|
||
export const fetchImgixBase64Image = (cache: GatsbyCache) => ( | ||
url: string, | ||
): TE.TaskEither<Error, string> => | ||
withCache(`gatsby-plugin-imgix-base64-url-${url}`, cache, () => | ||
pipe( | ||
url, | ||
fetch, | ||
TE.chain((res) => | ||
pipe( | ||
TE.rightTask<Error, Buffer>(() => res.buffer()), | ||
TE.chain((buffer) => TE.right(buffer.toString('base64'))), | ||
TE.chain((base64) => | ||
TE.right( | ||
buildBase64URL(String(res.headers.get('content-type')), base64), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
); |
40 changes: 40 additions & 0 deletions
40
packages/gatsby-source-url/src/createImgixBase64FieldConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import * as T from 'fp-ts/lib/Task'; | ||
import * as TE from 'fp-ts/lib/TaskEither'; | ||
import { GatsbyCache } from 'gatsby'; | ||
import { GraphQLFieldConfig, GraphQLNonNull, GraphQLString } from 'graphql'; | ||
import { fetchImgixBase64Image } from './api/fetchBase64Image'; | ||
import { | ||
ImgixSourceDataResolver, | ||
taskEitherFromSourceDataResolver, | ||
} from './utils'; | ||
|
||
interface CreateImgixBase64UrlFieldConfigArgsWithResolver<TSource> { | ||
resolveUrl: ImgixSourceDataResolver<TSource, string>; | ||
cache: GatsbyCache; | ||
} | ||
interface CreateImgixBase64UrlFieldConfigArgs<TSource> { | ||
resolveUrl?: ImgixSourceDataResolver<TSource, string>; | ||
cache: GatsbyCache; | ||
} | ||
export function createImgixBase64FieldConfig<TSource, TContext = unknown>({ | ||
resolveUrl, | ||
cache, | ||
}: CreateImgixBase64UrlFieldConfigArgsWithResolver< | ||
TSource | ||
>): GraphQLFieldConfig<TSource, TContext> { | ||
return { | ||
type: new GraphQLNonNull(GraphQLString), | ||
resolve: (obj: TSource): Promise<string> => | ||
pipe( | ||
obj, | ||
taskEitherFromSourceDataResolver<TSource, string>(resolveUrl), | ||
TE.chain(fetchImgixBase64Image(cache)), | ||
TE.getOrElse( | ||
(e): T.Task<string> => { | ||
throw e; | ||
}, | ||
), | ||
)(), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { FluidObject } from 'gatsby-image'; | ||
type ImgixFluidObject = Omit<FluidObject, 'base64'> & | ||
Required<Pick<FluidObject, 'base64'>>; |