Skip to content

Commit 30bf54c

Browse files
authored
Fix cloudflare functions so forwarding works (#13152)
1 parent 64828b0 commit 30bf54c

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

.changeset/cyan-rockets-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"website": minor
3+
---
4+
5+
feat:Fix cloudflare functions so forwarding works

js/_website/src/lib/components/DocsCopyMarkdown.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
try {
4444
const response = await fetch(`/api/markdown/${doc_name}`);
4545
if (response.ok) {
46-
const data = await response.json();
47-
return data.markdown || "";
46+
return (await response.text()) || "";
4847
}
4948
} catch (error) {
5049
console.error("Error fetching markdown:", error);

js/_website/src/routes/api/markdown/[doc]/+server.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { json } from "@sveltejs/kit";
21
import { readFileSync } from "fs";
32
import { resolve as pathResolve } from "path";
43
import { svxToMarkdown } from "$lib/utils/svx-to-markdown";
54
import docs_json from "$lib/templates/docs.json";
65

76
export const prerender = true;
87

8+
const MARKDOWN_HEADERS = {
9+
"Content-Type": "text/markdown; charset=utf-8",
10+
"X-Robots-Tag": "noindex"
11+
};
12+
913
export function entries() {
1014
return docs_json.pages.gradio.flatMap((category) =>
1115
category.pages.map((page) => ({ doc: page.name }))
@@ -28,7 +32,7 @@ export async function GET({ params }) {
2832
}
2933

3034
if (!svxPath) {
31-
return json({ markdown: "", error: "Doc not found" }, { status: 404 });
35+
return new Response("Doc not found", { status: 404 });
3236
}
3337

3438
try {
@@ -37,12 +41,9 @@ export async function GET({ params }) {
3741

3842
const markdown = await svxToMarkdown(svxContent, name);
3943

40-
return json({ markdown });
44+
return new Response(markdown, { headers: MARKDOWN_HEADERS });
4145
} catch (error) {
4246
console.error("Error generating markdown:", error);
43-
return json(
44-
{ markdown: "", error: "Error generating markdown" },
45-
{ status: 500 }
46-
);
47+
return new Response("Error generating markdown", { status: 500 });
4748
}
4849
}

js/_website/src/routes/api/markdown/guide/[guide]/+server.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { json } from "@sveltejs/kit";
21
import guide_names from "$lib/json/guides/guide_names.json";
32
import { cleanGuideHtml } from "$lib/utils/clean-guide-html";
43

54
export const prerender = true;
65

6+
const MARKDOWN_HEADERS = {
7+
"Content-Type": "text/markdown; charset=utf-8",
8+
"X-Robots-Tag": "noindex"
9+
};
10+
711
export function entries() {
812
return guide_names.guide_urls.map((guide) => ({ guide }));
913
}
@@ -17,11 +21,13 @@ export async function GET({ params }) {
1721
const markdown = data.guide?.content;
1822

1923
if (!markdown) {
20-
return json({ markdown: "", error: "Guide not found" }, { status: 404 });
24+
return new Response("Guide not found", { status: 404 });
2125
}
2226

23-
return json({ markdown: await cleanGuideHtml(markdown) });
27+
return new Response(await cleanGuideHtml(markdown), {
28+
headers: MARKDOWN_HEADERS
29+
});
2430
} catch {
25-
return json({ markdown: "", error: "Guide not found" }, { status: 404 });
31+
return new Response("Guide not found", { status: 404 });
2632
}
2733
}

0 commit comments

Comments
 (0)