Skip to content

Commit

Permalink
fix(prerenderer): write responses with json signature to original path (
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 28, 2023
1 parent b4386f8 commit b53e001
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/prerender.ts
Expand Up @@ -14,6 +14,8 @@ import { compressPublicAssets } from "./compress";

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

const JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(\.\d{1,17})?([Ee][+-]?\d+)?\s*$/; // From unjs/destr

const linkParents = new Map<string, Set<string>>();

export async function prerender(nitro: Nitro) {
Expand Down Expand Up @@ -220,7 +222,9 @@ export async function prerender(nitro: Nitro) {
// Guess route type and populate fileName
const contentType = res.headers.get("content-type") || "";
const isImplicitHTML =
!route.endsWith(".html") && contentType.includes("html");
!route.endsWith(".html") &&
contentType.includes("html") &&
!JsonSigRx.test(dataBuff.subarray(0, 32).toString("utf8"));
const routeWithIndex = route.endsWith("/") ? route + "index" : route;
const htmlPath =
route.endsWith("/") || nitro.options.prerender.autoSubfolderIndex
Expand Down
3 changes: 3 additions & 0 deletions test/fixture/routes/json-string.ts
@@ -0,0 +1,3 @@
export default eventHandler(() => {
return '{"foo":"bar"}';
});
1 change: 1 addition & 0 deletions test/fixture/routes/prerender.ts
Expand Up @@ -12,6 +12,7 @@ export default defineEventHandler((event) => {
"../api/hey",
"/api/param/foo.json",
"/api/param/foo.css",
"/json-string",
event.path.includes("?") ? "/api/param/hidden" : "/prerender?withQuery",
];

Expand Down
1 change: 1 addition & 0 deletions test/presets/cloudflare-pages.test.ts
Expand Up @@ -53,6 +53,7 @@ describe("nitro:preset:cloudflare-pages", async () => {
"/build/*",
"/favicon.ico",
"/icon.png",
"/json-string",
"/api/hello",
"/prerender/index.html",
"/prerender/index.html.br",
Expand Down

0 comments on commit b53e001

Please sign in to comment.