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
28 changes: 10 additions & 18 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,19 @@ export type ListenOptions =

Deno.serve;

export let getRouter: <State>(app: App<State>) => Router<MiddlewareFn<State>>;
// deno-lint-ignore no-explicit-any
export let getIslandRegistry: (app: App<any>) => ServerIslandRegistry;
// deno-lint-ignore no-explicit-any
export let getBuildCache: (app: App<any>) => BuildCache | null;
// deno-lint-ignore no-explicit-any
export let setBuildCache: (app: App<any>, cache: BuildCache | null) => void;

export class App<State> {
#router: Router<MiddlewareFn<State>> = new UrlPatternRouter<
MiddlewareFn<State>
>();
#islandRegistry: ServerIslandRegistry = new Map();
#buildCache: BuildCache | null = null;
buildCache: BuildCache | null = null;
#islandNames = new Set<string>();

static {
getRouter = (app) => app.#router;
getIslandRegistry = (app) => app.#islandRegistry;
getBuildCache = (app) => app.#buildCache;
setBuildCache = (app, cache) => app.#buildCache = cache;
get router(): Router<MiddlewareFn<State>> {
return this.#router;
}
get islandRegistry(): ServerIslandRegistry {
return this.#islandRegistry;
}

/**
Expand Down Expand Up @@ -178,15 +170,15 @@ export class App<State> {
async handler(): Promise<
(request: Request, info?: Deno.ServeHandlerInfo) => Promise<Response>
> {
if (this.#buildCache === null) {
this.#buildCache = await ProdBuildCache.fromSnapshot(
if (this.buildCache === null) {
this.buildCache = await ProdBuildCache.fromSnapshot(
this.config,
this.#islandRegistry.size,
);
}

if (
!this.#buildCache.hasSnapshot && this.config.mode === "production" &&
!this.buildCache.hasSnapshot && this.config.mode === "production" &&
DENO_DEPLOYMENT_ID !== undefined
) {
return missingBuildHandler;
Expand Down Expand Up @@ -216,7 +208,7 @@ export class App<State> {
this.config,
next,
this.#islandRegistry,
this.#buildCache!,
this.buildCache!,
);

const span = trace.getActiveSpan();
Expand Down
15 changes: 5 additions & 10 deletions src/app_test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "@std/expect";
import { App, getIslandRegistry, setBuildCache } from "./app.ts";
import { App } from "./app.ts";
import { FakeServer } from "./test_utils.ts";
import { ProdBuildCache } from "./build_cache.ts";

Expand Down Expand Up @@ -422,15 +422,10 @@ Deno.test.ignore("FreshApp - finish setup", async () => {
return ctx.render(<div>ok</div>);
});

setBuildCache(
app,
await ProdBuildCache.fromSnapshot({
...app.config,
build: {
outDir: "foo",
},
}, getIslandRegistry(app).size),
);
app.buildCache = await ProdBuildCache.fromSnapshot({
...app.config,
build: { outDir: "foo" },
}, app.islandRegistry.size);

const server = new FakeServer(await app.handler());
const res = await server.get("/");
Expand Down
38 changes: 13 additions & 25 deletions src/dev/builder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
App,
getBuildCache,
getIslandRegistry,
type ListenOptions,
setBuildCache,
} from "../app.ts";
import { App, type ListenOptions } from "../app.ts";
import { fsAdapter } from "../fs.ts";
import * as path from "@std/path";
import * as colors from "@std/fmt/colors";
Expand Down Expand Up @@ -72,14 +66,11 @@ export class Builder implements FreshBuilder {

devApp.config.mode = "development";

setBuildCache(
devApp,
new MemoryBuildCache(
devApp.config,
BUILD_ID,
this.#transformer,
this.#options.target,
),
devApp.buildCache = new MemoryBuildCache(
devApp.config,
BUILD_ID,
this.#transformer,
this.#options.target,
);

await Promise.all([
Expand All @@ -90,14 +81,11 @@ export class Builder implements FreshBuilder {
}

async build<T>(app: App<T>): Promise<void> {
setBuildCache(
app,
new DiskBuildCache(
app.config,
BUILD_ID,
this.#transformer,
this.#options.target,
),
app.buildCache = new DiskBuildCache(
app.config,
BUILD_ID,
this.#transformer,
this.#options.target,
);

return await this.#build(app, false);
Expand All @@ -120,7 +108,7 @@ export class Builder implements FreshBuilder {
// Ignore
}

const buildCache = getBuildCache(app)! as
const buildCache = app.buildCache! as
| MemoryBuildCache
| DiskBuildCache;

Expand All @@ -133,7 +121,7 @@ export class Builder implements FreshBuilder {
};
const seenEntries = new Map<string, Island>();
const mapIslandToEntry = new Map<Island, string>();
const islandRegistry = getIslandRegistry(app);
const islandRegistry = app.islandRegistry;
for (const island of islandRegistry.values()) {
const filePath = island.file instanceof URL
? island.file.href
Expand Down
3 changes: 1 addition & 2 deletions tests/active_links_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { PartialInIsland } from "./fixtures_islands/PartialInIsland.tsx";
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
import { FakeServer } from "../src/test_utils.ts";
import { Partial } from "fresh/runtime";
import { getBuildCache, setBuildCache } from "../src/app.ts";

await buildProd(allIslandApp);

Expand All @@ -29,7 +28,7 @@ function testApp<T>(): App<T> {
.island(partialInIsland, "PartialInIsland", PartialInIsland)
.island(jsonIsland, "JsonIsland", JsonIsland)
.use(staticFiles());
setBuildCache(app, getBuildCache(allIslandApp));
app.buildCache = allIslandApp.buildCache;
return app;
}

Expand Down
4 changes: 1 addition & 3 deletions tests/islands_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import { FnIsland } from "./fixtures_islands/FnIsland.tsx";
import { FragmentIsland } from "./fixtures_islands/FragmentIsland.tsx";
import { EscapeIsland } from "./fixtures_islands/EscapeIsland.tsx";
import * as path from "@std/path";
import { setBuildCache } from "../src/app.ts";
import { getBuildCache } from "../src/app.ts";
import type { FreshConfig } from "../src/config.ts";
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
import { FakeServer } from "../src/test_utils.ts";
Expand All @@ -36,7 +34,7 @@ await buildProd(allIslandApp);

function testApp(config?: FreshConfig) {
const app = new App(config);
setBuildCache(app, getBuildCache(allIslandApp));
app.buildCache = allIslandApp.buildCache;
return app;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/partials_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { FakeServer } from "../src/test_utils.ts";
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
import { OptOutPartialLink } from "./fixtures_islands/OptOutPartialLink.tsx";
import * as path from "@std/path";
import { getBuildCache, setBuildCache } from "../src/app.ts";

import { retry } from "@std/async/retry";

const loremIpsum = await Deno.readTextFile(
Expand All @@ -43,7 +43,7 @@ function testApp<T>(): App<T> {
.island(optOutPartialLink, "OptOutPartialLink", OptOutPartialLink)
.use(staticFiles());

setBuildCache(app, getBuildCache(allIslandApp));
app.buildCache = allIslandApp.buildCache;
return app;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/test_utils.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, getIslandRegistry, setBuildCache } from "../src/app.ts";
import { App } from "../src/app.ts";
import { launch, type Page } from "@astral/astral";
import * as colors from "@std/fmt/colors";
import { type Document, DOMParser, HTMLElement } from "linkedom";
Expand Down Expand Up @@ -69,9 +69,9 @@ export async function buildProd(app: App<unknown>) {
await builder.build(app);
const cache = await ProdBuildCache.fromSnapshot(
app.config,
getIslandRegistry(app).size,
app.islandRegistry.size,
);
setBuildCache(app, cache);
app.buildCache = cache;
}

export async function withBrowserApp(
Expand Down
Loading