Follow-up from #3162. This PR does various tweaks, mostly related to code blocks. - Adds missing titles to code blocks, and updates titles to be more consistent - Adds filenames to most ` ```tsx` blocks with e.g. `main.ts`, `dev.ts` or a suitable example (e.g. `routes/greeting.tsx`) based on the code - Adds filenames to ` ```diff` blocks if there's a relevant file, to make the icon e.g. TS over Diff icon - Code blocks with Fresh code (e.g. interfaces) uses ` ```tsx fresh 🍋` - Changes most ` ```sh` blocks with example terminal commands to ` ```sh Terminal` - Updates ` ```txt Project Structure` or ` ```diff Project Structure` blocks to be more consistent - Changes `sh` or `txt` to `txt-files` which improves highlighting and makes icon consistent - Renames `<project dir>` to `<project root>` to match builder `root` option terminology - Adds `<project root>` root folder to most/all overview folder overviews - Makes the structure blocks more consistently displayed - Tried to make some "bare" examples more complete ## Comparison Here's a small curated set of before/after changes. ### Deployment - `deno` highlighted in terminal commands - `.gitignore` syntax highlighting works without using `sh` as language <img width="1641" height="1009" alt="image" src="https://github.com/user-attachments/assets/170cf833-1f6a-4b0d-8d2d-a081bc28a967" /> ### File routing Updated icon, comments same as before, but root and hierarchy have new colors (`operator`/`function`). <img width="1647" height="588" alt="image" src="https://github.com/user-attachments/assets/f28db51b-4bee-45e4-be1c-e137a6d3ae3d" />
123 lines
2.8 KiB
Markdown
123 lines
2.8 KiB
Markdown
---
|
||
description: |
|
||
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.
|
||
---
|
||
|
||
Let's set up your first Fresh project. To create a new project, run this
|
||
command:
|
||
|
||
```sh Terminal
|
||
deno run -Ar jsr:@fresh/init
|
||
```
|
||
|
||
This will span a short wizard that guides you through the setup, like the
|
||
project name, if you want to use tailwindcss and if you're using vscode. Your
|
||
project folder should look like this:
|
||
|
||
```txt-files Project structure
|
||
<project root>
|
||
├── islands/ # Components that need JS to run client-side
|
||
│ └── Counter.tsx
|
||
├── routes/ # File system based routes
|
||
│ ├── api/
|
||
│ │ └── [name].tsx # API route for /api/:name
|
||
│ ├── _app.tsx # Renders the outer <html> content structure
|
||
│ └── index.tsx # Renders /
|
||
├── static/ # Contains static assets like css, logos, etc
|
||
│ └── ...
|
||
│
|
||
├── deno.json # Contains dependencies, tasks, etc
|
||
├── dev.ts # Run this during development
|
||
└── main.ts # Run this for production
|
||
```
|
||
|
||
Run the `dev` task to launch your app in development mode:
|
||
|
||
```sh Terminal
|
||
deno task dev
|
||
```
|
||
|
||
Go to the URL printed in the terminal to view your app.
|
||
|
||
TODO IMAGE
|
||
|
||
## Creating our first route
|
||
|
||
Let's create a new about page at `/about`. We can do that by adding a new file
|
||
at `routes/about.tsx`.
|
||
|
||
```tsx routes/about.tsx
|
||
import { define } from "../utils.ts";
|
||
|
||
export default define.page(() => {
|
||
return (
|
||
<main>
|
||
<h1>About</h1>
|
||
<p>This is the about page.</p>
|
||
</main>
|
||
);
|
||
});
|
||
```
|
||
|
||
If we navigate to `/about` in the browser we'll see our newly created page.
|
||
|
||
TODO: IMAGE
|
||
|
||
## Create an island
|
||
|
||
We're going to create a countdown component that requires JavaScript to function
|
||
in the browser.
|
||
|
||
Create a new file at `islands/Countdown.tsx`
|
||
|
||
```tsx islands/Countdown.tsx
|
||
export function Countdown(props: { target: string }) {
|
||
const count = useSignal(10);
|
||
|
||
useEffect(() => {
|
||
const timer = setInterval(() => {
|
||
if (count.value <= 0) {
|
||
clearInterval(timer);
|
||
}
|
||
|
||
count.value -= 1;
|
||
}, 1000);
|
||
|
||
return () => clearInterval(timer);
|
||
}, []);
|
||
|
||
if (count.value <= 0) {
|
||
return <p>Countdown: 🎉</p>;
|
||
}
|
||
|
||
return <p>Countdown: {count}</p>;
|
||
}
|
||
```
|
||
|
||
Let's add the countdown to our about page:
|
||
|
||
```tsx routes/about.tsx
|
||
import { define } from "../utils.ts";
|
||
import { Countdown } from "../islands/Countdown.tsx";
|
||
|
||
export default define.page(() => {
|
||
return (
|
||
<main>
|
||
<h1>About</h1>
|
||
<p>This is the about page.</p>
|
||
<Countdown />
|
||
</main>
|
||
);
|
||
});
|
||
```
|
||
|
||
Now, we can see our countdown in action:
|
||
|
||
TODO: IMAGE
|
||
|
||
## Next steps
|
||
|
||
TODO
|