-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
static-image.server.tsx
137 lines (126 loc) · 3.76 KB
/
static-image.server.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React, { FunctionComponent } from "react"
import { GatsbyImage as GatsbyImageServer } from "./gatsby-image.server"
import { GatsbyImageProps, IGatsbyImageData } from "./gatsby-image.browser"
import PropTypes from "prop-types"
import { ImageFormat, Layout, Fit } from "../image-utils"
export interface IStaticImageProps extends Omit<GatsbyImageProps, "image"> {
src: string
layout?: Layout
formats?: Array<ImageFormat>
placeholder?: "tracedSVG" | "dominantColor" | "blurred" | "none"
tracedSVGOptions?: Record<string, unknown>
width?: number
height?: number
maxWidth?: number
maxHeight?: number
sizes?: string
quality?: number
transformOptions?: {
fit?: Fit
}
jpgOptions?: Record<string, unknown>
pngOptions?: Record<string, unknown>
webpOptions?: Record<string, unknown>
avifOptions?: Record<string, unknown>
blurredOptions?: Record<string, unknown>
}
// These values are added by Babel. Do not add them manually
interface IPrivateProps {
__imageData?: IGatsbyImageData
__error?: string
}
export function _getStaticImage(
GatsbyImage: FunctionComponent<GatsbyImageProps>
): React.FC<IStaticImageProps & IPrivateProps> {
return function StaticImage({
src,
__imageData: imageData,
__error,
// We extract these because they're not meant to be passed-down to GatsbyImage
/* eslint-disable @typescript-eslint/no-unused-vars */
width,
maxWidth,
height,
maxHeight,
tracedSVGOptions,
placeholder,
formats,
quality,
transformOptions,
jpgOptions,
pngOptions,
webpOptions,
blurredOptions,
/* eslint-enable @typescript-eslint/no-unused-vars */
...props
}): JSX.Element {
if (__error) {
console.warn(__error)
}
if (imageData) {
return <GatsbyImage image={imageData} {...props} />
}
console.warn(`Image not loaded`, src)
if (!__error && process.env.NODE_ENV === `development`) {
console.warn(
`Please ensure that "gatsby-plugin-image" is included in the plugins array in gatsby-config.js, and that your version of gatsby is at least 2.24.78`
)
}
return null
}
}
const StaticImage: React.FC<
IStaticImageProps & IPrivateProps
> = _getStaticImage(GatsbyImageServer)
const checkDimensionProps: PropTypes.Validator<number> = (
props: IStaticImageProps & IPrivateProps,
propName: keyof IStaticImageProps & IPrivateProps,
...rest
) => {
if (
props.layout !== `fixed` &&
(propName === `width` || propName === `height`) &&
props[propName]
) {
return new Error(
`"${propName}" ${props[propName]} may not be passed when layout is "${
props.layout || `constrained`
}"`
)
} else {
if (
props.layout === `fixed` &&
(propName === `maxWidth` || propName === `maxHeight`) &&
props[propName]
) {
return new Error(
`"${propName}" may not be passed when layout is "${props.layout}"`
)
}
}
return PropTypes.number(props, propName, ...rest)
}
const validLayouts = new Set([`fixed`, `fluid`, `constrained`])
export const propTypes = {
src: PropTypes.string.isRequired,
alt: PropTypes.string.isRequired,
width: checkDimensionProps,
height: checkDimensionProps,
maxHeight: checkDimensionProps,
maxWidth: checkDimensionProps,
sizes: PropTypes.string,
layout: (props: IStaticImageProps & IPrivateProps): Error | undefined => {
if (props.layout === undefined) {
return undefined
}
if (validLayouts.has(props.layout.toLowerCase())) {
return undefined
}
return new Error(
`Invalid value ${props.layout}" provided for prop "layout". Defaulting to "fixed". Valid values are "fixed", "fluid" or "constrained"`
)
},
}
StaticImage.displayName = `StaticImage`
StaticImage.propTypes = propTypes
export { StaticImage }