Skip to content

[BUG] Images are loaded incorrectly in production #113

@xiaohai-huang

Description

@xiaohai-huang

Description

Images load correctly during development, but when the React app is served from the Resources folder, incorrect images are displayed due to identical file names with different extensions.

Steps to Reproduce

  1. Import two images with identical file names but different file extensions from separate folders.
import PlaceholderAvatar from "src/assets/images/icons/default-avatar.png";
import PlayerAvatar from "src/assets/images/default-avatar.jpg";
  1. Build the react app. Below shows the files are all stored under /static/media/ folder.
    image

  2. ResourcesHelper.LoadResource is unable to load the correct image.

Potential Cause

Currently, the files in Resources folder are loaded without file extension.

protected virtual T Get<T>(ReactContext context, AssetReferenceType realType, object realValue) where T : class
{
switch (realType)
{
case AssetReferenceType.Resource:
return ResourcesHelper.LoadResource<T>(realValue as string);
case AssetReferenceType.Global:
if (context.Globals.TryGetValue(realValue as string, out var res)) return res as T;
else return default;
case AssetReferenceType.Object:
return realValue as T;
case AssetReferenceType.File:
case AssetReferenceType.Url:
case AssetReferenceType.None:
case AssetReferenceType.Procedural:
case AssetReferenceType.Data:
default:
return null;
}
}

public static T LoadResource<T>(string path, bool excludeExtension = true) where T : class
{
if (string.IsNullOrWhiteSpace(path)) return default(T);
if (!typeof(Object).IsAssignableFrom(typeof(T))) return default(T);
if (excludeExtension) path = GetResourcePathWithoutExtension(path);
return Resources.Load(path, typeof(T)) as T;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions