chore: release 1.5.0 (#1899)
This commit is contained in:
@@ -1,106 +0,0 @@
|
||||
---
|
||||
description: |
|
||||
Add a global app wrapper to provide common meta tags or context for application routes.
|
||||
---
|
||||
|
||||
An app wrapper is defined in an `_app.tsx` file in `routes/` folder and is
|
||||
typically used to create the outer structure of an HTML document. It must
|
||||
contain a default export that is a regular Preact component. Only one such
|
||||
wrapper is allowed per application.
|
||||
|
||||
The component to be wrapped is received via props, in addition to a few other
|
||||
things. This allows for the introduction of a global container functioning as a
|
||||
template which can be conditioned based on state and params. Note that any state
|
||||
set by middleware is available via `props.state`.
|
||||
|
||||
```tsx routes/_app.tsx
|
||||
import { AppProps } from "$fresh/server.ts";
|
||||
|
||||
export default function App({ Component, state }: AppProps) {
|
||||
// do something with state here
|
||||
return (
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>My Fresh app</title>
|
||||
</head>
|
||||
<body>
|
||||
<Component />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## Async app wrapper
|
||||
|
||||
Similar to routes and layouts, the app wrapper can be made asynchronous. This
|
||||
changes the function signature so that the first argument is the `Request`
|
||||
instance and the second one is the `AppContext`.
|
||||
|
||||
```tsx routes/_app.tsx
|
||||
import { AppContext } from "$fresh/server.ts";
|
||||
|
||||
export default async function App(req: Request, ctx: AppContext) {
|
||||
const data = await loadData();
|
||||
|
||||
return (
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>My Fresh app</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello {data.name}</h1>
|
||||
<ctx.Component />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
### Define helper
|
||||
|
||||
To make it quicker to type the async app wrapper, Fresh includes a `defineApp`
|
||||
helper which already infers the correct types for you.
|
||||
|
||||
```tsx routes/_app.tsx
|
||||
import { defineApp } from "$fresh/server.ts";
|
||||
|
||||
export default defineApp(async (req, ctx) => {
|
||||
const data = await loadData();
|
||||
|
||||
return (
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>My Fresh app</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello {data.name}</h1>
|
||||
<ctx.Component />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
});
|
||||
```
|
||||
|
||||
## Disabling the app wrapper
|
||||
|
||||
Rendering the app wrapper can be skipped on a route or layout basis. To do that,
|
||||
set `skipAppWrapper: true` to the layout or route config.
|
||||
|
||||
```tsx routes/my-special-route.tsx
|
||||
import { RouteConfig } from "$fresh/server.ts";
|
||||
|
||||
export const config: RouteConfig = {
|
||||
skipAppWrapper: true, // Skip the app wrapper during rendering
|
||||
};
|
||||
|
||||
export default function Page() {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
@@ -1,198 +0,0 @@
|
||||
---
|
||||
description: |
|
||||
Partials allow areas of a page to be updated without causing the browser to reload the page. They enable optimized fine grained UI updates and can be used to do client-side navigation.
|
||||
---
|
||||
|
||||
Partials allow areas of the page to be updated with new content by the server
|
||||
without causing the browser to reload the page. They make your website feel more
|
||||
app-like because only the parts of the page that need to be updated will
|
||||
updated.
|
||||
|
||||
## Enabling partials
|
||||
|
||||
Partials are enabled by adding a `f-client-nav` attribute to an HTML element and
|
||||
wrapping one or more areas in the page with a
|
||||
`<Partial name="my-partial">`-component.
|
||||
|
||||
The quickest way to get started is to enable partials for every page in
|
||||
`routes/_app.tsx` by making the following changes.
|
||||
|
||||
```diff routes/_app.tsx
|
||||
import { AppProps } from "$fresh/server.ts";
|
||||
+ import { Partial } from "$fresh/runtime.ts";
|
||||
|
||||
export default function App({ Component }: AppProps) {
|
||||
return (
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>My Fresh app</title>
|
||||
</head>
|
||||
- <body>
|
||||
+ <body f-client-nav>
|
||||
+ <Partial name="body">
|
||||
<Component />
|
||||
+ </Partial>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
By adding the `f-client-nav` attribute, we enable partials for every element
|
||||
beneath the `<body>`-tag. To mark an area of the page as a partial we wrap it
|
||||
with a `<Partial>`-component with a unique name.
|
||||
|
||||
Behind the scenes, when the user clicks an `<a>`-tag, Fresh fetches the new page
|
||||
and only pulls out the relevant content out of the HTML response. When it finds
|
||||
a matching partial area it will update the content inside the partial.
|
||||
|
||||
> ℹ️ The `name` prop of the `<Partial>` component is expected to be unique among
|
||||
> Partials. That's how Fresh knows which parts of the response need to go on the
|
||||
> current page.
|
||||
|
||||
> ℹ️ Passing `f-client-nav={false}` disables client side navigation and partials
|
||||
> for the current elements and the elements below it.
|
||||
|
||||
### Optimizing partial requests
|
||||
|
||||
By default, with `f-client-nav` set, Fresh fetches the full next page and only
|
||||
picks out the relevant parts of the response. We can optimize this pattern
|
||||
further by only rendering the parts we need, instead of always rendering the
|
||||
full page. This is done by adding the `f-partial` attribute to a link.
|
||||
|
||||
```diff
|
||||
- <a href="/docs/routes">Routes</a>
|
||||
+ <a href="/docs/routes" f-partial="/partials/docs/routes">Routes</a>
|
||||
```
|
||||
|
||||
When the `f-partial` attribute is present, Fresh will navigate to the page URL
|
||||
defined in the `href` attribute, but fetch the updated UI from the URL specified
|
||||
in `f-partial` instead. This can be a highly optimized route that only delivers
|
||||
the content you care about.
|
||||
|
||||
Let's use a typical documentation page layout as an example. It often features a
|
||||
main content area and a sidebar of links to switch between pages of the
|
||||
documentation (marked green here).
|
||||
|
||||

|
||||
|
||||
The code for such a page (excluding styling) might look like this:
|
||||
|
||||
```tsx routes/docs/[id].tsx
|
||||
export default defineRoute(async (req, ctx) => {
|
||||
const content = await loadContent(props.params.id);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<aside>
|
||||
<a href="/docs/page1">Page 1</a>
|
||||
<a href="/docs/page2">Page 2</a>
|
||||
</aside>
|
||||
<Partial name="docs-content">
|
||||
{content}
|
||||
</Partial>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
```
|
||||
|
||||
An optimal route that only renders the content instead of the outer layout with
|
||||
the sidebar might look like this respectively.
|
||||
|
||||
```tsx partials/docs/[id].tsx
|
||||
import { defineRoute, RouteConfig } from "$fresh/server.ts";
|
||||
import { Partial } from "$fresh/runtime.ts";
|
||||
|
||||
// We only want to render the content, so disable
|
||||
// the `_app.tsx` template as well as any potentially
|
||||
// inherited layouts
|
||||
export const config: RouteConfig = {
|
||||
skipAppWrapper: true,
|
||||
skipInheritedLayouts: true,
|
||||
};
|
||||
|
||||
export default defineRoute(async (req, ctx) => {
|
||||
const content = await loadContent(props.params.id);
|
||||
|
||||
// Only render the new content
|
||||
return (
|
||||
<Partial name="docs-content">
|
||||
{content}
|
||||
</Partial>
|
||||
);
|
||||
});
|
||||
```
|
||||
|
||||
By adding the `f-partial` attribute we tell Fresh to fetch the content from our
|
||||
newly added `/partials/docs/[id].tsx` route.
|
||||
|
||||
```diff routes/docs/[id].tsx
|
||||
<aside>
|
||||
- <a href="/docs/page1">Page 1</a>
|
||||
- <a href="/docs/page2">Page 2</a>
|
||||
+ <a href="/docs/page1" f-partial="/partials/docs/page1">Page 1</a>
|
||||
+ <a href="/docs/page2" f-partial="/partials/docs/page2">Page 2</a>
|
||||
</aside>
|
||||
```
|
||||
|
||||
With this in place, Fresh will navigate to the new page when clicking any of the
|
||||
two links and _only_ load the content rendered by our optimized partial route.
|
||||
|
||||
> Currently, `f-partial` is scoped to `<a>`, `<button>` and `<form>` elements.
|
||||
> This might be extended to more elements in the future.
|
||||
|
||||
## Sending multiple Partials at the same time
|
||||
|
||||
A neat aspect of partials in Fresh is that a response can return as many
|
||||
partials as desired. That way you can update multiple unrelated areas on your
|
||||
page in one single HTTP response. A scenario where this is useful are online
|
||||
shops for example.
|
||||
|
||||
```tsx routes/partials/cart
|
||||
export default function AddToCartPartial() {
|
||||
return (
|
||||
<>
|
||||
<Partial name="cart-items" mode="append">
|
||||
{/* Render the new cart item here */}
|
||||
</Partial>
|
||||
<Partial name="total-price">
|
||||
<p>Total: {totalPrice} €</p>
|
||||
</Partial>
|
||||
</>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
Both partials will be applied to the current page.
|
||||
|
||||
## Replacement mode
|
||||
|
||||
By default the whole content inside a partial will be replaced, but there are
|
||||
scenarios where you want to prepend or append new content instead. This can be
|
||||
achieved by adding the `mode` prop to a `Partial` component.
|
||||
|
||||
- `replace` - Swap out the content of the existing partial (default)
|
||||
- `prepend` - Insert the new content before the existing content
|
||||
- `append` - Insert the new content after the existing content
|
||||
|
||||
Personally, we’ve found that the `append` mode is really useful when you have an
|
||||
UI which displays log messages or similar list-like data.
|
||||
|
||||
```tsx
|
||||
export default function LogView() {
|
||||
const lines = getNewLogLines();
|
||||
|
||||
return (
|
||||
<Partial name="logs-list" mode="append">
|
||||
{lines.map((line) => {
|
||||
return <li key={line}>{line}</li>;
|
||||
})}
|
||||
</Partial>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
> ℹ️ When picking the `prepend` or `append` mode, make sure to add keys to the
|
||||
> elements.
|
||||
@@ -1,128 +0,0 @@
|
||||
---
|
||||
description: |
|
||||
New versions of Fresh are regularly released. This page explains how to update your project.
|
||||
---
|
||||
|
||||
Fresh consists of multiple pieces which are independently versioned and
|
||||
released.
|
||||
|
||||
- Fresh (https://deno.land/x/fresh)
|
||||
- Preact (https://esm.sh/preact)
|
||||
- preact-render-to-string (https://esm.sh/preact-render-to-string)
|
||||
|
||||
Some plugins also have their own dependencies that can be updated independently.
|
||||
|
||||
- Twind (https://esm.sh/twind) (for the twind plugin)
|
||||
|
||||
For the most part these pieces can be updated independently. Certain versions of
|
||||
Fresh may require a minimum version of a given dependency. This is documented
|
||||
below.
|
||||
|
||||
| Fresh version | Preact | preact-render-to-string | Deno |
|
||||
| ------------- | ----------------- | ----------------------- | --------- |
|
||||
| 1.0.0-1.0.2 | >=10.8.1 <11.0.0 | >=5.2.0 <6.0.0 | >= 1.23.0 |
|
||||
| 1.1.0-1.1.5 | >=10.8.1 <11.0.0 | >=5.2.0 <6.0.0 | >= 1.25.0 |
|
||||
| 1.2.0 | >=10.15.0 <11.0.0 | >=6.1.0 | >= 1.25.0 |
|
||||
|
||||
## Updating dependencies
|
||||
|
||||
To update your dependencies, you have two options:
|
||||
|
||||
- Run the Fresh updater to update your project dependencies.
|
||||
- Manually update the dependency versions in your `deno.json` file.
|
||||
|
||||
### Auto updater
|
||||
|
||||
The auto updater is a command line tool that will update your project's
|
||||
`deno.json` file to the latest versions of Fresh and its dependencies. It may
|
||||
also contain code mods for your project that will update your code to the latest
|
||||
recommended patterns for Fresh projects.
|
||||
|
||||
To run the auto updater, run the following command from the root of your
|
||||
project:
|
||||
|
||||
```sh Terminal
|
||||
$ deno run -A -r https://fresh.deno.dev/update
|
||||
```
|
||||
|
||||
You will be prompted to confirm the changes that will be made to your project.
|
||||
|
||||
### Manual update
|
||||
|
||||
To manually update your project's dependencies, you can edit the `deno.json`
|
||||
file in the root of your projects directory. Dependency versions are encoded
|
||||
into the URLs in this file. For example, here is how to update a project from
|
||||
Fresh 1.0.2 to 1.1.3, and update Preact to the latest version:
|
||||
|
||||
```diff deno.json
|
||||
{
|
||||
"imports": {
|
||||
- "$fresh/": "https://deno.land/x/fresh@1.0.2/",
|
||||
+ "$fresh/": "https://deno.land/x/fresh@1.1.5/",
|
||||
|
||||
- "preact": "https://esm.sh/preact@10.8.1",
|
||||
- "preact/": "https://esm.sh/preact@10.8.1/",
|
||||
+ "preact": "https://esm.sh/preact@10.11.0",
|
||||
+ "preact/": "https://esm.sh/preact@10.11.0/",
|
||||
|
||||
- "preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.0",
|
||||
+ "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
|
||||
|
||||
"twind": "https://esm.sh/twind@0.16.17",
|
||||
"twind/": "https://esm.sh/twind@0.16.17/"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Automatic update checks
|
||||
|
||||
Fresh will periodically check if a new Fresh version is available if it's
|
||||
running outside of CI. This happens once per day and can be disabled by setting
|
||||
the `FRESH_NO_UPDATE_CHECK=true` environment variable.
|
||||
|
||||
## Code mods
|
||||
|
||||
Code mods are small scripts that can be run to update your project's code to
|
||||
match the latest recommended patterns for Fresh projects. Code mods can be run
|
||||
through the auto updater. Sometimes the code mod can not cover all cases, so you
|
||||
may need to manually update some code. This section explains the code mods
|
||||
currently available.
|
||||
|
||||
### Classical JSX -> Automatic JSX
|
||||
|
||||
> This code mod is only available in Fresh 1.1.0 and above.
|
||||
|
||||
The classical JSX transform that relies on a `/** @jsx h */` pragma is no longer
|
||||
the recommended way to use JSX in Fresh projects. Instead, starting with version
|
||||
1.1.0, Fresh projects should use the automatic JSX transform that requires no
|
||||
JSX pragma or preact import.
|
||||
|
||||
```diff
|
||||
- /** @jsx h */
|
||||
- import { h } from "preact";
|
||||
|
||||
export default function Page() {
|
||||
return <div>Hello world!</div>;
|
||||
}
|
||||
```
|
||||
|
||||
This code mod will update your deno.json file to include the relevant compiler
|
||||
options to enable the automatic JSX transform. It will then go through your
|
||||
project and remove any `/** @jsx h */` pragmas and `import { h } from "preact"`
|
||||
statements.
|
||||
|
||||
### Classic twind -> Twind plugin
|
||||
|
||||
> This code mod is only available in Fresh 1.1.0 and above.
|
||||
|
||||
Fresh version 1.1.0 introduced a new plugin for using twind with Fresh. This
|
||||
plugin is much nicer to use than the raw twind integration that was previously
|
||||
available.
|
||||
|
||||
This code mod will update your project to use the new twind plugin. It will
|
||||
update your `main.ts` file to import the twind plugin and add it to the plugins
|
||||
array. It will also update your files to remove many unnecessary uses of the
|
||||
`tw` function, and remove unnecessary twind imports. While the code mod can
|
||||
handle most cases, you may need to manually update some code. Additionally you
|
||||
will need to manually update your `twind.config.ts` if you use a custom
|
||||
configuration.
|
||||
@@ -1,43 +0,0 @@
|
||||
---
|
||||
description: |
|
||||
Style active links with ease in Fresh
|
||||
---
|
||||
|
||||
When Fresh renders an `<a>`-element, it will automatically add a
|
||||
[data attribute](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes)
|
||||
to the link which can be used for styling purposes. Fresh will add one of the
|
||||
two following attributes:
|
||||
|
||||
- `data-current` - Added to links with an exact path match
|
||||
- `data-ancestor` - Added to links which partially match the current URL
|
||||
|
||||
## Styling with CSS
|
||||
|
||||
`data-*` attributes can be natively styled in CSS via attribute selectors.
|
||||
|
||||
```css
|
||||
/* Give links pointing to the current page a green color */
|
||||
a[data-current] {
|
||||
color: green;
|
||||
}
|
||||
|
||||
/* Color all ancestor links of the current page */
|
||||
a[data-ancestor] {
|
||||
color: peachpuff;
|
||||
}
|
||||
```
|
||||
|
||||
## Tailwind / Twind
|
||||
|
||||
In tailwind or tailwind like styling solutions you can style `data-*`-attributes
|
||||
by prepending the classes with `[data-current]:`
|
||||
|
||||
```tsx
|
||||
function Menu() {
|
||||
return (
|
||||
<a href="/foo" class="[data-current]:text-green-600">
|
||||
link to some page
|
||||
</a>
|
||||
);
|
||||
}
|
||||
```
|
||||
@@ -1,79 +0,0 @@
|
||||
---
|
||||
description: |
|
||||
Add components like <title> or <meta> to a <head> tag using Fresh's <Head> component.
|
||||
---
|
||||
|
||||
We can use the `<Head />` component in `$fresh/runtime.ts` to add elements as
|
||||
children of the `<head>` element. By adding elements as children of Fresh's
|
||||
`<Head />` tag, these automatically get injected into the `<head>` element of
|
||||
the web page. Some uses include:
|
||||
|
||||
- Setting the document title using `<title>`
|
||||
- Specifying page metadata using `<meta>`
|
||||
- Linking to resources like stylesheets using `<link>`
|
||||
- Including third-party JavaScript code using `<script>`
|
||||
|
||||
```tsx routes/index.tsx
|
||||
import { Head } from "$fresh/runtime.ts";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Fresh App</title>
|
||||
<meta
|
||||
name="description"
|
||||
content="This is a brief description of Fresh"
|
||||
/>
|
||||
<link rel="stylesheet" href="styles.css" />
|
||||
<script src="script.js"></script>
|
||||
</Head>
|
||||
<div class="p-4 mx-auto max-w-screen-md">
|
||||
<h1>Hello World</h1>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## Avoiding duplicate tags
|
||||
|
||||
You might end up with duplicate tags, when multiple `<Head />` components are
|
||||
rendered on the same page. This can happen when you render `<Head />` in a route
|
||||
and another `<Head />` in another component for example.
|
||||
|
||||
```tsx
|
||||
// routes/page-a.tsx
|
||||
<Head>
|
||||
<meta name="og:title" content="This is a title" />
|
||||
</Head>
|
||||
|
||||
// components/MyTitle.tsx
|
||||
<Head>
|
||||
<meta name="og:title" content="Other title" />
|
||||
</Head>
|
||||
```
|
||||
|
||||
To ensure that the tag is not duplicated, Fresh supports setting the `key` prop.
|
||||
By giving matching elements the same `key` prop, only the last one will be
|
||||
rendered.
|
||||
|
||||
```diff
|
||||
// routes/page-a.tsx
|
||||
<Head>
|
||||
- <meta name="og:title" content="This is a title" />
|
||||
+ <meta name="og:title" content="This is a title" key="title" />
|
||||
</Head>
|
||||
|
||||
// components/MyTitle.tsx
|
||||
<Head>
|
||||
- <meta name="og:title" content="Other title" />
|
||||
+ <meta name="og:title" content="Other title" key="title" />
|
||||
</Head>
|
||||
```
|
||||
|
||||
The rendered page will only include the `<meta>`-tag with `"Other title"`.
|
||||
|
||||
> [info]: The `<title>`-tag is automatically deduplicated, even without a `key`
|
||||
> prop.
|
||||
Reference in New Issue
Block a user