Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-image): Use bare GATSBY___IMAGE global #30713

Merged
merged 5 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ import { GatsbyImage, IGatsbyImageData } from "../gatsby-image.browser"
import { render, waitFor } from "@testing-library/react"
import * as hooks from "../hooks"

type GlobalOverride = NodeJS.Global &
typeof global.globalThis & {
// eslint-disable-next-line @typescript-eslint/naming-convention
GATSBY___IMAGE: boolean
SERVER: boolean
}

// Prevents terser for bailing because we're not in a babel plugin
jest.mock(`../../../macros/terser.macro`, () => (strs): string => strs.join(``))

Expand All @@ -19,8 +12,9 @@ describe(`GatsbyImage browser`, () => {

beforeEach(() => {
console.warn = jest.fn()
;(global as GlobalOverride).SERVER = true
LekoArts marked this conversation as resolved.
Show resolved Hide resolved
;(global as GlobalOverride).GATSBY___IMAGE = true
console.error = jest.fn()
global.SERVER = true
global.GATSBY___IMAGE = true
})

beforeEach(() => {
Expand Down Expand Up @@ -73,20 +67,20 @@ describe(`GatsbyImage browser`, () => {

afterEach(() => {
jest.clearAllMocks()
;(global as GlobalOverride).SERVER = undefined
;(global as GlobalOverride).GATSBY___IMAGE = undefined
global.SERVER = undefined
global.GATSBY___IMAGE = undefined
})

it(`shows a suggestion to switch to the new gatsby-image API when available`, async () => {
;(global as GlobalOverride).GATSBY___IMAGE = false
global.GATSBY___IMAGE = undefined

const { container } = render(
<GatsbyImage image={image} alt="Alt content" />
)

await waitFor(() => container.querySelector(`[data-placeholder-image=""]`))

expect(console.warn).toBeCalledWith(
expect(console.error).toBeCalledWith(
`[gatsby-plugin-image] You're missing out on some cool performance features. Please add "gatsby-plugin-image" to your gatsby-config.js`
)
})
Expand Down Expand Up @@ -164,7 +158,7 @@ describe(`GatsbyImage browser`, () => {
container.querySelector(`[data-main-image=""]`)
)

img.dispatchEvent(new Event(`load`))
img?.dispatchEvent(new Event(`load`))

expect(onStartLoadSpy).toBeCalledWith({ wasCached: false })
expect(onLoadSpy).toBeCalled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,20 @@ import { GatsbyImage } from "../gatsby-image.server"
import { IGatsbyImageData } from "../gatsby-image.browser"
import { SourceProps } from "../picture"

type GlobalOverride = NodeJS.Global &
typeof global.globalThis & {
SERVER: boolean | undefined
// eslint-disable-next-line @typescript-eslint/naming-convention
GATSBY___IMAGE: boolean | undefined
}

// Prevents terser for bailing because we're not in a babel plugin
jest.mock(`../../../macros/terser.macro`, () => (strs): string => strs.join(``))

describe(`GatsbyImage server`, () => {
beforeEach(() => {
console.warn = jest.fn()
;(global as GlobalOverride).SERVER = true
;(global as GlobalOverride).GATSBY___IMAGE = true
global.SERVER = true
global.GATSBY___IMAGE = true
})

afterEach(() => {
jest.clearAllMocks()
;(global as GlobalOverride).SERVER = false
;(global as GlobalOverride).GATSBY___IMAGE = false
global.SERVER = false
global.GATSBY___IMAGE = false
})

it(`shows nothing when the image props is not passed`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
hasNativeLazyLoadSupport,
storeImageloaded,
hasImageLoaded,
gatsbyImageIsInstalled,
} from "./hooks"
import { PlaceholderProps } from "./placeholder"
import { MainImageProps } from "./main-image"
Expand Down Expand Up @@ -182,7 +183,11 @@ class GatsbyImageHydrator extends Component<
const cacheKey = JSON.stringify(this.props.image.images)

// when SSR and native lazyload is supported we'll do nothing ;)
if (hasNativeLazyLoadSupport() && ssrElement && global.GATSBY___IMAGE) {
if (
hasNativeLazyLoadSupport() &&
ssrElement &&
gatsbyImageIsInstalled()
) {
this.props.onStartLoad?.({ wasCached: false })

// When the image is already loaded before we have hydrated, we trigger onLoad and cache the item
Expand Down Expand Up @@ -272,8 +277,8 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
return null
}

if (!global.GATSBY___IMAGE) {
console.warn(
if (!gatsbyImageIsInstalled()) {
console.error(
`[gatsby-plugin-image] You're missing out on some cool performance features. Please add "gatsby-plugin-image" to your gatsby-config.js`
)
}
Expand Down
12 changes: 8 additions & 4 deletions packages/gatsby-plugin-image/src/components/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const hasNativeLazyLoadSupport = (): boolean =>
typeof HTMLImageElement !== `undefined` &&
`loading` in HTMLImageElement.prototype

export function gatsbyImageIsInstalled(): boolean {
return typeof GATSBY___IMAGE !== `undefined` && GATSBY___IMAGE
}

export function storeImageloaded(cacheKey?: string): void {
if (cacheKey) {
imageCache.add(cacheKey)
Expand Down Expand Up @@ -86,7 +90,7 @@ export function getWrapperProps(
let className = `gatsby-image-wrapper`

// If the plugin isn't installed we need to apply the styles inline
if (!global.GATSBY___IMAGE) {
if (!gatsbyImageIsInstalled()) {
wrapperStyle.position = `relative`
wrapperStyle.overflow = `hidden`
}
Expand All @@ -95,7 +99,7 @@ export function getWrapperProps(
wrapperStyle.width = width
wrapperStyle.height = height
} else if (layout === `constrained`) {
if (!global.GATSBY___IMAGE) {
if (!gatsbyImageIsInstalled()) {
wrapperStyle.display = `inline-block`
}
className = `gatsby-image-wrapper gatsby-image-wrapper-constrained`
Expand Down Expand Up @@ -267,7 +271,7 @@ export function getMainProps(
}

// fallback when it's not configured in gatsby-config.
if (!global.GATSBY___IMAGE) {
if (!gatsbyImageIsInstalled()) {
style = {
height: `100%`,
left: 0,
Expand Down Expand Up @@ -347,7 +351,7 @@ export function getPlaceholderProps(
}

// fallback when it's not configured in gatsby-config.
if (!global.GATSBY___IMAGE) {
if (!gatsbyImageIsInstalled()) {
result.style = {
height: `100%`,
left: 0,
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-image/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export const onCreateWebpackConfig: GatsbyNode["onCreateWebpackConfig"] = ({
actions.setWebpackConfig({
plugins: [
plugins.define({
[`global.GATSBY___IMAGE`]: true,
// eslint-disable-next-line @typescript-eslint/naming-convention
GATSBY___IMAGE: true,
}),
],
})
Expand Down
9 changes: 2 additions & 7 deletions packages/gatsby-plugin-image/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
export {}

declare global {
declare var SERVER: boolean

namespace NodeJS {
interface Global {
GATSBY___IMAGE: boolean | undefined
}
}
declare var SERVER: boolean | undefined
declare var GATSBY___IMAGE: boolean | undefined
}