Skip to content
Closed
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
30 changes: 10 additions & 20 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as path from "@std/path";
import { type ComponentType, h } from "preact";
import { renderToString } from "preact-render-to-string";
import type { ComponentType } from "preact";
import { trace } from "@opentelemetry/api";

import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts";
Expand All @@ -15,8 +14,7 @@ import {
} from "./config.ts";
import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
import type { ServerIslandRegistry } from "./context.ts";
import { FinishSetup, ForgotBuild } from "./finish_setup.tsx";
import { HttpError } from "./error.ts";
import { HttpError, SetupError } from "./error.ts";
import { mergePaths } from "./utils.ts";

// TODO: Completed type clashes in older Deno versions
Expand Down Expand Up @@ -254,11 +252,14 @@ export class App<State> {
);
}

if (
!this.#buildCache.hasSnapshot && this.config.mode === "production" &&
DENO_DEPLOYMENT_ID !== undefined
) {
return missingBuildHandler;
if (!this.#buildCache.hasSnapshot && this.config.mode === "production") {
let message =
"The build cache is not set up. Run `deno task build` before starting the server.";
if (DENO_DEPLOYMENT_ID !== undefined) {
message +=
' Go to "Settings" in Deno Deploy and set the build command to `deno task build`.';
}
throw new SetupError(message);
}

return async (
Expand Down Expand Up @@ -338,14 +339,3 @@ export class App<State> {
await listenOnFreePort(options, handler);
}
}

// deno-lint-ignore require-await
const missingBuildHandler = async (): Promise<Response> => {
const headers = new Headers();
headers.set("Content-Type", "text/html; charset=utf-8");

const html = DENO_DEPLOYMENT_ID
? renderToString(h(FinishSetup, null))
: renderToString(h(ForgotBuild, null));
return new Response(html, { headers, status: 500 });
};
23 changes: 23 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,26 @@ export class HttpError extends Error {
this.status = status;
}
}

/**
* Error that's throw when the app setup fails. Usually caused by
* `deno task build` not being run before starting the server.
*
* @example Basic usage
* ```ts ignore
* import { App, SetupError } from "fresh";
* import { expect } from "@std/expect";
*
* const app = new App()
* .get("/", () => new Response("ok"))
*
* try {
* // Throws if `deno task build` was not run beforehand
* const handler = await app.handler();
* } catch (error) {
* expect(error).toBeInstanceOf(SetupError);
* expect(error.message).toBe("App not setup");
* }
* ```
*/
export class SetupError extends Error {}
54 changes: 0 additions & 54 deletions src/finish_setup.tsx

This file was deleted.

5 changes: 5 additions & 0 deletions tests/fixture_precompile/valid/main.tsx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes might need to be reverted if checks in App.prototype.handler() are moved to App.prototype.listen().

Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { App } from "../../../src/app.ts";
import { Builder } from "../../../src/dev/mod.ts";

const builder = new Builder();

const app = new App({ staticDir: "./static" }).get(
"/",
Expand All @@ -25,6 +28,8 @@ const app = new App({ staticDir: "./static" }).get(
),
);

await builder.build(app);

const handler = app.handler();
const res = await handler(new Request("http://localhost/"));
// deno-lint-ignore no-console
Expand Down
Loading