feat: remove fresh cli, do manifest generation in process (#223)

This commit removes the `fresh` cli, replacing it with a small bootstrap script,
and JIT generation of the manifest that happens in process during development.
This commit is contained in:
Luca Casonato
2022-06-14 09:39:38 +02:00
committed by GitHub
parent 844ded8cfd
commit 836cc24bdd
40 changed files with 362 additions and 730 deletions
-62
View File
@@ -1,62 +0,0 @@
# Data fetching
Not all pages can be server rendered synchronously - some pages are dynamic and
require fetching some data from an external source. This data fetching is often
asynchronous.
Fresh supports asynchronous data fetching in handlers. This is not done during
rendering, but before rendering in the route
handler<!-- TODO: link route handlers page -->.
## Fetching data in a `handler`
To fetch data, you must first define a custom `handler` function. This function
is called when the route is requested. In there you can do some handling, and
then return a `Response` object. If you want to render the page, you can call
the `ctx.render` function. This returns a `Promise<Response>` that you can
return from the handler.
The `ctx.render` function takes a single argument, which is some data that you
want to pass to the page template. The data can be accessed in the template via
the `props.data` property.
Here is a usage example of a `handler` fetches a URL as JSON and provides the
resulting data to the template:
```tsx
import { h, Handlers, PageProps, useData } from "../deps.ts";
interface ModuleInfo {
latest: string;
versions: string[];
}
export const handler: Handlers<ModuleInfo> = {
async GET(ctx) {
const resp = await fetch("https://cdn.deno.land/std/meta/versions.json");
let data = null;
if (resp.status === 200) data = await resp.json();
return ctx.render(data);
},
};
export default function Page(props: PageProps<ModuleInfo>) {
const data = props.data;
if (data === null) {
return <div>Data not available.</div>;
}
const { latest, versions } = data;
return (
<p>
The Deno standard library has {versions.length} versions, with {latest}
{" "}
being the most recent.
</p>
);
}
```
If you want to fetch data client side, inside of an island, you can use
libraries like `"swr"`, or raw `useEffect` and `useState` as you usually would.
-28
View File
@@ -1,28 +0,0 @@
# File system routing
The naming and location of files in the `routes` folder is important because
these are used to specify the route where a specific API endpoint or HTML page
is available. This concept is called file system routing.
As an example, the HTML page described by `index.tsx` would be served at `/`,
while the `about.tsx` page would be served at `/about`. File system routing is
also capable of dynamic path segments: `/posts/[post].jsx` would be used for
requests to `/posts/foo`, `/posts/bar`, or `/posts/baz`, but not for
`/posts/foo/comments` (that would be matched by `/posts/[post]/comments.tsx`).
Multi-level dynamic segments are also possible: `/[owner]/[repo]/[...path]`
would match `/denoland/deno/README.md` and `/lucacasonato/fresh/cli/build.rs`.
Multi level dynamic segments can only be used as the last path segment.
## Comparison with path-to-regex
If you are more familiar with path-to-regex style routes, here is a simple
translation table to get you up to speed.
| `path-to-regex` | File system routing |
| ----------------------- | ------------------------------------------------------------------ |
| `/` | `/index.tsx` |
| `/about` | `/about.tsx` or `/about/index.tsx` |
| `/posts/:name` | `/posts/[name].tsx` or `/posts/[name]/index.tsx` |
| `/posts/:name/comments` | `/posts/[name]/comments.tsx` or `/posts/[name]/comments/index.tsx` |
| `/x/:module/:path*` | `/x/[module]/[...path].tsx` |
-85
View File
@@ -1,85 +0,0 @@
# Getting Started
Welcome to the `fresh` documentation.
## Requirements
The documentation assumes you have Deno 1.20.1 or later installed.
To install Deno, follow the installation instructions in the manual:
https://deno.land/manual/getting_started/installation
## Setup
`fresh` comes with a CLI tool, conveniently also called `fresh`. It is used to
scaffold new projects and generate the route manifest. You will only need this
tool if you are creating a new project, or are creating or moving pages around
inside an existing project. The `fresh` CLI is not required for most development
or in CI, as `fresh` does not have a build step. To install `fresh`:
```
deno install -A -f --no-check -n fresh -r https://raw.githubusercontent.com/lucacasonato/fresh/main/cli.ts
```
## Creating a new project
Creating a new project can be done as follows:
```
fresh init my-project
```
The first argument passed to `fresh init` (in the above case `my-project`), is
the path to a folder you would like to initalize a `fresh` project in. To
initalize a project in the current working directory, pass `.` as the first
argument.
Now move into the directory you just created / initalized (for example
`cd my-project`).
To learn more about how projects are structured, take a look at the
[Project Structure](./project-structure.md) documentation.
## Running the project locally
You can now run your new project locally using the `deno` CLI:
```
deno task start
```
The `--watch` option in `start` task will cause your script to be reloaded on
any changes to the source. If you do not want this you can omit the `--watch`
option in `deno.json`.
After you have started the project, you can view it in your browser at
http://localhost:8000. You will first get served a JIT server rendered page,
which will get hydrated using client side JS after a few moments.
## Deploying the project to Deno Deploy
To deploy your project you will need to push the source code to either a web
server, or GitHub.com. Fresh currently only supports public GitHub repositories.
For this guide we will use GitHub.com and the Deno Deploy GitHub integration.
First create a new repository on GitHub.com with the same account you signed up
to Deno Deploy with (or an organization you can access from that account). Then
follow the setup instructions for GitHub on your machine, and push your code
into the repository.
Visit your project on dash.deno.com, go to the "Git" tab in the settings, select
your organization and repository, select your production branch (usually
`main`), and finally select your entrypoint module (`main.ts`. You might need to
grant the Deno Deploy app permission to the repository at this point (follow
prompts on the Deno Deploy dashboard).
Your project is now linked to Deno Deploy and will automatically deployed on
every push. Your project now has a URL you can visit to view your site live.
Example: https://fresh.deno.dev
## Next steps
Now that the project is deployed, it is time to make some changes to the default
project. For that read the guides on
[project structure](./project-structure.md), [data fetching](./data-fetching.md)
and [file system routing](./file-system-routing.md)
+4 -8
View File
@@ -17,10 +17,10 @@ static page might need a little bit of JavaScript to power a image carousel or
[islands architecture][islands-architecture]. This refers to a page having
little "islands" of interactivity, in a sea of otherwise static content.
`fresh` embraces this model. All pages are rendered server side, but you can
Fresh embraces this model. All pages are rendered server side, but you can
create "island components" that are _also_ rendered client side. To do this,
`fresh` projects have a special `islands/` folder. The modules in this folder
each encapsulate a single island component. The name of the module should be the
Fresh projects have a special `islands/` folder. The modules in this folder each
encapsulate a single island component. The name of the module should be the
[pascal case][pascal-case] name of the island component. For example a counter
component would be defined in the file `islands/Counter.tsx`. A buy now button
would be defined in the file `islands/BuyNowButton.tsx`.
@@ -87,11 +87,7 @@ export default function Page() {
}
```
Before trying this out, one must run `fresh manifest` again to update the
`fresh.gen.ts` file with the new island component. Just like routes, island
components need to be registered in the manifest file. This means that
`fresh manifest` needs to be run every time a new island component is added,
removed, or renamed.
The page that is rendered on the client now has an interactive countdown.
[islands-architecture]: https://jasonformat.com/islands-architecture
[pascal-case]: https://en.wiktionary.org/wiki/Pascal_case
+18 -14
View File
@@ -1,30 +1,34 @@
---
description: |
Create a new fresh project by running `fresh init`. This scaffolds out the
various files and folders a fresh project needs.
Create a new fresh project by running the fresh project creation tool. This
scaffolds out the various files and folders a fresh project needs.
---
New `fresh` projects can be created with the `fresh` CLI tool. It will scaffold
out a new project with some example files to get you started.
New Fresh projects can be created with using the fresh project creation tool. It
will scaffold out a new project with some example files to get you started.
To create a new project in the directory `"my-project"`, run:
```
$ fresh init my-project
Manifest generated and written to ./my-project/fresh.gen.ts
$ deno run -A --no-check https://raw.githubusercontent.com/lucacasonato/fresh/main/init.ts my-project
The manifest has been generated for 3 routes and 1 islands.
Project created!
Run `deno task start` in the project directory to get started.
```
This will create a directory containing some files and directories. There are 3
This will create a directory containing some files and directories. There are 4
files that are strictly necessary to run a fresh project:
- **`main.ts`**: This is the entrypoint for your project. It is the file that
will you run to start your fresh application. This file doesn't actually need
to be called `main.ts`, but it's best practice to call the entrypoint
`main.ts`.
- **`dev.ts`**: This is the development entrypoint for your project. This is the
file that you run to start your project. This file doesn't need to be called
`dev.ts`, but this is the convention.
- **`main.ts`**: This is the production entrypoint for your project. It is the
file that you link to Deno Deploy. This file doesn't actually need to be
`main.ts`, but this is the convention.
- **`fresh.gen.ts`**: This is the manifest file that contains information about
your routes and islands. This file is automatically generated based on your
`routes/` and `islands/` folder by the `fresh` CLI when running the
`fresh manifest` command.
your routes and islands. This file is automatically generated in development
based on your `routes/` and `islands/` folders.
- **`import_map.json`**: This is an [import map][import-map] that is used to
manage dependencies for the project. This allows for easy importing and
updating of dependencies.
+2 -19
View File
@@ -30,7 +30,7 @@ declare a component that should be rendered every time a user visits the page.
This is done with JSX.
> ℹ️ To learn more about JSX, you can read [this article][jsx] in the React
> documentation. Beware that `fresh` does not use React, but rather
> documentation. Beware that Fresh does not use React, but rather
> [Preact][preact], a lighter weight virtual dom library that works similar to
> React.
@@ -54,24 +54,7 @@ export default function AboutPage() {
> element function. These are just boilerplate. You don't need to know exactly
> what they do - they just ensure that JSX get's rendered correctly.
This alone is not yet enough to actually view the page in the browser though. If
one tries to visit `http://localhost:8000/about`, the server will return a 404
code.
Before `fresh` can render the page, the `fresh.gen.ts` manifest needs to be
updated to reflect this new route. To do this, one can run the `fresh manifest`
command:
```
$ fresh manifest
Manifest generated and written to ./my-project/fresh.gen.ts
```
> ℹ️ Updating the manifest needs to be done every time you add, remove, or
> rename a page. It does not need to be done if just the contents of a page are
> edited.
Now the page will be visible at `http://localhost:8000/about`.
The new page will be visible at `http://localhost:8000/about`.
<!-- You can find more in depth information about routes on the
[_Concepts: Routes_][concepts-routes] documentation page page. The following
-2
View File
@@ -44,8 +44,6 @@ The `PageProps` interface actually contains a bunch of useful properties that
can be used to customize the rendered output. Next to the matched url pattern
parameters, the raw `url`, and the `route` name can also be found in here.
As this is a new page, the manifest needs to be updated again: `fresh manifest`.
Navigating to `https://localhost:8000/greet/Luca` will now render a page showing
"Greetings to you, Luca!".
+4 -4
View File
@@ -11,9 +11,9 @@ for interactivity, but it is much worse for resiliency and user experience as a
whole. Browsers have great built in systems for form submission, revolving
around the HTML `<form>` element.
`fresh` builds the core of it's form submission infrastructure around the native
`<form>` element. This page explains how to use `<form>` in `fresh`, and the
next chapter explains how to progressively enhance your forms with client side
Fresh builds the core of it's form submission infrastructure around the native
`<form>` element. This page explains how to use `<form>` in Fresh, and the next
chapter explains how to progressively enhance your forms with client side
JavaScript to make them more interactive.
The way forms work in the browser, is that they perform a HTML navigation action
@@ -21,7 +21,7 @@ when the user submits the form. In most cases this means that when the form is
submitted, a GET or POST request is sent to the server with the form data, which
then responds with a new page to render.
`fresh` can handle both GET and POST requests through the
Fresh can handle both GET and POST requests through the
[custom handlers][custom-handlers] feature of routes. The handlers can perform
any necessary processing on the form data, and then pass data to the
`ctx.render()` call to render a new page.
+3 -3
View File
@@ -1,16 +1,16 @@
---
description: |
In this chapter of the `fresh` documentation, you'll be introduced to the
In this chapter of the Fresh documentation, you'll be introduced to the
framework. Create a new project, run it locally, edit and create pages, fetch
data, handle user interactions, and deploy it.
---
In this chapter of the `fresh` documentation, you'll be introduced to the
In this chapter of the Fresh documentation, you'll be introduced to the
framework. You'll learn how to create a new project, run it locally, edit and
create pages, fetch data, handle user interactions, and how to then deploy the
project to [Deno Deploy][deno-deploy].
The documentation assumes you have Deno 1.12.0 or later installed.
The documentation assumes you have Deno 1.22.3 or later installed.
To install Deno, follow the
[installation instructions in the Deno manual][manual-installation].
-40
View File
@@ -1,40 +0,0 @@
---
description: |
Install the `fresh` CLI tool to scaffold new projects and generate the fresh
manifest. The CLI tool can be installed with `deno install`.
---
`fresh` comes with a CLI tool, conveniently also called `fresh`. It is used to
scaffold new projects and generate the fresh manifest (more on that later). This
tool is only needed to create a new project, or to re-generate the fresh
manifest after you have added, removed, or renamed a pages or islands in an
existing project.
The CLI tool is not required for most development or in CI, as `fresh` does not
have a build step. This means that if you are only doing some light editing on
an existing project, you do not need to install or use the `fresh` CLI.
To install the `fresh` CLI:
```
deno install -A -f --no-check -n fresh -r https://raw.githubusercontent.com/lucacasonato/fresh/main/cli.ts
```
The tool should now be available as `fresh` in your terminal. To try out if
installation was successful you can just run `fresh` without any arguments:
```
$ fresh
fresh 0.1.0
The next-gen web framework.
To initalize a new project:
fresh init ./myproject
To (re-)generate the manifest file:
fresh manifest
SUBCOMMANDS:
init Initalize a fresh project
manifest (Re-)generate the manifest file
```
+10 -10
View File
@@ -1,30 +1,30 @@
---
description: |
`fresh` is a full stack modern web framework for JavaScript and TypeScript
Fresh is a full stack modern web framework for JavaScript and TypeScript
developers, designed to make it trivial to create high-quality, performant,
and personalized web applications.
---
`fresh` is a full stack modern web framework for JavaScript and TypeScript
Fresh is a full stack modern web framework for JavaScript and TypeScript
developers, designed to make it trivial to create high-quality, performant, and
personalized web applications. You can use it to create your home page, a blog,
a large web application like GitHub or Twitter, or anything else you can think
of.
At its core, `fresh` is a combination of a routing framework and templating
engine that renders pages on demand, on the server. In addition to this
just-in-time (JIT) rendering on the server, `fresh` also provides an interface
for seamlessly rendering some components on the client for maximum
interactivity. The framework uses [Preact][preact] and JSX for rendering and
templating on both the server and the client.
At its core, Fresh is a combination of a routing framework and templating engine
that renders pages on demand, on the server. In addition to this just-in-time
(JIT) rendering on the server, Fresh also provides an interface for seamlessly
rendering some components on the client for maximum interactivity. The framework
uses [Preact][preact] and JSX for rendering and templating on both the server
and the client.
`fresh` also does not have a build step. The code you write is also directly the
Fresh also does not have a build step. The code you write is also directly the
code that is run on the server, and the code that is executed on the client. Any
necessary transpilation of TypeScript or JSX to plain JavaScript is done on the
fly, just when it is needed. This allows for insanely fast iteration loops, and
very very fast deployments.
`fresh` projects can be deployed manually to any platform with `deno`, but it is
Fresh projects can be deployed manually to any platform with `deno`, but it is
intended to be deployed to an edge runtime like [Deno Deploy][deno-deploy] for
the best experience.
-24
View File
@@ -1,24 +0,0 @@
# Project strcture
Initalizing a fresh project will generate multiple files. For now the most
important files are `main.ts`, `fresh.gen.ts`, the route modules in the `routes`
subdirectory, and the islands in the `islands` subdirectory.
The `main.ts` file is the entrypoint to your project. It is the file you link in
Deno Deploy, or run locally with the `deno` CLI.
The `fresh.gen.ts` file is a manifest of all routes and islands in a project.
When moving around or creating new pages or islands, this file needs to be
updated. This is done automatically when you run the `fresh manifest` command.
You never have to edit this file manually.
The files in the `routes` subfolder describe the HTML pages and API endpoints of
your application. All routes can act as both API endpoints and dynamic HTML
pages. More info about how the `routes` subdirectory works can be found on the
[file system routing](./file-system-routing.md) page.
Finally there is a `deps.client.ts` and `deps.server.ts`. These files are the
central location for all external imports. The `deps.client.ts` file is used for
runtime dependencies (ones that are used both in routes and islands), and the
`deps.server.ts` file is used for server only dependencies (for example ones
used in API routes or middleware).
-1
View File
@@ -5,7 +5,6 @@
"getting-started": {
"title": "Getting Started",
"pages": [
["installation", "Installation"],
["create-a-project", "Create a project"],
["running-locally", "Running locally"],
["create-a-route", "Create a route"],