chore: release 1.6.0 (#2113)

This commit is contained in:
Marvin Hagemeister
2023-12-01 16:20:40 +01:00
committed by GitHub
parent bd372b9535
commit dace008611
12 changed files with 111 additions and 635 deletions
@@ -15,6 +15,9 @@ static file and can serve it immediately without any further processing. On
pages with islands, having to do no processing greatly speeds up page load
times.
Plugins can build static assets during ahead-of-time builds. This can be used to
preprocess or generate CSS files, for example.
## Creating an optimized build
To have Fresh optimize all the assets, run one of the following commands:
@@ -30,6 +33,10 @@ This will create a `_fresh` folder in the project directory. That folder
contains the optimized assets and a `snapshot.json` file which includes some
metadata for Fresh.
Any other static files generated by plugins will be stored in the
`_fresh/static` subfolder. They will be served the same as other
[static files](/docs/concepts/static-files.md).
> [info]: The `_fresh` folder should not be committed to the repository. Add an
> entry in the `.gitignore` file to ensure that it is not committed. Create that
> file at the root of your git repository if not present.
+13
View File
@@ -54,6 +54,19 @@ export default function BlogpostPage({ data }) {
}
```
This can also be achieved by throwing an error, if you're uninterested in
passing specific data to your 404 page:
```tsx
import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
GET(_req, _ctx) {
throw new Deno.errors.NotFound();
},
};
```
### 500: Internal Server Error
The 500 page can be customized by creating a `_500.tsx` file in the `routes/`
+53 -1
View File
@@ -80,12 +80,17 @@ client.
The `render` hook needs to synchronously return a
[`PluginRenderResult`](https://deno.land/x/fresh/server.ts?s=PluginRenderResult)
object. Additional CSS and JS modules can be added to be injected into the page
by adding them to `styles` and `scripts` arrays in this object.
by adding them to `styles`, `links` and `scripts` arrays in this object. The
plugin can also replace the the HTML in side the `<body>`-element of the page by
including a `htmlText` string in this object.
`styles` are injected into the `<head>` of the page as inline CSS. Each entry
can define the CSS text to inject, as well as an optional `id` for the style
tag, and an optional `media` attribute for the style tag.
`links` are injected into the `<head>` of the page as `<link>` tags. A link tag
is created for each entry, with attributes from the entry's properties.
`scripts` define JavaScript/TypeScript modules to be injected into the page. The
possibly loaded modules need to be defined up front in the `Plugin#entrypoints`
property. Each defined module must be a JavaScript/TypeScript module that has a
@@ -123,6 +128,26 @@ rendering, plugin `render` hooks, and the
[`RenderFunction`](https://deno.land/x/fresh/server.ts?s=RenderFunction) that
may be provided to Fresh's `start` entrypoint in the `main.ts` file.
### Hook: `buildStart`
This hook is run at the start of the Fresh
[ahead-of-time build task](/docs/concepts/ahead-of-time-builds). It may be
synchronous or asynchronous.
The build start hook is called with the
[`ResolvedFreshConfig`](https://deno.land/x/fresh/src/server/types.ts?s=ResolvedFreshConfig)
object, which contains the full Fresh configuration.
This hook may be used to generate precompiled static assets. Any files saved to
the `static` subfolder of `config.build.outDir` (typically `_fresh`) will be
served the same as other [static files](/docs/concepts/static-files).
### Hook: `buildEnd`
This hook is run at the end of the Fresh
[ahead-of-time build task](/docs/concepts/ahead-of-time-builds). It may be
synchronous or asynchronous.
### Routes and Middlewares
You can create routes and middlewares that get loaded and rendered like the
@@ -138,3 +163,30 @@ For more examples see the [Concepts: Routing](/docs/concepts/routing) page.
To create a middleware you need to create a `MiddlewareHandler` function.
And to create a route you can create both a Handler and/or component.
A very basic example can be found
[here](https://github.com/denoland/fresh/blob/main/tests/fixture_plugin/utils/route-plugin.ts).
### Islands
Islands from plugins can be loaded by specifying a list of file paths in your
plugin. Those files will be treated by Fresh as if they had been placed inside
the `islands/` directory. They will be processed and bundled for the browser in
the same way.
```tsx my-island-plugin.ts
import { Plugin } from "$fresh/server.ts";
export default function myIslandPlugin(): Plugin {
return {
name: "my-island-plugin",
islands: {
baseLocation: import.meta.url,
paths: [
"./plugin/MyPluginIsland.tsx",
"./plugin/OtherPluginIsland.tsx",
],
},
};
}
```
@@ -65,6 +65,12 @@ export interface RouterOptions {
* @default {/(?:[^/]*_|[^/]*\.|)test\.(?:ts|tsx|mts|js|mjs|jsx|)\/*$/}
*/
ignoreFilePattern?: RegExp;
/**
* Serve fresh from a base path instead of from the root.
* "/foo/bar" -> http://localhost:8000/foo/bar
* @default {undefined}
*/
basePath?: string;
}
```
@@ -135,6 +141,30 @@ await start(manifest, { router: { trailingSlash: true } });
By default Fresh ignores test files which are co-located next routes and
islands. If you want, you can change the pattern Fresh uses ignore these files
### basePath
This setting allows you to serve a Fresh app from sub-path of a domain. A value
of `/foo/bar` would serve the app from `http://localhost:8000/foo/bar` instead
of `http://localhost:8000/` for example.
The `basePath` will be automatically applied to absolute links in your app. For
example, when the `basePath` is `/foo/bar`, linking to `/about` will
automatically become `/foo/bar/about`.
```jsx
<a href="/about">About</a>;
```
Rendered HTML:
```html
<a href="/foo/bar/about">About</a>
```
The `basePath` is also applied to the `src` and `srcset` attribute of
`<img>`-tags, the `href` attribute of `<link>` and the `src` attribute of
`<script>` tags.
## Server
Now that Deno has stabilized [Deno.serve](https://deno.land/api?s=Deno.serve)
@@ -0,0 +1,136 @@
---
description: |
Migrating from twind to Tailwind CSS
---
Starting with version 1.6 Fresh comes with a proper Tailwind CSS plugin out of
the box. When you create a new Fresh project, checking the Tailwind CSS option
will now install the Tailwind CSS plugin instead of twind like it did before.
## Migrating to Tailwind CSS
1. Create a `<project>/tailwind.config.ts` file in your project folder:
```ts tailwind.config.ts
import { type Config } from "tailwindcss";
export default {
content: [
"{routes,islands,components}/**/*.{ts,tsx}",
],
} as Config;
```
2. Create a css file in your static directory `<project>/static/styles.css`:
```css static/styles.css
@tailwind base;
@tailwind components;
@tailwind utilities;
```
3. Add the created stylesheet in your HTML in `<project>/routes/_app.tsx`:
```diff routes/_app.tsx
import { AppProps } from "$fresh/server.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 Project</title>
+ <link rel="stylesheet" href="/styles.css" />
</head>
<body>
<Component />
</body>
</html>
);
}
```
4. Replace the `twind` plugin with `tailwind`
```diff fresh.config.ts
import { defineConfig } from "$fresh/server.ts";
- import twind from "$fresh/plugins/twind.ts";
+ import tailwind from "$fresh/plugins/tailwind.ts";
export default defineConfig({
- plugins: [twind()],
+ plugins: [tailwind()],
});
```
5. Update your `deno.json` file and add the following `tailwindcss` imports. To
make the
[vscode Tailwind CSS extension](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss)
work, we also need to set `"nodeModulesDir": true`. This will create a
`node_modules` directory in your project folder:
```diff deno.json
{
+ "nodeModulesDir": true,
"imports": {
"$fresh/": "https://deno.land/x/fresh@1.5.2/",
"preact": "https://esm.sh/preact@10.19.2",
"preact/": "https://esm.sh/preact@10.19.2/",
- "twind": "https://esm.sh/twind@0.16.19",
- "twind/": "https://esm.sh/twind@0.16.19/",
+ "tailwindcss": "npm:tailwindcss@3.3.5",
+ "tailwindcss/": "npm:/tailwindcss@3.3.5/",
+ "tailwindcss/plugin": "npm:/tailwindcss@3.3.5/plugin.js"
}
}
```
6. Add `node_modules` to your `.gitignore` or create one if the file is not
present in your project root directory.
```diff .gitignore
+ node_modules/
```
7. Set up [Ahead of Time builds](/docs/concepts/ahead-of-time-builds) if you
haven't already.
That's it! Now you can use Tailwind CSS in your project.
> [info]: If you're a vscode user, be sure to install the
> [official Tailwind CSS extension](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss)
> to get full intellisense support. For it to work you also need to set
> `"nodeModulesDir": true` in your `deno.json`.
> [warn]: Tailwind CSS doesn't support the grouping syntax from twind:
> `text(lg uppercase gray-100)`. These need to be rewritten to their expanded
> values like `text-lg uppercase text-gray-100`.
## Frequently Asked Questions (FAQ)
### What are the differences between twind and Tailwind CSS?
Twind is a project that tries to enable you to use Tailwind-like styling
capabilities in a single script that can also be used in the browser. The key
difference between the two is that twind generates CSS on the fly on every
request and was shipped to the browser to make newly generated classes by
islands work in Fresh. Overall, this wasn't an ideal setup for building
performant sites.
In contrast to that, Tailwind CSS extracts generates the resulting CSS file
ahead of time, which only happens once per deployment. There is no runtime
component needed, which makes your Fresh project respond faster to requests.
During the Tailwind CSS v2 days twind pushed a lot of great ideas like allowing
any number to be used for classes like `opacity-82` and others, but it hasn't
kept up with recent developments of Tailwind CSS. In fact, twind has been
unmaintained for more than a year by now. We never could get autocompletion with
twind to work either.
### Why did Fresh use twind instead of Tailwind CSS?
When Fresh was originally built, Deno didn't support npm modules or node APIs.
This meant that Tailwind CSS didn't work with Deno. Now, many years later, Deno
does ship with support for both of that and we can use the same npm
`tailwindcss` module as everyone else.