Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prerenderer): write responses with json signature to original path #1963

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/prerender.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default eventHandler(() => {
return '{"foo":"bar"}';
});
1 change: 1 addition & 0 deletions test/fixture/routes/prerender.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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