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.

fix(share): keep share pages out of search results (#21250)

Arvin Xu2a80ce95b401
diff --git a/src/app/robots.tsx b/src/app/robots.tsx
index b0df32977be..dfc9ae017a2 100644
--- a/src/app/robots.tsx
+++ b/src/app/robots.tsx
@@ -23,7 +23,12 @@ const robots = (): MetadataRoute.Robots => {
userAgent: 'Twitterbot',
},
{
- allow: ['/'],
+ // Shared links stay out of search results through the `noindex` directive the share
+ // document and response header carry. A crawler can only read that directive while it may
+ // fetch the URL, so the three real share routes are allowed on top of the `/share/*`
+ // block. Everything else under `/share/` keeps the block: the SPA fallback answers those
+ // paths with the indexable app shell.
+ allow: ['/', '/share/t/', '/share/page/', '/share/artifact/'],
// `/a/*` is the Agent Share visitor page: link-visible, creator-owned
// content that must never be indexed. `/agent/*` is the creator's own
// agent pages (and still forwards legacy share links).
diff --git a/src/app/spa-share/[locale]/[[...path]]/route.ts b/src/app/spa-share/[locale]/[[...path]]/route.ts
index 935e98b5a84..6790b84ac4e 100644
--- a/src/app/spa-share/[locale]/[[...path]]/route.ts
+++ b/src/app/spa-share/[locale]/[[...path]]/route.ts
@@ -8,6 +8,7 @@ import { appEnv } from '@/envs/app';
import { fileEnv } from '@/envs/file';
import { pythonEnv } from '@/envs/python';
import { translation } from '@/libs/i18n/serverTranslation';
+import { withShareNoIndexHeader } from '@/libs/next/shareRoutes';
import { buildAnalyticsConfig, renderSpaHtml } from '@/libs/spaHtml';
import { type Locales, normalizeLocale } from '@/locales/resources';
import { getServerGlobalConfig } from '@/server/globalConfig';
@@ -82,5 +83,7 @@ export async function GET(
const template = await getTemplate();
- return renderSpaHtml(template, { seoMeta: await buildSeoMeta(locale), serverConfig: spaConfig });
+ return withShareNoIndexHeader(
+ renderSpaHtml(template, { seoMeta: await buildSeoMeta(locale), serverConfig: spaConfig }),
+ );
}
diff --git a/src/libs/next/shareRoutes.test.ts b/src/libs/next/shareRoutes.test.ts
index 45a295b7f7e..e9ee0288290 100644
--- a/src/libs/next/shareRoutes.test.ts
+++ b/src/libs/next/shareRoutes.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
-import { isShareSpaRoute } from './shareRoutes';
+import { isShareSpaRoute, SHARE_NO_INDEX_DIRECTIVE, withShareNoIndexHeader } from './shareRoutes';
describe('isShareSpaRoute', () => {
it.each([
@@ -27,3 +27,22 @@ describe('isShareSpaRoute', () => {
expect(isShareSpaRoute(pathname)).toBe(false);
});
});
+
+describe('withShareNoIndexHeader', () => {
+ it('keeps a rendered share document out of search results', async () => {
+ const response = withShareNoIndexHeader(
+ new Response('<html></html>', { headers: { 'content-type': 'text/html; charset=utf-8' } }),
+ );
+
+ expect(response.headers.get('X-Robots-Tag')).toBe('noindex, nofollow');
+ expect(response.headers.get('content-type')).toBe('text/html; charset=utf-8');
+ await expect(response.text()).resolves.toBe('<html></html>');
+ });
+
+ it('covers responses that render no head', () => {
+ const response = withShareNoIndexHeader(new Response('Not Found', { status: 404 }));
+
+ expect(response.headers.get('X-Robots-Tag')).toBe(SHARE_NO_INDEX_DIRECTIVE);
+ expect(response.status).toBe(404);
+ });
+});
diff --git a/src/libs/next/shareRoutes.ts b/src/libs/next/shareRoutes.ts
index 5d4fd91b23a..be5466bdb1c 100644
--- a/src/libs/next/shareRoutes.ts
+++ b/src/libs/next/shareRoutes.ts
@@ -12,3 +12,26 @@ export const isShareSpaRoute = (pathname: string): boolean => {
SHARE_TOPIC_ROUTE.test(path) || SHARE_PAGE_ROUTE.test(path) || SHARE_ARTIFACT_ROUTE.test(path)
);
};
+
+/**
+ * Shared links are meant to be passed around, not indexed.
+ *
+ * The share document already carries a `robots` meta tag (`spa-share`, per-topic OG in the
+ * Cloudflare worker build). The header states the same directive for every share response
+ * instead of only the ones that render a head.
+ *
+ * Neither signal counts while crawlers are blocked from fetching the URL, which is why the apex
+ * robots.txt allows these routes on top of its `/share/*` block.
+ */
+export const SHARE_NO_INDEX_DIRECTIVE = 'noindex, nofollow';
+
+export const withShareNoIndexHeader = (response: Response): Response => {
+ const headers = new Headers(response.headers);
+ headers.set('X-Robots-Tag', SHARE_NO_INDEX_DIRECTIVE);
+
+ return new Response(response.body, {
+ headers,
+ status: response.status,
+ statusText: response.statusText,
+ });
+};