mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-06-04 02:56:42 +00:00
[fix] SSR of Image renders source
When rendered on the server, images now include the 'src' and will be downloaded by the browser rather than waiting for the client-side JS to initiate image loading. Fix #543
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
/* eslint-env jasmine, jest */
|
/* eslint-env jasmine, jest */
|
||||||
|
|
||||||
|
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
|
||||||
import Image from '../';
|
import Image from '../';
|
||||||
import ImageUriCache from '../ImageUriCache';
|
import ImageUriCache from '../ImageUriCache';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { mount, render } from 'enzyme';
|
import { mount, render } from 'enzyme';
|
||||||
|
|
||||||
|
const originalCanUseDOM = ExecutionEnvironment.canUseDOM;
|
||||||
const originalImage = window.Image;
|
const originalImage = window.Image;
|
||||||
|
|
||||||
describe('components/Image', () => {
|
describe('components/Image', () => {
|
||||||
@@ -113,6 +115,15 @@ describe('components/Image', () => {
|
|||||||
ImageUriCache.remove(uriTwo);
|
ImageUriCache.remove(uriTwo);
|
||||||
expect(component.render().find('img').attr('src')).toBe(uriTwo);
|
expect(component.render().find('img').attr('src')).toBe(uriTwo);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('is set immediately when rendered on the server', () => {
|
||||||
|
ExecutionEnvironment.canUseDOM = false;
|
||||||
|
const uri = 'https://google.com/favicon.ico';
|
||||||
|
const component = render(<Image source={{ uri }} />);
|
||||||
|
expect(component.find('img').attr('src')).toBe(uri);
|
||||||
|
expect(ImageUriCache.has(uri)).toBe(true);
|
||||||
|
ExecutionEnvironment.canUseDOM = originalCanUseDOM;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('prop "style"', () => {
|
describe('prop "style"', () => {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import applyNativeMethods from '../../modules/applyNativeMethods';
|
import applyNativeMethods from '../../modules/applyNativeMethods';
|
||||||
|
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
|
||||||
import createDOMElement from '../../modules/createDOMElement';
|
import createDOMElement from '../../modules/createDOMElement';
|
||||||
import ImageLoader from '../../modules/ImageLoader';
|
import ImageLoader from '../../modules/ImageLoader';
|
||||||
import ImageResizeMode from './ImageResizeMode';
|
import ImageResizeMode from './ImageResizeMode';
|
||||||
@@ -41,8 +42,8 @@ const ImageSourcePropType = oneOfType([
|
|||||||
string
|
string
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const getImageState = (uri, isPreviouslyLoaded) => {
|
const getImageState = (uri, shouldDisplaySource) => {
|
||||||
return isPreviouslyLoaded ? STATUS_LOADED : uri ? STATUS_PENDING : STATUS_IDLE;
|
return shouldDisplaySource ? STATUS_LOADED : uri ? STATUS_PENDING : STATUS_IDLE;
|
||||||
};
|
};
|
||||||
|
|
||||||
const resolveAssetDimensions = source => {
|
const resolveAssetDimensions = source => {
|
||||||
@@ -111,10 +112,10 @@ class Image extends Component {
|
|||||||
super(props, context);
|
super(props, context);
|
||||||
// If an image has been loaded before, render it immediately
|
// If an image has been loaded before, render it immediately
|
||||||
const uri = resolveAssetSource(props.source);
|
const uri = resolveAssetSource(props.source);
|
||||||
const isPreviouslyLoaded = ImageUriCache.has(uri);
|
const shouldDisplaySource = ImageUriCache.has(uri) || !canUseDOM;
|
||||||
this.state = { shouldDisplaySource: isPreviouslyLoaded };
|
this.state = { shouldDisplaySource };
|
||||||
this._imageState = getImageState(uri, isPreviouslyLoaded);
|
this._imageState = getImageState(uri, shouldDisplaySource);
|
||||||
isPreviouslyLoaded && ImageUriCache.add(uri);
|
shouldDisplaySource && ImageUriCache.add(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
|
|||||||
Reference in New Issue
Block a user