feat: a new "island" based architechture (#97)
This commit is contained in:
+35
-32
@@ -4,35 +4,50 @@ 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 fetch in server render using suspense. This
|
||||
allows render to be interupted to wait for a promise to resolve. Once the
|
||||
promise resolves, the render is retried.
|
||||
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 -->.
|
||||
|
||||
## `useData` hook
|
||||
## Fetching data in a `handler`
|
||||
|
||||
To make using asynchronous data sources during server render super easy, fresh
|
||||
exposes the `useData` hook. This hook takes a key and a fetcher function that
|
||||
can return a promise. It is invoked once on the server. The server will wait
|
||||
until the promise resolves, at which point it will render the page with the new
|
||||
data.
|
||||
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 fetcher for the `useData` hook is never run on the client. Instead the data
|
||||
that was computed on the server during render will be used.
|
||||
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 `useData` hook where the fetcher fetches the URL
|
||||
provided as the key as JSON:
|
||||
Here is a usage example of a `handler` fetches a URL as JSON and provides the
|
||||
resulting data to the template:
|
||||
|
||||
```tsx
|
||||
import { h, PageProps, useData } from "../deps.ts";
|
||||
import { h, Handlers, PageProps, useData } from "../deps.ts";
|
||||
|
||||
export default function Page(_props: PageProps) {
|
||||
const info = useData("https://cdn.deno.land/std/meta/versions.json", fetcher);
|
||||
interface ModuleInfo {
|
||||
latest: string;
|
||||
versions: string[];
|
||||
}
|
||||
|
||||
if (info === null) {
|
||||
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 } = info;
|
||||
const { latest, versions } = data;
|
||||
return (
|
||||
<p>
|
||||
The Deno standard library has {versions.length} versions, with {latest}
|
||||
@@ -41,19 +56,7 @@ export default function Page(_props: PageProps) {
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
interface ModuleInfo {
|
||||
latest: string;
|
||||
versions: string[];
|
||||
}
|
||||
|
||||
async function fetcher(url: string): Promise<ModuleInfo | null> {
|
||||
const resp = await fetch(url);
|
||||
if (resp.status === 200) return resp.json();
|
||||
return null;
|
||||
}
|
||||
```
|
||||
|
||||
The `useData` can not generate data client side. Calling `useData` on the client
|
||||
with a key that no data was generated for on the server will result in `useData`
|
||||
throwing an error.
|
||||
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.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# File system routing
|
||||
|
||||
The naming and location of files in the `pages` folder is important because
|
||||
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.
|
||||
|
||||
|
||||
@@ -66,13 +66,11 @@ 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.
|
||||
|
||||
Now go back to GitHub and open the `main.ts` file that you pushed. Copy the URL
|
||||
in the address bar. It should look something like this:
|
||||
`https://github.com/lucacasonato/fresh/blob/main/example/main.ts`. Visit your
|
||||
project on dash.deno.com, enter the URL you copied into the
|
||||
`Link GitHub repository` input box in the project settings, and press `Link`.
|
||||
You might need to grant the Deno Deploy app permission to the repository at this
|
||||
point (follow prompts on the Deno Deploy dashboard).
|
||||
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.
|
||||
|
||||
+14
-14
@@ -1,24 +1,24 @@
|
||||
# Project strcture
|
||||
|
||||
Initalizing a fresh project will generate multiple files. For now the most
|
||||
important files are `main.ts`, `routes.gen.ts`, and the pages and api routes in
|
||||
the `pages` subfolder.
|
||||
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 `routes.gen.ts` file is a manifest of all pages and api routes in a project.
|
||||
When moving around or creating new pages, this file needs to be updated. This is
|
||||
done automatically when you run the `fresh routes` command. You never have to
|
||||
edit this file manually.
|
||||
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 `pages` subfolder describe the HTML pages and API endpoints of
|
||||
your application. All pages can act as both API endpoints and possibly dynamic
|
||||
HTML pages. More info about how the `pages` subfolder works can be found on the
|
||||
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.ts` and `api_deps.ts`. These files are the central
|
||||
location for all external imports. The `deps.ts` file is used for runtime
|
||||
dependencies (ones that are used both on the server and on the client), and the
|
||||
`api_deps.ts` file is used for server only dependencies (for example ones used
|
||||
in API routes).
|
||||
Finally there is a `client_deps.ts` and `server_deps.ts`. These files are the
|
||||
central location for all external imports. The `client_deps.ts` file is used for
|
||||
runtime dependencies (ones that are used both in routes and islands), and the
|
||||
`server_deps.ts` file is used for server only dependencies (for example ones
|
||||
used in API routes or middleware).
|
||||
|
||||
Reference in New Issue
Block a user