feat: expose layouts and pages on App (#3086)

This PR extracts the functionality from the `fs_routes` plugin and
exposes it onto `App`. These are the new APIs:

- `app.layout(pattern, route)`
- `app.onError(pattern, route)`
- `app.notFound(pattern, route)`
- `app.route(pattern, route)`

This also removes the need for `.use(staticFiles())`. This line can be
safely removed.

Fixes https://github.com/denoland/fresh/issues/3040
Fixes https://github.com/denoland/fresh/issues/3033
Fixes https://github.com/denoland/fresh/issues/2591
Fixes https://github.com/denoland/fresh/issues/950
Fixes https://github.com/denoland/fresh/issues/1918
This commit is contained in:
Marvin Hagemeister
2025-07-14 19:27:48 +02:00
committed by GitHub
parent ccb621d2a3
commit c11cf7b41a
35 changed files with 1328 additions and 971 deletions
+2 -3
View File
@@ -1,4 +1,4 @@
import { App, staticFiles } from "fresh";
import { App } from "fresh";
import {
allIslandApp,
assertNotSelector,
@@ -27,8 +27,7 @@ function testApp<T>(): App<T> {
const app = new App<T>()
.island(selfCounter, "SelfCounter", SelfCounter)
.island(partialInIsland, "PartialInIsland", PartialInIsland)
.island(jsonIsland, "JsonIsland", JsonIsland)
.use(staticFiles());
.island(jsonIsland, "JsonIsland", JsonIsland);
setBuildCache(app, getBuildCache(allIslandApp));
return app;
}
+1 -23
View File
@@ -19,7 +19,6 @@ import {
withBrowserApp,
} from "./test_utils.tsx";
import { parseHtml, waitForText } from "./test_utils.tsx";
import { staticFiles } from "../src/middlewares/static_files.ts";
import { expect } from "@std/expect";
import { JsxConditional } from "./fixtures_islands/JsxConditional.tsx";
import { FnIsland } from "./fixtures_islands/FnIsland.tsx";
@@ -47,7 +46,6 @@ Deno.test({
const counterIsland = getIsland("Counter.tsx");
const app = testApp()
.use(staticFiles())
.island(counterIsland, "Counter", Counter)
.get("/", (ctx) => {
const sig = signal(3);
@@ -73,7 +71,6 @@ Deno.test({
const multipleIslands = getIsland("Multiple.tsx");
const app = testApp()
.use(staticFiles())
.island(multipleIslands, "Multiple1", Multiple1)
.island(multipleIslands, "Multiple2", Multiple2)
.get("/", (ctx) => {
@@ -103,7 +100,6 @@ Deno.test({
const counterIsland = getIsland("Counter.tsx");
const app = testApp()
.use(staticFiles())
.island(counterIsland, "Counter", Counter)
.get("/", (ctx) => {
const sig = signal(0);
@@ -132,7 +128,6 @@ Deno.test({
const jsonIsland = getIsland("JsonIsland.tsx");
const app = testApp()
.use(staticFiles())
.island(jsonIsland, "JsonIsland", Counter)
.get("/", (ctx) => {
return ctx.render(
@@ -160,7 +155,6 @@ Deno.test({
const nullIsland = getIsland("NullIsland.tsx");
const app = testApp()
.use(staticFiles())
.island(nullIsland, "NullIsland", NullIsland)
.get("/", (ctx) => {
return ctx.render(
@@ -184,7 +178,6 @@ Deno.test({
const islandInIsland = getIsland("IslandInIsland.tsx");
const app = testApp()
.use(staticFiles())
.island(counter, "Counter", Counter)
.island(islandInIsland, "IslandInIsland", IslandInIsland)
.get("/", (ctx) => {
@@ -213,7 +206,6 @@ Deno.test({
const jsxIsland = getIsland("JsxIsland.tsx");
const app = testApp()
.use(staticFiles())
.island(jsxIsland, "JsxIsland", JsxIsland)
.get("/", (ctx) => {
return ctx.render(
@@ -241,7 +233,6 @@ Deno.test({
const jsxIsland = getIsland("JsxIsland.tsx");
const app = testApp()
.use(staticFiles())
.island(jsxIsland, "JsxIsland", JsxIsland)
.get("/", (ctx) => {
return ctx.render(
@@ -271,7 +262,6 @@ Deno.test({
const jsxChildrenIsland = getIsland("JsxChildrenIsland.tsx");
const app = testApp()
.use(staticFiles())
.island(jsxChildrenIsland, "JsxChildrenIsland", JsxChildrenIsland)
.get("/", (ctx) => {
return ctx.render(
@@ -307,7 +297,6 @@ Deno.test({
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp()
.use(staticFiles())
.island(passThrough, "PassThrough", PassThrough)
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) => {
@@ -339,7 +328,6 @@ Deno.test({
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp()
.use(staticFiles())
.island(counterWithSlots, "CounterWithSlots", CounterWithSlots)
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) => {
@@ -382,7 +370,6 @@ Deno.test({
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp()
.use(staticFiles())
.island(passThrough, "PassThrough", PassThrough)
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) => {
@@ -424,7 +411,6 @@ Deno.test({
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp()
.use(staticFiles())
.island(jsxConditional, "JsxConditional", JsxConditional)
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) => {
@@ -466,7 +452,6 @@ Deno.test({
const jsxConditional = getIsland("JsxConditional.tsx");
const app = testApp()
.use(staticFiles())
.island(jsxConditional, "JsxConditional", JsxConditional)
.get("/", (ctx) => {
return ctx.render(
@@ -541,7 +526,6 @@ Deno.test({
const fnIsland = getIsland("FnIsland.tsx");
const app = testApp()
.use(staticFiles())
.island(fragmentIsland, "FragmentIsland", FragmentIsland)
.island(fnIsland, "FnIsland", FnIsland)
.get("/", (ctx) => {
@@ -570,7 +554,6 @@ Deno.test({
const escapeIsland = getIsland("EscapeIsland.tsx");
const app = testApp()
.use(staticFiles())
.island(escapeIsland, "EscapeIsland", EscapeIsland)
.get("/", (ctx) => {
return ctx.render(
@@ -667,7 +650,6 @@ Deno.test({
const nodeProcess = getIsland("NodeProcess.tsx");
const app = testApp()
.use(staticFiles())
.island(nodeProcess, "NodeProcess", NodeProcess)
.get("/", (ctx) =>
ctx.render(
@@ -695,7 +677,6 @@ Deno.test({
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp({ basePath: "/foo" })
.use(staticFiles())
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) =>
ctx.render(
@@ -720,7 +701,6 @@ Deno.test({
const freshAttrs = getIsland("FreshAttrs.tsx");
const app = testApp()
.use(staticFiles())
.island(freshAttrs, "FreshAttrs", FreshAttrs)
.get("/", (ctx) =>
ctx.render(
@@ -747,8 +727,7 @@ Deno.test({
Deno.test({
name: "fsRoutes - load islands from group folder",
fn: async () => {
const app = testApp()
.use(staticFiles());
const app = testApp();
await fsRoutes(app, {
dir: path.join(
@@ -778,7 +757,6 @@ Deno.test({
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp()
.use(staticFiles())
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) =>
ctx.render(
+2 -3
View File
@@ -1,4 +1,4 @@
import { App, staticFiles } from "fresh";
import { App } from "fresh";
import { Partial } from "fresh/runtime";
import {
allIslandApp,
@@ -40,8 +40,7 @@ function testApp<T>(): App<T> {
.island(selfCounter, "SelfCounter", SelfCounter)
.island(partialInIsland, "PartialInIsland", PartialInIsland)
.island(jsonIsland, "JsonIsland", JsonIsland)
.island(optOutPartialLink, "OptOutPartialLink", OptOutPartialLink)
.use(staticFiles());
.island(optOutPartialLink, "OptOutPartialLink", OptOutPartialLink);
setBuildCache(app, getBuildCache(allIslandApp));
return app;