mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-03-23 07:40:46 +08:00
fix: image proxy url (#14505)
#### PR Dependency Tree * **PR #14505** 👈 This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Improvements** * Better image-proxy detection to avoid double-proxying already proxied images. * Improved runtime image proxy configuration so images load consistently across deployments. * More robust image URL handling for optimized image loading and fewer redundant requests. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -21,12 +21,24 @@ const imageProxyMiddlewareBuilder = () => {
|
||||
};
|
||||
};
|
||||
|
||||
const IMAGE_PROXY_PATH = '/api/worker/image-proxy';
|
||||
|
||||
export const isImageProxyURL = (imageUrl: string) => {
|
||||
try {
|
||||
const url = new URL(imageUrl, globalThis.location.origin);
|
||||
return url.pathname === IMAGE_PROXY_PATH && url.searchParams.has('url');
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const defaultImageProxyMiddlewarBuilder = imageProxyMiddlewareBuilder();
|
||||
|
||||
export const setImageProxyMiddlewareURL = defaultImageProxyMiddlewarBuilder.set;
|
||||
|
||||
export const defaultImageProxyMiddleware =
|
||||
defaultImageProxyMiddlewarBuilder.get();
|
||||
export const defaultImageProxyMiddleware: TransformerMiddleware = args => {
|
||||
return defaultImageProxyMiddlewarBuilder.get()(args);
|
||||
};
|
||||
|
||||
// TODO(@mirone): this should be configured when setup instead of runtime
|
||||
export class ImageProxyService extends StoreExtension {
|
||||
@@ -40,7 +52,7 @@ export class ImageProxyService extends StoreExtension {
|
||||
}
|
||||
|
||||
buildUrl(imageUrl: string) {
|
||||
if (imageUrl.startsWith(this.imageProxyURL)) {
|
||||
if (imageUrl.startsWith(this.imageProxyURL) || isImageProxyURL(imageUrl)) {
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user