Commit Graph
590 Commits
Author SHA1 Message Date
Christian SvenssonandMarvin Hagemeister 85b38df578 test: check that _app and _layout are ignored by dev err frame (#3168)
Encountered this issue in #3167, but already fixed in #3165. I noticed
`/_frsh/error_overlay` was using `_app`/`_layout`, which seemed
unnecessary.

Since this was already fixed in #3165, I just added a test to check that
this is not a regression in the future.

---------

Co-authored-by: Marvin Hagemeister <marvin@deno.com>
2025-08-01 12:53:06 +02:00
Marvin Hagemeister 5f928d2cac fix(dev): dev server should not overtake app's root error handler (#3165) 2025-08-01 10:08:13 +02:00
Marvin Hagemeister 62f8bb008e docs: update Fresh 2 migration info (#3161) 2025-07-31 10:46:44 +02:00
Marvin Hagemeister b898548e95 fix(build): incorrect specifier with file:// islands (#3157) 2025-07-31 09:23:15 +02:00
Marvin Hagemeister 996e4b0fb0 fix: missing fs routes due to floating promise (#3155)
This fixes a bug where file system routes where missing due to a
floating promise.

We really need type aware lint rules in Deno 👀
2025-07-30 15:17:33 +02:00
Marvin Hagemeister 1c9b096482 fix(dev): support registering islands as file URLs (#3153) 2025-07-30 12:29:29 +02:00
Marvin Hagemeister 9d769f3cf1 feat: support setting production source maps on builder (#3151)
This adds a new `sourceMap` option on the `Builder` configuration. This
can be used to optionally specify how production source maps should be
generated.

See https://esbuild.github.io/api/#source-maps for more information.
2025-07-29 21:20:15 +02:00
Marvin Hagemeister c30e46ec30 feat: optimize static asset content type handling (#3149)
No need to detect them at runtime when we can do this during build. This
saves us from having to load `@std/media-types` during production which
is quite a heavy library due to the amount of file types out there.
2025-07-29 20:30:13 +02:00
Marvin Hagemeister ff34ec8342 chore: lift dependency to deno.json + update esbuild (#3142) 2025-07-25 19:01:28 +02:00
Marvin Hagemeister ae960adefd chore: switch MiddlewareFn -> Middleware (#3137) 2025-07-25 16:00:49 +02:00
Octo8080XandMarvin Hagemeister a988dc37e1 feat: add CSRF middleware (#3018)
Co-authored-by: Marvin Hagemeister <marvin@deno.com>
2025-07-25 15:55:33 +02:00
Marvin Hagemeister 3ad3e6ecf3 feat: support lazily loading routes/handlers (#3131)
We can improve cold start times by only loading route modules when the particular pattern matches. This PR introduces the concept of lazy middlewares, routes and handlers to do that.
2025-07-25 14:33:28 +02:00
Marvin Hagemeister 85c17fac72 fix: nested index route not working in prod builds (#3132)
This was due to a mismatch in the disk snapshot compared to in memory.
2025-07-24 21:27:16 +02:00
Marvin Hagemeister 41b11de470 docs: update fs route (#3125) 2025-07-24 15:34:51 +02:00
Marvin Hagemeister 118242eaa6 major: build islands + scan routeDir without importing user code (#3122)
This fixes a bunch of long standing issues in our tracker where a build
wouldn't finish because of importing user code. It's common for backend
apps to spawn some open ended tasks like subscribing to Deno KV or other
things. This addresses another problem of having to set up ENV vars just
for the build, even though they are technically not required to build
islands.

This also gives a roughly ~30% performance boost on cold starts (at
least on `deno.com`). Unfortunately, this does require some breaking
changes. But I believe the performance improvements + removing the
footgun of loading user code during build is well worth it. This work is
also a precursor in case we want to exploring moving to vite at some
point in the future.

## Breaking changes

1. Build options have moved to the builder.
```diff
  // main.ts
- const app = new App({ build: { outDir: "dist/" } })
+ const app = new App()

  // dev.ts
- const builder = new Builder();
+ const builder = new Builder({ outDir: "dist/" });
```

2. Another breaking change is how file based routes are loaded. Since
they're prepared and loaded by the builder the API changed.

```diff
  // main.ts
  const app = new App()
    .use(staticFiles())
  
- await fsRoutes(app, {
-   loadIsland: (path) => import(`./islands/${path}`),
-   loadRoute: (path) => import(`./routes/${path}`),
- }); 
+ app.fsRoutes()
```
You can optionally change the `islandDir` or `routeDir` by passing that
to the `Builder`:

```ts
// dev.ts
const builder = new Builder({
  islandDir: "/path/to/islands", 
  routeDir: "/path/to/routes"
});
```

3. Import `app` lazily in `dev.ts`. Passing `app` to `builder.build()`
is not necessary anymore.

```diff
- import { app } from "./main.ts";

  const builder = new Builder({ target: "safari12" });
  
  if (Deno.args.includes("build")) {
-    await builder.build(app);
+    await builder.build();
  } else {
-   await builder.listen(app);
+   await builder.listen(() => import("./main.ts"));
  }
```

4. The production run command has changed. It's now using `deno serve`
```diff
  // deno.json
  {
    "tasks": {
      "dev": "deno run -A --watch=static/,routes/ dev.ts",
      "dev": "deno run -A dev.ts build",
-     "start": "deno run -A main.ts",
+     "start": "deno serve -A _fresh/server.js",
    }
  }
```
Remove this bit in `main.ts`:

```diff
  // main.ts
  
- if (import.meta.main) {
-   await app.listen()
- }
```

Fixes https://github.com/denoland/fresh/issues/2240
Fixes https://github.com/denoland/fresh/issues/2640
Fixes https://github.com/denoland/fresh/issues/2592
Fixes https://github.com/denoland/fresh/issues/1843
2025-07-24 15:04:05 +02:00
Marvin Hagemeister cd7c3479a4 fix: basePath not applied to app.route() (#3116)
Fixes https://github.com/denoland/fresh/issues/3115
2025-07-19 12:29:45 +02:00
Marvin Hagemeister 39737b4530 fix: write matched pattern into ctx.route (#3113)
Fixes https://github.com/denoland/fresh/issues/3112
2025-07-18 22:38:56 +02:00
Asher GomezandMarvin Hagemeister 83da608504 BREAKING(core): remove FreshBuilder interface (#2935)
Once can just use `Builder` for typing.

---------

Co-authored-by: Marvin Hagemeister <marvin@deno.com>
2025-07-18 19:15:14 +02:00
Marvin Hagemeister a703e79181 docs: update canary docs for Fresh 2 (#3108)
There are still some rough edges here and there, but it's in a much
better shape than what we have today.
2025-07-18 15:18:38 +02:00
Marvin Hagemeister 226e250386 fix: middleware regressions (#3106)
Fixes some regressions found after merging
https://github.com/denoland/fresh/pull/3086

- Missing 404 middlewares when mounting sub-apps
- revert more staticFiles logic to always require the middleware like
before
2025-07-17 15:23:27 +02:00
Marvin Hagemeister c279efa345 fix: incorrect middleware ordering (#3102)
This is a regression introduced in
https://github.com/denoland/fresh/pull/3086
2025-07-16 16:43:46 +02:00
Marvin Hagemeister 599973bb5c fix: support non-fallthrough static files like before (#3099) 2025-07-16 14:34:39 +02:00
Luca Casonato b7384037f9 fix: find nearest deno config with compiler options (#3096)
Previously it would only consult the compiler options in the workspace
root deno.json, now it also considers relevant workspace members.
2025-07-16 11:36:21 +00:00
Marvin Hagemeister c11cf7b41a 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
2025-07-14 19:27:48 +02:00
Marvin Hagemeister 6eeb20c7a3 Revert "feat: support overwriting default 404 handler (#3081)" (#3093)
This reverts commit 0b3546f201.

Reverting this because it will be superseded by
https://github.com/denoland/fresh/pull/3086
2025-07-14 18:11:20 +02:00
Marvin Hagemeister e2699923b0 chore: rename tests FreshApp -> App (#3087) 2025-07-10 10:24:39 +02:00
Marvin Hagemeister a941dd33c5 chore: ignore test tmp delete failures on windows (#3088) 2025-07-10 10:21:22 +02:00
zhizheng cfc9a9a601 chore: ensures more stable CI runs on Windows environments. (#3070)
This pull request includes a small but important change to the
`withTmpDir` function in `src/test_utils.ts`. The change adds error
handling to ensure that a `PermissionDenied` error during directory
removal does not interrupt the execution.
2025-07-10 10:09:16 +02:00
Marvin Hagemeister 31434a2cf9 fix: wrong fresh domain used in hash (#3085) 2025-07-09 23:39:39 +02:00
Marvin Hagemeister f7174d0df0 chore: cleanup cors middleware (#3083)
- Remove some indirections
- Make tests self contained
- Switch tests to `Deno.test` as the LSP doesn't yet recognise
`describe/it`
2025-07-09 21:44:57 +02:00
Chris d072dc2fc0 fix: avoid reload for user-code history changes (#3045) (#3048)
The Fresh partials popstate handler causes page reloads when user code
makes use of navigation history state. This can lead to a loss of user
data in a browsing context.
To fix this behavior, FreshHistoryState now has a flag which validates
partials functionality/explicitly opts-in to any partial related
location management.
2025-07-09 20:59:32 +02:00
Coty ee46263053 feat: Add localhost help text to server address output (#3077)
When binding to `0.0.0.0` or `::`, show localhost equivalent URL for
convenience

Closes #3076

output from fresh www

```
deno task start
Task start deno run -A --watch=static/,routes/,../src,../docs dev.ts
Watcher Process started.

 🍋 Fresh ready
    Local:  http://0.0.0.0:8000/ (http://localhost:8000/)

GA4_MEASUREMENT_ID environment variable not set. Google Analytics reporting disabled.
```
2025-07-09 00:58:04 +02:00
Marvin Hagemeister 0b3546f201 feat: support overwriting default 404 handler (#3081)
Fixes https://github.com/denoland/fresh/issues/1918
2025-07-09 00:40:12 +02:00
Marvin Hagemeister 8416078b6a fix(core): improve island props serialization (#3071) 2025-06-30 16:31:58 +02:00
Asher Gomez 8f813fad90 fix(core): use PageProps instead of FreshContext in define*() compat functions (#3064)
In Fresh 1, define apps used `RouteContext`, which align with
`PageProps` in Fresh 2, rather than `FreshContext`. E.g. before this PR,
one would get a type error if they tried to use `ctx.Component` within
`defineApp()` and now they shouldn't.
2025-06-27 09:54:53 +02:00
Asher Gomez 682368ba30 refactor(src): remove redundant coloring from createCodeFrame() (#3049)
`src/runtime/server/preact_hooks.tsx` is the only place that
`createCodeFrame()` is used which stripped the ANSI codes anyway.
2025-06-18 10:43:56 +02:00
Marvin Hagemeister 85d22e0bb0 fix(core): move to new esbuild deno plugin (#3034)
Move to the new esbuild plugin that leverages `@deno/loader` under the
hood. This is based on the exact same rust crates that Deno itself uses
to do resolution, instead of doing it's own resolution.
2025-06-18 10:42:47 +02:00
75897de196 chore: add tests for fresh/compat imports (#3036)
- `fresh/compat - defineFn mixed return types` - Tests all supported
return types (Response, VNode, null) with both sync and async handlers,
including FreshContext parameter usage

- `fresh/compat - multiple compat imports` - Tests importing multiple
compat functions simultaneously

close #3025 

--patch-1
try to use deno/std Api to replace

--patch-2
make it simple and clean

--patch-3
Convert update_test.ts to .tsx for JSX support
Use static imports instead of dynamic imports for better performance
Switch from @std/assert to @std/expect for consistency
Use ReturnType<> for automatic type inference instead of manual type
definitions

Sincerely grateful to @iuioiua for the guidance and patience.

---------

Co-authored-by: 李嘉图·M·路 <146103794+Ricardo-M-Zheng@users.noreply.github.com>
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2025-06-17 13:06:21 +02:00
Asher Gomez c3c79352b0 fix(core): refine HttpError.status property type (#2890)
As this is property only ever has an error HTTP status code (>400).
2025-06-16 17:57:49 +02:00
Asher Gomez 71b7de4581 fix(core): align defineFn() with previous definition (#3043)
https://github.com/denoland/fresh/blob/990357629e50ef123e5b5ee2b80c250be867534f/src/server/defines.ts#L13-L16

Closes #3042
2025-06-16 11:04:18 +02:00
Asher Gomez e868a924fa fix(update): correct return and argument types for defineFn() (#3023)
Previously, the return union type of the inner function and the function
itself contained a `Request` instead of a `Response`.
2025-06-12 13:25:36 +02:00
Octo8080XandMarvin Hagemeister 1213aad90d feat: add CORS middleware (#3013)
## Why was this PR initiated?

I opened an issue and held discussions about officially providing
middleware in Fresh by porting middleware from Hono.

https://github.com/denoland/fresh/issues/3011

## What does this PR do? 

I introduced the CORS middleware bundled with Fresh by porting the CORS
middleware from Hono.


I appreciate your review.

---------

Co-authored-by: Marvin Hagemeister <marvin@deno.com>
2025-06-11 17:34:00 +02:00
Christian Svensson b4c3ff8e98 feat: add flag to FreshContext for partial requests (#2993)
Addresses a gap between [Fresh
v1](https://github.com/denoland/fresh/blob/1.x/src/server/types.ts#L192)
and Fresh v2, by adding `ctx.isPartial` to `FreshContext`.

The same route (e.g. `GET /api/autocomplete`) can be called from both an
Island or a Partial. Returning JSON or HTML depending on the destination
of the code.

Or is the intent that endpoints returning partials and data should be
separated, and this pattern is not to be encouraged in Fresh v2? My use
case in Fresh v1 was an `/routes/api/cart.tsx`, which can either just
toggle a product from the cart (via cookie), but also update UI elements
(e.g. cart icon) based on if called from partial or not.


![image](https://github.com/user-attachments/assets/a1cbcfb1-2446-4d6d-bce5-4c3d6acf9bfd)
2025-05-26 14:15:36 +02:00
Marvin Hagemeister 750ab35499 fix: add back LayoutConfig (#2982)
~~Makes it easier to upgrade from 1.x .~~

Initially, I assumed it would be just an alias to `RouteConfig`, but
upon closer inspection that has some additional properties that make no
sense for a layout.
2025-05-21 13:44:09 +02:00
Christian Svensson 17257dd1c2 test: use disposable tmp dirs (#2983)
Replaces the patterns below with consistent `AsyncDisposable` and `await
using` resource management for automatic cleanup.

- `await withTmpDir(async (dir) => { ... })` with `finally`
  - Makes code a bit flatter, and reduces callback-hell a bit
- `await Deno.makeTempDir()` + `Deno.remove()`
- `await Deno.makeTempDir()`

Helps be more consistent in removing temporary folders. Among others,
ensures temp folders are removed in the tests below:

- `src/build_cache_test.ts`
- `src/dev/builder_test.ts`
- `src/dev/dev_build_cache_test.ts`

## TODO's

The `Deno.makeTempDir()` method is still used by `buildProd` as well,
but this method is used in the top module scope, where it does not work
correctly with `Deno.test` (it runs before the test methods). I removed
this part from the PR and leave this for later to avoid making the PR
too big.

-
https://github.com/csvn/fresh/blob/test%2Fdisposable-tmp-dirs/tests/test_utils.tsx/#L64
-
https://github.com/csvn/fresh/blob/test%2Fdisposable-tmp-dirs/www/main_test.ts/#L7
2025-05-21 11:13:02 +02:00
Christian Svensson db142115ec feat: support automatic workspace detection in Dev (#2985)
Adds support for [Automatic Workspace
Detection](https://chromium.googlesource.com/devtools/devtools-frontend/+/main/docs/ecosystem/automatic_workspace_folders.md).

- Avoids 404's or middleware logging when this feature is active (e.g.
Chrome/Edge with DevTools open)
- Generates the same UUID based on root folder path (without writing
anything to disk)
- The middleware is lazy and does nothing until the path is opened for
`com.chrome.devtools.json`
2025-05-21 11:09:43 +02:00
Christian Svensson bc8cdb9c7b fix: convert island filePath to valid variable name (#2984)
I only added a unit test, but maybe it's testing internals too much?
Should I add a more E2E test in `tests/` with real files?


Closes #2971
2025-05-21 11:06:16 +02:00
Asher Gomez 144ed2e75c refactor(core): minor fresh/compat cleanups (#2880) 2025-05-19 12:05:22 +02:00
Marvin Hagemeister e996b5e2fe feat(core): support exclude option for transformers (#2960)
This PR adds general support for an `exclude` option to file
transformers. This allows plugin to skip processing excluded files. The
exclude array can be a `string`, a `RegExp` or a glob pattern.

Fixes https://github.com/denoland/fresh/issues/2937
2025-05-16 15:17:43 +02:00
Marvin Hagemeister 63cb208f8b fix(core): support method handler on _error, _404 and _500 routes (#2955)
This PR adds support for using method handlers in `_error`, `_404` and
`_500` files.

Fixes https://github.com/denoland/fresh/issues/2953
2025-05-15 17:15:30 +02:00