Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(next): unify asset base URL and namespace app static dirs (#18221)

Innei50548e7d3192
diff --git a/docs/self-hosting/environment-variables/basic.mdx b/docs/self-hosting/environment-variables/basic.mdx
index dba83761da4..24dcb26e2f4 100644
--- a/docs/self-hosting/environment-variables/basic.mdx
+++ b/docs/self-hosting/environment-variables/basic.mdx
@@ -210,10 +210,26 @@ SSRF_ALLOW_IP_ADDRESS_LIST=192.168.1.100,10.0.0.50
- Allow access to internal API gateway: `10.0.0.50`
- Allow access to internal documentation server: `172.16.0.10`
-### `NEXT_PUBLIC_ASSET_PREFIX`
+### `ASSET_BASE_URL`
- Type: Optional
-- Description: The path access prefix for static resources can be set to the URL for CDN access. For more details, please refer to: [assetPrefix](https://nextjs.org/docs/app/api-reference/config/next-config-js/assetPrefix)
+- Description: Base URL for build output (Next.js static assets and the SPA bundles). Set it to a CDN or object-storage origin to serve build assets from somewhere other than the app itself. It maps to Next.js [assetPrefix](https://nextjs.org/docs/app/api-reference/config/next-config-js/assetPrefix). Leave it unset to serve everything from the app origin.
+- Default: -
+- Example: `https://cdn.example.com`
+
+<Callout type={'warning'}>
+ When this points at a different origin than the app, that host **must** respond with an
+ `Access-Control-Allow-Origin` header covering your app's origin. Once an asset base URL is
+ configured, LobeHub emits `crossorigin` on the generated `<script>` and `<link>` tags — a host
+ without the header makes the browser refuse to execute those assets, leaving a blank page. It also
+ means script errors from those files reach error monitoring with a real stack instead of an opaque
+ `Script error.` frame.
+</Callout>
+
+### `NEXT_PUBLIC_ASSET_PREFIX`
+
+- Type: Optional, **deprecated** — use `ASSET_BASE_URL` instead
+- Description: Previous name for `ASSET_BASE_URL`. Still used as a fallback when `ASSET_BASE_URL` is unset, and carries the same cross-origin requirement described above.
- Default: -
- Example: `https://cdn.example.com`
diff --git a/docs/self-hosting/environment-variables/basic.zh-CN.mdx b/docs/self-hosting/environment-variables/basic.zh-CN.mdx
index 153bc1fa2c3..f78852f95e2 100644
--- a/docs/self-hosting/environment-variables/basic.zh-CN.mdx
+++ b/docs/self-hosting/environment-variables/basic.zh-CN.mdx
@@ -205,10 +205,24 @@ SSRF_ALLOW_IP_ADDRESS_LIST=192.168.1.100,10.0.0.50
- 允许访问内网 API 网关:`10.0.0.50`
- 允许访问内网文档服务器:`172.16.0.10`
-### `NEXT_PUBLIC_ASSET_PREFIX`
+### `ASSET_BASE_URL`
- 类型:可选
-- 描述:静态资源的路径访问前缀,你可以设置为 CDN 访问的 URL,具体可参考: [assetPrefix](https://nextjs.org/docs/app/api-reference/config/next-config-js/assetPrefix)
+- 描述:构建产物(Next.js 静态资源与 SPA 包)的基础 URL。设置为 CDN 或对象存储的域名,即可让构建产物不再由应用自身分发。它对应 Next.js 的 [assetPrefix](https://nextjs.org/docs/app/api-reference/config/next-config-js/assetPrefix)。不设置时,所有资源仍由应用同源提供。
+- 默认值:-
+- 示例:`https://cdn.example.com`
+
+<Callout type={'warning'}>
+ 当它指向与应用不同的域名时,该资源域名**必须**返回覆盖应用域名的 `Access-Control-Allow-Origin`
+ 响应头。一旦配置了资源基础 URL,LobeHub 会在生成的 `<script>` 与 `<link>` 标签上带出
+ `crossorigin`;缺少该响应头会让浏览器拒绝执行这些资源,页面直接白屏。它同时也让这些文件的脚本报错能带着真实堆栈进入错误监控,而不是一条无信息的
+ `Script error.`。
+</Callout>
+
+### `NEXT_PUBLIC_ASSET_PREFIX`
+
+- 类型:可选,**已废弃** —— 请改用 `ASSET_BASE_URL`
+- 描述:`ASSET_BASE_URL` 的旧名称。在未设置 `ASSET_BASE_URL` 时仍会作为回退生效,并且同样受上述跨域要求约束。
- 默认值:-
- 示例:`https://cdn.example.com`
diff --git a/packages/const/src/meta.ts b/packages/const/src/meta.ts
index da467328c12..d546c6a6111 100644
--- a/packages/const/src/meta.ts
+++ b/packages/const/src/meta.ts
@@ -9,4 +9,4 @@ export const DEFAULT_BACKGROUND_COLOR = undefined;
export const DEFAULT_AGENT_META: MetaData = {};
export const DEFAULT_INBOX_TITLE = 'Lobe AI';
export const DEFAULT_INBOX_AVATAR = BRANDING_LOGO_URL || '/avatars/lobe-ai.png';
-export const DEFAULT_USER_AVATAR_URL = BRANDING_LOGO_URL || '/icons/icon-192x192.png';
+export const DEFAULT_USER_AVATAR_URL = BRANDING_LOGO_URL || '/app-icons/icon-192x192.png';
diff --git a/packages/const/src/url.ts b/packages/const/src/url.ts
index 8a1a33bb135..d03a802f6d6 100644
--- a/packages/const/src/url.ts
+++ b/packages/const/src/url.ts
@@ -75,7 +75,7 @@ export const GROUP_CHAT_TOPIC_URL = (groupId: string, topicId: string) =>
export const LIBRARY_URL = (id: string) => urlJoin('/resource/library', id);
-export const imageUrl = (filename: string) => `/images/${filename}`;
+export const imageUrl = (filename: string) => `/app-images/${filename}`;
export const LOBE_URL_IMPORT_NAME = 'settings';
diff --git a/public/icons/icon-192x192.maskable.png b/public/app-icons/icon-192x192.maskable.png
similarity index 100%
rename from public/icons/icon-192x192.maskable.png
rename to public/app-icons/icon-192x192.maskable.png
diff --git a/public/icons/icon-192x192.png b/public/app-icons/icon-192x192.png
similarity index 100%
rename from public/icons/icon-192x192.png
rename to public/app-icons/icon-192x192.png
diff --git a/public/icons/icon-512x512.maskable.png b/public/app-icons/icon-512x512.maskable.png
similarity index 100%
rename from public/icons/icon-512x512.maskable.png
rename to public/app-icons/icon-512x512.maskable.png
diff --git a/public/icons/icon-512x512.png b/public/app-icons/icon-512x512.png
similarity index 100%
rename from public/icons/icon-512x512.png
rename to public/app-icons/icon-512x512.png
diff --git a/public/images/agent-artwork-styles/style-clay.jpg b/public/app-images/agent-artwork-styles/style-clay.jpg
similarity index 100%
rename from public/images/agent-artwork-styles/style-clay.jpg
rename to public/app-images/agent-artwork-styles/style-clay.jpg
diff --git a/public/images/agent-artwork-styles/style-geometric.jpg b/public/app-images/agent-artwork-styles/style-geometric.jpg
similarity index 100%
rename from public/images/agent-artwork-styles/style-geometric.jpg
rename to public/app-images/agent-artwork-styles/style-geometric.jpg
diff --git a/public/images/agent-artwork-styles/style-pixel.jpg b/public/app-images/agent-artwork-styles/style-pixel.jpg
similarity index 100%
rename from public/images/agent-artwork-styles/style-pixel.jpg
rename to public/app-images/agent-artwork-styles/style-pixel.jpg
diff --git a/public/images/agent-artwork-styles/style-sticker.jpg b/public/app-images/agent-artwork-styles/style-sticker.jpg
similarity index 100%
rename from public/images/agent-artwork-styles/style-sticker.jpg
rename to public/app-images/agent-artwork-styles/style-sticker.jpg
diff --git a/public/images/agent-artwork-styles/style-watercolor.jpg b/public/app-images/agent-artwork-styles/style-watercolor.jpg
similarity index 100%
rename from public/images/agent-artwork-styles/style-watercolor.jpg
rename to public/app-images/agent-artwork-styles/style-watercolor.jpg
diff --git a/public/images/agent_gateway_dark.webp b/public/app-images/agent_gateway_dark.webp
similarity index 100%
rename from public/images/agent_gateway_dark.webp
rename to public/app-images/agent_gateway_dark.webp
diff --git a/public/images/agent_gateway_light.webp b/public/app-images/agent_gateway_light.webp
similarity index 100%
rename from public/images/agent_gateway_light.webp
rename to public/app-images/agent_gateway_light.webp
diff --git a/public/images/banner_creator.png b/public/app-images/banner_creator.png
similarity index 100%
rename from public/images/banner_creator.png
rename to public/app-images/banner_creator.png
diff --git a/public/images/banner_market_modal.webp b/public/app-images/banner_market_modal.webp
similarity index 100%
rename from public/images/banner_market_modal.webp
rename to public/app-images/banner_market_modal.webp
diff --git a/public/images/chatmode_chat_dark.webp b/public/app-images/chatmode_chat_dark.webp
similarity index 100%
rename from public/images/chatmode_chat_dark.webp
rename to public/app-images/chatmode_chat_dark.webp
diff --git a/public/images/chatmode_chat_light.webp b/public/app-images/chatmode_chat_light.webp
similarity index 100%
rename from public/images/chatmode_chat_light.webp
rename to public/app-images/chatmode_chat_light.webp
diff --git a/public/images/chatmode_docs_dark.webp b/public/app-images/chatmode_docs_dark.webp
similarity index 100%
rename from public/images/chatmode_docs_dark.webp
rename to public/app-images/chatmode_docs_dark.webp
diff --git a/public/images/chatmode_docs_light.webp b/public/app-images/chatmode_docs_light.webp
similarity index 100%
rename from public/images/chatmode_docs_light.webp
rename to public/app-images/chatmode_docs_light.webp
diff --git a/public/images/community_footer_dark.webp b/public/app-images/community_footer_dark.webp
similarity index 100%
rename from public/images/community_footer_dark.webp
rename to public/app-images/community_footer_dark.webp
diff --git a/public/images/community_footer_light.webp b/public/app-images/community_footer_light.webp
similarity index 100%
rename from public/images/community_footer_light.webp
rename to public/app-images/community_footer_light.webp
diff --git a/public/images/community_header_dark.webp b/public/app-images/community_header_dark.webp
similarity index 100%
rename from public/images/community_header_dark.webp
rename to public/app-images/community_header_dark.webp
diff --git a/public/images/community_header_light.webp b/public/app-images/community_header_light.webp
similarity index 100%
rename from public/images/community_header_light.webp
rename to public/app-images/community_header_light.webp
diff --git a/public/images/empty_topic_dark.webp b/public/app-images/empty_topic_dark.webp
similarity index 100%
rename from public/images/empty_topic_dark.webp
rename to public/app-images/empty_topic_dark.webp
diff --git a/public/images/empty_topic_light.webp b/public/app-images/empty_topic_light.webp
similarity index 100%
rename from public/images/empty_topic_light.webp
rename to public/app-images/empty_topic_light.webp
diff --git a/public/images/mode_lite_dark.webp b/public/app-images/mode_lite_dark.webp
similarity index 100%
rename from public/images/mode_lite_dark.webp
rename to public/app-images/mode_lite_dark.webp
diff --git a/public/images/mode_lite_light.webp b/public/app-images/mode_lite_light.webp
similarity index 100%
rename from public/images/mode_lite_light.webp
rename to public/app-images/mode_lite_light.webp
diff --git a/public/images/mode_pro_dark.webp b/public/app-images/mode_pro_dark.webp
similarity index 100%
rename from public/images/mode_pro_dark.webp
rename to public/app-images/mode_pro_dark.webp
diff --git a/public/images/mode_pro_light.webp b/public/app-images/mode_pro_light.webp
similarity index 100%
rename from public/images/mode_pro_light.webp
rename to public/app-images/mode_pro_light.webp
diff --git a/public/images/screenshot_background.webp b/public/app-images/screenshot_background.webp
similarity index 100%
rename from public/images/screenshot_background.webp
rename to public/app-images/screenshot_background.webp
diff --git a/public/images/theme_auto.webp b/public/app-images/theme_auto.webp
similarity index 100%
rename from public/images/theme_auto.webp
rename to public/app-images/theme_auto.webp
diff --git a/public/images/theme_dark.webp b/public/app-images/theme_dark.webp
similarity index 100%
rename from public/images/theme_dark.webp
rename to public/app-images/theme_dark.webp
diff --git a/public/images/theme_light.webp b/public/app-images/theme_light.webp
similarity index 100%
rename from public/images/theme_light.webp
rename to public/app-images/theme_light.webp
diff --git a/src/app/manifest.ts b/src/app/manifest.ts
index caf3465d78a..b5bd064a978 100644
--- a/src/app/manifest.ts
+++ b/src/app/manifest.ts
@@ -10,7 +10,7 @@ const manifest = async (): Promise<MetadataRoute.Manifest> => {
icons: [
{
sizes: '192x192',
- src: '/icons/icon-192x192.png',
+ src: '/app-icons/icon-192x192.png',
type: 'image/png',
},
],
@@ -35,22 +35,22 @@ const manifest = async (): Promise<MetadataRoute.Manifest> => {
{
purpose: 'any',
sizes: '192x192',
- url: '/icons/icon-192x192.png',
+ url: '/app-icons/icon-192x192.png',
},
{
purpose: 'maskable',
sizes: '192x192',
- url: '/icons/icon-192x192.maskable.png',
+ url: '/app-icons/icon-192x192.maskable.png',
},
{
purpose: 'any',
sizes: '512x512',
- url: '/icons/icon-512x512.png',
+ url: '/app-icons/icon-512x512.png',
},
{
purpose: 'maskable',
sizes: '512x512',
- url: '/icons/icon-512x512.maskable.png',
+ url: '/app-icons/icon-512x512.maskable.png',
},
],
id: kebabCase(BRANDING_NAME),
diff --git a/src/features/ChatInput/ActionBar/Plus/index.tsx b/src/features/ChatInput/ActionBar/Plus/index.tsx
index 0329e8afbad..48a2eafe655 100644
--- a/src/features/ChatInput/ActionBar/Plus/index.tsx
+++ b/src/features/ChatInput/ActionBar/Plus/index.tsx
@@ -457,7 +457,9 @@ const usePlusMenuItems = ({ close }: { close: () => void }): ActionDropdownMenuI
<img
alt=""
className="cover"
- src={isDark ? '/images/agent_gateway_dark.webp' : '/images/agent_gateway_light.webp'}
+ src={
+ isDark ? '/app-images/agent_gateway_dark.webp' : '/app-images/agent_gateway_light.webp'
+ }
/>
<div className="body">
<div className="title">{t('gatewayMode.cardTitle')}</div>
diff --git a/src/layout/AuthProvider/MarketAuth/MarketAuthConfirmModal.tsx b/src/layout/AuthProvider/MarketAuth/MarketAuthConfirmModal.tsx
index 26ea5343bf3..469e4f199c6 100644
--- a/src/layout/AuthProvider/MarketAuth/MarketAuthConfirmModal.tsx
+++ b/src/layout/AuthProvider/MarketAuth/MarketAuthConfirmModal.tsx
@@ -17,14 +17,14 @@ const styles = createStaticStyles(({ css }) => ({
container: css`
padding-block-start: 32px;
- background-image: url('/images/community_header_light.webp');
+ background-image: url('/app-images/community_header_light.webp');
background-repeat: no-repeat;
background-position: 400% 0;
background-size: 400px auto;
background-blend-mode: multiply;
`,
container_dark: css`
- background-image: url('/images/community_header_dark.webp');
+ background-image: url('/app-images/community_header_dark.webp');
background-blend-mode: screen;
`,
}));
diff --git a/src/libs/next/config/define-config.test.ts b/src/libs/next/config/define-config.test.ts
index 8e58164f24d..8f4f2ca9f03 100644
--- a/src/libs/next/config/define-config.test.ts
+++ b/src/libs/next/config/define-config.test.ts
@@ -1,9 +1,80 @@
-import { describe, expect, it } from 'vitest';
+import { afterEach, describe, expect, it } from 'vitest';
import { defineConfig } from './define-config';
describe('defineConfig', () => {
+ const originalAssetBaseUrl = process.env.ASSET_BASE_URL;
+ const originalLegacyPrefix = process.env.NEXT_PUBLIC_ASSET_PREFIX;
+
+ afterEach(() => {
+ if (originalAssetBaseUrl === undefined) delete process.env.ASSET_BASE_URL;
+ else process.env.ASSET_BASE_URL = originalAssetBaseUrl;
+
+ if (originalLegacyPrefix === undefined) delete process.env.NEXT_PUBLIC_ASSET_PREFIX;
+ else process.env.NEXT_PUBLIC_ASSET_PREFIX = originalLegacyPrefix;
+ });
+
it('disables Next.js agent rule injection', () => {
expect(defineConfig({}).agentRules).toBe(false);
});
+
+ describe('crossOrigin', () => {
+ it('stays unset when no asset prefix is configured', () => {
+ delete process.env.ASSET_BASE_URL;
+ delete process.env.NEXT_PUBLIC_ASSET_PREFIX;
+
+ expect(defineConfig({}).crossOrigin).toBeUndefined();
+ });
+
+ it('is anonymous when ASSET_BASE_URL is set', () => {
+ process.env.ASSET_BASE_URL = 'https://assets.example.com';
+ delete process.env.NEXT_PUBLIC_ASSET_PREFIX;
+
+ expect(defineConfig({}).crossOrigin).toBe('anonymous');
+ });
+
+ it('is anonymous when only the deprecated NEXT_PUBLIC_ASSET_PREFIX is set', () => {
+ delete process.env.ASSET_BASE_URL;
+ process.env.NEXT_PUBLIC_ASSET_PREFIX = 'https://legacy.example.com';
+
+ expect(defineConfig({}).crossOrigin).toBe('anonymous');
+ });
+ });
+
+ describe('assetPrefix', () => {
+ it('is undefined when no env is set', () => {
+ delete process.env.ASSET_BASE_URL;
+ delete process.env.NEXT_PUBLIC_ASSET_PREFIX;
+
+ expect(defineConfig({}).assetPrefix).toBeUndefined();
+ });
+
+ it('derives from ASSET_BASE_URL, stripping a trailing slash', () => {
+ process.env.ASSET_BASE_URL = 'https://assets.example.com/';
+ delete process.env.NEXT_PUBLIC_ASSET_PREFIX;
+
+ expect(defineConfig({}).assetPrefix).toBe('https://assets.example.com');
+ });
+
+ it('falls back to the deprecated NEXT_PUBLIC_ASSET_PREFIX', () => {
+ delete process.env.ASSET_BASE_URL;
+ process.env.NEXT_PUBLIC_ASSET_PREFIX = 'https://legacy.example.com';
+
+ expect(defineConfig({}).assetPrefix).toBe('https://legacy.example.com');
+ });
+
+ it('strips a trailing slash from the deprecated NEXT_PUBLIC_ASSET_PREFIX too', () => {
+ delete process.env.ASSET_BASE_URL;
+ process.env.NEXT_PUBLIC_ASSET_PREFIX = 'https://legacy.example.com/';
+
+ expect(defineConfig({}).assetPrefix).toBe('https://legacy.example.com');
+ });
+
+ it('prefers ASSET_BASE_URL over the deprecated key', () => {
+ process.env.ASSET_BASE_URL = 'https://assets.example.com';
+ process.env.NEXT_PUBLIC_ASSET_PREFIX = 'https://legacy.example.com';
+
+ expect(defineConfig({}).assetPrefix).toBe('https://assets.example.com');
+ });
+ });
});
diff --git a/src/libs/next/config/define-config.ts b/src/libs/next/config/define-config.ts
index d4281db5458..97ea08116fe 100644
--- a/src/libs/next/config/define-config.ts
+++ b/src/libs/next/config/define-config.ts
@@ -49,13 +49,19 @@ export function defineConfig(config: CustomNextConfig) {
},
};
- const assetPrefix = process.env.NEXT_PUBLIC_ASSET_PREFIX;
+ const assetPrefix = (process.env.ASSET_BASE_URL || process.env.NEXT_PUBLIC_ASSET_PREFIX)?.replace(
+ /\/+$/,
+ '',
+ );
const nextConfig: NextConfig = {
...(isStandaloneMode ? standaloneConfig : {}),
// Stop `next dev` from auto-injecting the nextjs-agent-rules block into AGENTS.md.
agentRules: false,
assetPrefix,
+ // Gated, not unconditional: an asset host that omits Access-Control-Allow-Origin
+ // turns every tag into one the browser refuses to execute. Same-origin needs no opt-in.
+ crossOrigin: assetPrefix ? 'anonymous' : undefined,
compiler: {
emotion: true,
@@ -111,7 +117,7 @@ export function defineConfig(config: CustomNextConfig) {
value: 'public, max-age=31536000, immutable',
},
],
- source: '/icons/(.*).(png|jpe?g|gif|svg|ico|webp)',
+ source: '/app-icons/(.*).(png|jpe?g|gif|svg|ico|webp)',
},
{
headers: [
@@ -128,7 +134,7 @@ export function defineConfig(config: CustomNextConfig) {
value: 'public, max-age=31536000, immutable',
},
],
- source: '/images/(.*).(png|jpe?g|gif|svg|ico|webp)',
+ source: '/app-images/(.*).(png|jpe?g|gif|svg|ico|webp)',
},
{
headers: [
diff --git a/src/routes/(main)/community/(list)/(home)/features/CreatorRewardBanner.tsx b/src/routes/(main)/community/(list)/(home)/features/CreatorRewardBanner.tsx
index 6a0b43f2fca..a303b5c8e34 100644
--- a/src/routes/(main)/community/(list)/(home)/features/CreatorRewardBanner.tsx
+++ b/src/routes/(main)/community/(list)/(home)/features/CreatorRewardBanner.tsx
@@ -57,7 +57,7 @@ const styles = createStaticStyles(({ css }) => ({
width: 50%;
border-radius: 0 12px 12px 0;
- background: url('/images/banner_creator.png') right center / auto 100% no-repeat;
+ background: url('/app-images/banner_creator.png') right center / auto 100% no-repeat;
${responsive.sm} {
display: none;
diff --git a/src/routes/(main)/community/(list)/_layout/Footer.tsx b/src/routes/(main)/community/(list)/_layout/Footer.tsx
index 4a4c40e9642..d03a6f2e47f 100644
--- a/src/routes/(main)/community/(list)/_layout/Footer.tsx
+++ b/src/routes/(main)/community/(list)/_layout/Footer.tsx
@@ -18,11 +18,11 @@ const styles = createStaticStyles(({ css }) => ({
background-size: 512px auto;
`,
footer_dark: css`
- background-image: url('/images/community_footer_dark.webp');
+ background-image: url('/app-images/community_footer_dark.webp');
background-blend-mode: screen;
`,
footer_light: css`
- background-image: url('/images/community_footer_light.webp');
+ background-image: url('/app-images/community_footer_light.webp');
background-blend-mode: multiply;
`,
}));