[] = [];
let sharedTree;
@@ -298,7 +326,7 @@ export const renderShare = async (ctx: Context, next: Next) => {
share.teamId
);
- if (share && !ctx.userAgent.isBot) {
+ if (share && !isBot) {
await share.update(
{
lastAccessedAt: new Date(),
@@ -385,15 +413,9 @@ export const renderShare = async (ctx: Context, next: Next) => {
? ((await team.publicAvatarUrl()) ?? undefined)
: undefined;
- const content =
- document || collection
- ? await DocumentHelper.toHTML(document || collection!, {
- includeStyles: false,
- includeHead: false,
- includeTitle: true,
- signedUrls: Hour.seconds,
- })
- : undefined;
+ // Only bots read the server-rendered content, so skip the render for others.
+ const model = document || collection;
+ const content = model && isBot ? await renderShareContent(model) : undefined;
const canonicalUrl = share
? `${share.canonicalUrl}${
diff --git a/server/routes/index.test.ts b/server/routes/index.test.ts
index 29ac53ff2a..a08a7ddcf3 100644
--- a/server/routes/index.test.ts
+++ b/server/routes/index.test.ts
@@ -6,8 +6,10 @@ import {
TeamPreference,
} from "@shared/types";
import { parseSessionsCookie } from "@shared/utils/sessions";
+import { parser } from "@server/editor";
import env from "@server/env";
import {
+ buildAttachment,
buildShare,
buildDocument,
buildIntegration,
@@ -17,6 +19,11 @@ import { getTestServer } from "@server/test/support";
const server = getTestServer();
+const botHeaders = {
+ "User-Agent":
+ "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
+};
+
describe("/s/:id", () => {
it("should return standard title in html when loading unpublished share", async () => {
const share = await buildShare({
@@ -109,6 +116,60 @@ describe("/s/:id", () => {
expect(body).toContain(`${document.title}`);
});
+ it("should render document content with signed attachment urls", async () => {
+ const team = await buildTeam();
+ const attachment = await buildAttachment({ teamId: team.id });
+ const document = await buildDocument({
+ teamId: team.id,
+ text: `Shared content\n\n`,
+ });
+ const share = await buildShare({
+ documentId: document.id,
+ teamId: document.teamId,
+ });
+ const res = await server.get(`/s/${share.id}`, { headers: botHeaders });
+ const body = await res.text();
+ expect(res.status).toEqual(200);
+ expect(body).toContain("Shared content");
+ expect(body).toContain("
{
+ const document = await buildDocument({ text: "Shared content" });
+ const share = await buildShare({
+ documentId: document.id,
+ teamId: document.teamId,
+ });
+ const res = await server.get(`/s/${share.id}`, {
+ headers: {
+ "User-Agent":
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
+ },
+ });
+ const body = await res.text();
+ expect(res.status).toEqual(200);
+ expect(body).toContain(`${document.title}`);
+ expect(body).not.toContain(' {
+ const document = await buildDocument({ text: "First version" });
+ const share = await buildShare({
+ documentId: document.id,
+ teamId: document.teamId,
+ });
+ const first = await server.get(`/s/${share.id}`, { headers: botHeaders });
+ expect(await first.text()).toContain("First version");
+
+ document.content = parser.parse("Second version")?.toJSON();
+ await document.save();
+ const second = await server.get(`/s/${share.id}`, {
+ headers: botHeaders,
+ });
+ expect(await second.text()).toContain("Second version");
+ });
+
it("should return markdown when Accept header prefers text/markdown", async () => {
const document = await buildDocument();
const share = await buildShare({
diff --git a/server/utils/RedisPrefixHelper.ts b/server/utils/RedisPrefixHelper.ts
index 695d194ea2..8dca8edbe9 100644
--- a/server/utils/RedisPrefixHelper.ts
+++ b/server/utils/RedisPrefixHelper.ts
@@ -36,6 +36,18 @@ export class RedisPrefixHelper {
return `cd:${collectionId}`;
}
+ /**
+ * Gets key for caching the rendered HTML of a shared document or collection.
+ * The key changes when the model is updated.
+ *
+ * @param id The document or collection ID.
+ * @param updatedAt The time the model was last updated.
+ * @returns the cache key string.
+ */
+ public static getShareContentKey(id: string, updatedAt: Date) {
+ return `share:html:${id}:${updatedAt.getTime()}`;
+ }
+
/**
* Gets key for caching embed check results. This is a global cache key
* (not team-specific) since embed headers are the same for all users.
diff --git a/server/utils/redirectOnClient.ts b/server/utils/redirectOnClient.ts
index 6b4cd050a4..3977f1e491 100644
--- a/server/utils/redirectOnClient.ts
+++ b/server/utils/redirectOnClient.ts
@@ -1,4 +1,5 @@
import { escape } from "es-toolkit/compat";
+import { isbot } from "isbot";
import type { Context } from "koa";
import { addMissingUrlPort } from "@shared/utils/urls";
@@ -33,7 +34,7 @@ export function redirectOnClient(
// Bots are shown a manual submit button instead of auto-submitting, so that
// link scanners in email clients don't follow the redirect.
- const isBot = this.userAgent.isBot;
+ const isBot = isbot(this.get("user-agent"));
if (isBot) {
formFields += `
diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json
index 930dddfb3a..c05ef1a4e8 100644
--- a/shared/i18n/locales/en_US/translation.json
+++ b/shared/i18n/locales/en_US/translation.json
@@ -221,7 +221,7 @@
"{{ count }} share link revoked_plural": "{{ count }} share links revoked",
"Could not revoke {{ count }} share link": "Could not revoke {{ count }} share link",
"Could not revoke {{ count }} share link_plural": "Could not revoke {{ count }} share links",
- "{{ name }} (Logged out)": "{{ name }} (Logged out)",
+ "Logged out": "Logged out",
"Switch workspace": "Switch workspace",
"Select a workspace": "Select a workspace",
"New workspace": "New workspace",
@@ -1159,7 +1159,6 @@
"This doesn’t appear to be a valid installation": "This doesn’t appear to be a valid installation",
"Choose a workspace": "Choose a workspace",
"Choose an {{ appName }} workspace or log in to continue connecting this app": "Choose an {{ appName }} workspace or log in to continue connecting this app",
- "Logged out": "Logged out",
"Create workspace": "Create workspace",
"Set up your workspace by providing a name and details for admin login. You can change these later.": "Set up your workspace by providing a name and details for admin login. You can change these later.",
"Workspace name": "Workspace name",
@@ -1846,6 +1845,7 @@
"View Thread": "View Thread",
"{{ actorName }} replied to a thread in": "{{ actorName }} replied to a thread in",
"{{ actorName }} commented on": "{{ actorName }} commented on",
+ "Unsubscribe from this doc": "Unsubscribe from this doc",
"Mentioned you in “{{ documentTitle }}”": "Mentioned you in “{{ documentTitle }}”",
"{{ actorName }} mentioned you in a thread": "{{ actorName }} mentioned you in a thread",
"{{ actorName }} mentioned you in a comment on “{{ documentTitle }}”": "{{ actorName }} mentioned you in a comment on “{{ documentTitle }}”",
@@ -1892,7 +1892,6 @@
"{{ actorName }} {{ eventName }} the document “{{ documentTitle }}”_plural": "{{ actorName }} {{ eventName }} the document “{{ documentTitle }}”",
"{{ actorName }} {{ eventName }} the document": "{{ actorName }} {{ eventName }} the document",
"{{ actorName }} {{ eventName }} the document_plural": "{{ actorName }} {{ eventName }} the document",
- "Unsubscribe from this doc": "Unsubscribe from this doc",
"{{ actorName }} shared “{{ documentTitle }}” with you": "{{ actorName }} shared “{{ documentTitle }}” with you",
"{{ actorName }} shared a document": "{{ actorName }} shared a document",
"{{ actorName }} shared “{{ documentTitle }}” with you.": "{{ actorName }} shared “{{ documentTitle }}” with you.",
diff --git a/yarn.lock b/yarn.lock
index de6226332d..7b197e64f0 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -11839,6 +11839,13 @@ __metadata:
languageName: node
linkType: hard
+"isbot@npm:^5.2.2":
+ version: 5.2.2
+ resolution: "isbot@npm:5.2.2"
+ checksum: 10c0/df8356dd8745d7fe1922fc8df894bf5560fe959ad7826ac97311c430dc9c6dd628c8aab68d3e45769b50b61411bd4530d874aae07947f6cd04e25206566a7bb3
+ languageName: node
+ linkType: hard
+
"isexe@npm:^2.0.0":
version: 2.0.0
resolution: "isexe@npm:2.0.0"
@@ -14078,6 +14085,7 @@ __metadata:
ioredis: "npm:^5.11.1"
ioredis-mock: "npm:^8.13.1"
ipaddr.js: "npm:^2.5.0"
+ isbot: "npm:^5.2.2"
iso-639-3: "npm:^3.0.1"
js-yaml: "npm:^4.3.2"
js.foresight: "npm:^4.2.1"