Software & Apps

Fast setup with Deno and Fresh

Fresh provides a zero JavaScript default by sending only HTML to the browser, which improves page loads on mobile data. This framework uses Preact for UI components and allows for easy deployment to Deno Deploy, which offers 100,000 free requests per day.

Fast setup with Deno and Fresh

Fresh requires Deno version 1.22.3 or higher. I find the zero JavaScript default to be the most effective part of this framework. It sends only HTML to the browser, which speeds up page loads on slow mobile data. The initialization command asks if you want to use Twind and then generates a directory with three routes and one island. If you know React, you will feel at home because the framework uses Preact. The lack of a build step makes the developer experience smooth. Although the reliance on Preact limits your UI choices, the architecture remains efficient. Deno provides a secure runtime for JavaScript and TypeScript. You can execute scripts directly from URLs or build an HTTP server that fetches data and returns it as a response. Deno handles dependencies via import maps, which use a JSON file to map import names to specific paths. This eliminates the need for a package.json file. Deno also provides APIs for tasks not covered by web standards, such as reading files. You can use the fetch API in Deno to make HTTP requests to a web server, wait for the response, and parse the body as text.

Managing routes and islands

The file-system routing system uses the routes directory to define paths. A file at routes/articles/[id].tsx handles the path /articles/foo. Handlers and components make up these routes. The handler can be a function or an object. If the handler is defined as an object, every key must correspond to an HTTP method. If an HTTP request does not have a corresponding key-handler, the system returns a 405 error. You can access path parameters through ctx.params. You can also use ctx.render to call a Preact template and pass data to it. Interactive elements live in the islands directory. These components use PascalCase filenames and an export default statement. Fresh rehydrates these islands on the client. Only these small islands use JavaScript, while the rest of the page stays light. Middleware logic resides in _middleware.ts. You can use ctx.next() to call subsequent handlers in a middleware chain.

File Purpose
deno.json Configures tasks and import maps
import_map.json Manages dependencies via URLs
dev.ts Entry point for development
main.ts Entry point for production
fresh.gen.ts Contains route and island manifest

You can manage your project tasks in deno.json using the tasks field. The fresh.gen.ts file contains the manifest for routes and islands, and you should check this file into your version control system. Static files sit in the static directory.

Deploying to the edge

Deno Deploy provides a globally distributed runtime for these applications. You can push your code to GitHub and connect it to Deno Deploy through the dashboard. The platform includes 100,000 requests per day in the free tier. To deploy your application to Deno Deploy, you push your code to a GitHub repository and then select the Fresh framework preset from the Deno Deploy dashboard settings to start the process. Once the deployment completes, your project becomes available at its assigned URL. To run the application in production, execute deno run --allow-net --allow-env main.ts. This command binds to the port supplied by the platform and listens on all interfaces. You can also run Fresh on any server using deno run. If you use a custom server, you must set a DENO_DEPLOYMENT_ID environment variable with a unique hash. Deno Deploy works with 33 regions worldwide to minimize latency. Using Deno Deploy allows you to sidestep the complexities of managing server infrastructure. You can also use Docker if you want platform independence or if you want to encapsulate your application within containers. Deno Deploy includes a feature to manage different versions, allowing you to switch between them or roll back to a previous version if necessary. Does the lack of database integration in the core framework hinder large-scale development?