Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion src/build/vite/prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ export async function buildEnvironments(ctx: NitroPluginContext, builder: ViteBu
await prerender(nitro);

// Build the Nitro server bundle
const output = (await builder.build(builder.environments.nitro)) as RolldownOutput;
let output: RolldownOutput | undefined;
if (nitro.options.static) {
// Static presets have no server entry. Vite's `buildApp()` builds every
// environment when none were built, so mark this one as handled.
builder.environments.nitro.isBuilt = true;
} else {
output = (await builder.build(builder.environments.nitro)) as RolldownOutput;
}

// Close the Nitro instance
await nitro.close();
Expand Down
6 changes: 6 additions & 0 deletions test/vite/static-preset-fixture/nitro.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "nitro";

export default defineConfig({
preset: "static",
prerender: { routes: ["/"] },
});
7 changes: 7 additions & 0 deletions test/vite/static-preset-fixture/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
fetch(_req: Request) {
return new Response("<h1>prerendered</h1>", {
headers: { "content-type": "text/html" },
});
},
};
6 changes: 6 additions & 0 deletions test/vite/static-preset-fixture/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import { nitro } from "nitro/vite";

export default defineConfig({
plugins: [nitro()],
});
33 changes: 33 additions & 0 deletions test/vite/static-preset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { fileURLToPath } from "node:url";
import { join } from "node:path";
import { readFile, readdir, rm, mkdir } from "node:fs/promises";
import { describe, expect, it } from "vitest";
import { createNitro, build, prepare } from "nitro/builder";

const fixtureDir = fileURLToPath(new URL("./static-preset-fixture", import.meta.url));
const tmpDir = fileURLToPath(new URL("./static-preset-fixture/.tmp", import.meta.url));

describe("static preset", () => {
const outDir = join(tmpDir, "vite");

it("build", async () => {
await rm(outDir, { recursive: true, force: true });
await mkdir(outDir, { recursive: true });
const nitro = await createNitro({
rootDir: fixtureDir,
output: { dir: outDir },
builder: "vite",
});
await prepare(nitro);
await build(nitro);
});

it("does not emit a server bundle", async () => {
expect((await readdir(outDir)).sort()).toEqual(["nitro.json", "public"]);
});

it("prerenders routes to the public dir", async () => {
const html = await readFile(join(outDir, "public/index.html"), "utf8");
expect(html).toContain("<h1>prerendered</h1>");
});
});
Loading