Skip to content

Commit b53e001

Browse files
authored
fix(prerenderer): write responses with json signature to original path (#1963)
1 parent b4386f8 commit b53e001

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

src/prerender.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { compressPublicAssets } from "./compress";
1414

1515
const allowedExtensions = new Set(["", ".json"]);
1616

17+
const JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(\.\d{1,17})?([Ee][+-]?\d+)?\s*$/; // From unjs/destr
18+
1719
const linkParents = new Map<string, Set<string>>();
1820

1921
export async function prerender(nitro: Nitro) {
@@ -220,7 +222,9 @@ export async function prerender(nitro: Nitro) {
220222
// Guess route type and populate fileName
221223
const contentType = res.headers.get("content-type") || "";
222224
const isImplicitHTML =
223-
!route.endsWith(".html") && contentType.includes("html");
225+
!route.endsWith(".html") &&
226+
contentType.includes("html") &&
227+
!JsonSigRx.test(dataBuff.subarray(0, 32).toString("utf8"));
224228
const routeWithIndex = route.endsWith("/") ? route + "index" : route;
225229
const htmlPath =
226230
route.endsWith("/") || nitro.options.prerender.autoSubfolderIndex

test/fixture/routes/json-string.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default eventHandler(() => {
2+
return '{"foo":"bar"}';
3+
});

test/fixture/routes/prerender.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default defineEventHandler((event) => {
1212
"../api/hey",
1313
"/api/param/foo.json",
1414
"/api/param/foo.css",
15+
"/json-string",
1516
event.path.includes("?") ? "/api/param/hidden" : "/prerender?withQuery",
1617
];
1718

test/presets/cloudflare-pages.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe("nitro:preset:cloudflare-pages", async () => {
5353
"/build/*",
5454
"/favicon.ico",
5555
"/icon.png",
56+
"/json-string",
5657
"/api/hello",
5758
"/prerender/index.html",
5859
"/prerender/index.html.br",

0 commit comments

Comments
 (0)