Skip to content

Commit c98179a

Browse files
LadyBluenotesroot
and
root
authored
Router section rewrite (#715)
Co-authored-by: root <root@TheGOAT>
1 parent 46ed86c commit c98179a

34 files changed

+788
-509
lines changed

.gitpod.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

playwright.config.ts

Lines changed: 0 additions & 107 deletions
This file was deleted.

sandbox.config.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/routes/reference/component-apis/lazy.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Lazy components trigger `<Suspense>`
1515

1616
```tsx
1717
// wrap import
18-
const ComponentA = lazy(() => import("./ComponentA"))
18+
const ComponentA = lazy(() => import("./ComponentA"));
1919

2020
// use in JSX
21-
;<ComponentA title={props.title} />
21+
<ComponentA title={props.title} />
2222
```
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"title": "Store Utilities",
33
"pages": [
4+
"create-mutable.mdx",
45
"create-store.mdx",
6+
"modify-mutable.mdx",
57
"produce.mdx",
68
"reconcile.mdx",
7-
"unwrap.mdx",
8-
"create-mutable.mdx",
9-
"modify-mutable.mdx"
9+
"unwrap.mdx"
1010
]
1111
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "Advanced Concepts",
3+
"pages": ["lazy-loading.mdx"]
4+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Lazy loading
3+
---
4+
5+
Lazy loading allows you to load only the necessary resources when they are needed.
6+
This can be useful when you have a large application with a lot of routes and components, and you want to reduce the initial load time.
7+
8+
In Solid Router, you can lazy load components using the `lazy` function from Solid:
9+
10+
```jsx
11+
import { lazy } from "solid-js";
12+
import { Router, Route } from "solid-router";
13+
14+
const Home = () => import("./Home");
15+
const Users = lazy(() => import("./Users"));
16+
17+
const App = () => (
18+
<Router>
19+
<Route path="/" element={<Home />} />
20+
<Route path="/users" element={<Users />} />
21+
</Router>
22+
);
23+
```
24+
25+
In the example above, the `Home` component is lazy loaded using the `lazy` function.
26+
The `lazy` function takes a function that returns a promise, which resolves to the component you want to load.
27+
When the route is matched, the component will be loaded and rendered.

src/routes/solid-router/concepts/actions.mdx

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,74 @@
22
title: "Actions"
33
---
44

5-
To communicate with your server, you will need to use actions.
6-
Actions are a way to submit data to your server and receive a response.
7-
They are isomorphic, meaning they can run on the server or the client.
5+
When developing applications, it is common to need to communicate new information to the server based on user interactions.
6+
Actions are Solid Routers solution to this problem.
87

9-
One question you will likely have when developing any sort of app is "how do I communicate new information to my server?".
10-
The user did something.
11-
What next? Solid Router's answer to this is _actions_.
8+
## What are actions?
129

13-
Actions give you the ability to specify an async action processing function and gives you elegant tools to help you easily manage and track submissions. Actions are isomorphic and represent a `POST` request.
10+
Actions are asynchronous processing functions that allow you to submit data to your server and receive a response.
11+
They are isomorphic, meaning they can run either on the server or the client, depending on what is needed.
12+
This flexibility makes actions a powerful tool for managing and tracking data submission.
1413

15-
Actions are isomorphic. This means that a submission can be handled on the server _or_ the client, whichever is optimal. They represent the server component of an [HTML form](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form), and even help you use HTML forms to submit data.
14+
### How actions work
15+
16+
Actions represent the server-side part of an [HTML form](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form).
17+
They handle submissions through POST requests, allowing you to easily use HTML forms to send data.
18+
19+
When a user performs an action, such as submitting a form, the data is sent to the server for processing via an action.
20+
21+
### Benefits of using actions
22+
23+
1. **Isomorphic**: Since actions can run on both the server and client, you can optimize performance by choosing the best execution environment for your needs.
24+
2. **Asynchronous processing**: Actions handle data submissions asynchronously, ensuring that your application remains responsive.
25+
3. **Simplified data handling**: By using actions, the process of managing and tracking data submissions can be streamlined, reducing the complexity of your application.
1626

1727
## Creating actions
1828

19-
Let's stop getting ahead of ourselves! First, let's create an action!
29+
To create an action, use the `action` function from the `@solidjs/router` package.
30+
This function takes an asynchronous function as an argument and returns a new function that can be used to submit data.
2031

21-
```tsx twoslash
32+
```tsx
2233
import { action, useAction } from "@solidjs/router";
2334

2435
const echo = action(async (message: string) => {
25-
// Imagine this is a call to fetch
36+
// Simulates an asynchronous operation, such as an API call
2637
await new Promise((resolve, reject) => setTimeout(resolve, 1000));
2738
console.log(message);
2839
});
29-
30-
export function MyComponent() {
31-
const myEcho = useAction(echo);
32-
}
3340
```
3441

35-
This `echo` action will act as your backend, however you can substitute it for any API, provided you are ok with it running on the client. Typically, route actions are used with some sort of solution like fetch or GraphQL.
42+
In this example, the `echo` action simulates a fetch call with a 1 second delay before logging the message to the console.
43+
The `echo` action will act as a backend, however, it can be substituted for any API provided it can be run on the client.
44+
Typically, route actions are used with some sort of solution like fetch or GraphQL.
3645

37-
These will return either a `Response` such as a redirect (we are not returning anything quite yet!) or any value. If you want to ensure the action only runs on the server for things like databases, you will want to use `"use server"`, introduced below.
46+
### Using actions
3847

39-
Naturally, this action won't do anything quite yet. We still need to call it somewhere! For now, let's call it manually from some component using the submit function returned from `action`.
48+
To use the action, you can call it from within a component using `useAction`.
49+
This returns a function that can be called with the necessary arguments to trigger the action.
4050

41-
```tsx twoslash
42-
import { action, useAction } from "@solidjs/router";
43-
44-
const echo = action(async (message: string) => {
45-
// Imagine this is a call to fetch
46-
await new Promise((resolve, reject) => setTimeout(resolve, 1000));
47-
console.log(message);
48-
});
51+
```tsx
52+
import { useAction } from "@solidjs/router";
4953

5054
export function MyComponent() {
5155
const myEcho = useAction(echo);
52-
myEcho("Hello from solid!");
56+
57+
myEcho("Hello from Solid!");
5358
}
5459
```
5560

56-
You should see `Hello from solid!` back in the console!
61+
In this component, `useAction` is used to get a reference to the `echo` action.
62+
The action is then called with the message `"Hello from Solid!"`, which will be logged to the console after a 1 second delay.
63+
64+
### Returning data from actions
5765

58-
### Returning from actions
66+
In many cases, after submitting data, the server sends some data back as well.
67+
This may be in the form of an error message if something has failed or the results of a successful operation.
68+
Anything returned from an action can be accessed using the reactive `action.result` property, where the value can change each time you submit your action.
5969

60-
In many cases, after submitting data the server sends some data back as well. Usually an error message if something failed. Anything returned from your action function can be accessed using the reactive `action.result` property. The value of this property can change each time you submit your action.
70+
To access the action's result, you must pass the action to `useSubmission`:
6171

62-
```tsx twoslash
72+
```tsx {8,14}
6373
import { action, useAction, useSubmission } from "@solidjs/router";
6474

6575
const echo = action(async (message: string) => {
@@ -70,35 +80,46 @@ const echo = action(async (message: string) => {
7080
export function MyComponent() {
7181
const myEcho = useAction(echo);
7282
const echoing = useSubmission(echo);
83+
7384
myEcho("Hello from solid!");
85+
7486
setTimeout(() => myEcho("This is a second submission!"), 1500);
87+
7588
return <p>{echoing.result}</p>;
7689
}
7790
```
7891

79-
While this method of using actions works, it leaves the implementation details of how you trigger `echo` up to you. When handling explicit user input, it's better to use a `form` for a multitude of reasons.
92+
Using `useSubmission` leaves the implementation details of how you trigger `echo` up to you.
93+
Whe handling user inputs, for example, it is better to use a `form` for a multitude of reasons.
8094

8195
## Using forms to submit data
8296

83-
We highly recommend using HTML forms as your method to submit data with actions. HTML forms can be used even before JavaScript loads, leading to instantly interactive applications.
97+
When submitting data with actions, it is recommended to use HTML forms.
98+
These forms can be used prior to JavaSCript loading, which creates instantly interactive applications.
99+
This also inherently provides accessibility benefits, saving the time of designing a custom UI library that may not have these benefits.
84100

85-
They have the added benefit of implicit accessibility. They can save you valuable time that would have otherwise been spent designing a UI library that will never have the aforementioned benefits.
101+
When using forms to submit actions, the first argument passed to your action function is an instance of [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData).
102+
To use actions with forms, pass the action to the `action` property of your form.
103+
This creates progressively enhanced forms that work even when JavaScript is disabled.
86104

87-
When forms are used to submit actions, the first argument is an instance of [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData). To write a form using actions, pass the action to the action property of your form. You can then walk away with amazing, progressively enhanced forms!
105+
If you do not return a `Response` from your action, the user will stay on the same page and responses will be re-triggered.
106+
Using a `redirect` can tell the browser to navigate to a new page.
88107

89-
If you don't return a `Response` from your action, the user will stay on the same page and your resources will be re-triggered. You can also throw a `redirect` to tell the browser to navigate.
90108

91-
```tsx twoslash
109+
```tsx
92110
import { action, redirect } from "@solidjs/router";
93111

94112
const isAdmin = action(async (formData: FormData) => {
95113
await new Promise((resolve, reject) => setTimeout(resolve, 1000));
114+
96115
const username = formData.get("username");
116+
97117
if (username === "admin") throw redirect("/admin");
98118
return new Error("Invalid username");
99119
});
100120

101121
export function MyComponent() {
122+
102123
return (
103124
<form action={isAdmin} method="post">
104125
<label for="username">Username:</label>
@@ -111,6 +132,8 @@ export function MyComponent() {
111132

112133
## Error handling
113134

114-
We strongly recommend with actions to "return" errors rather than throwing them. This can help with typing of submissions you'd use with `useSubmission`. This is important especially for handling progressive enhancement where no JS is present in the client so that we can use the error declaratively to render the updated page on the server.
135+
Rather than throwing errors, it is recommended to return them from actions.
136+
This helps with the typing of submissions that would be used with `useSubmission`.
137+
This is important when handling progressive enhancement where no JavaScript is present in the client, so that errors can be used declaratively to render the updated page on the server.
115138

116-
Additionally when using Server Actions it is good practice to try to handle errors on the server so that you can sanitize error messages.
139+
Additionally, when using server actions, it is good practice to handle errors on the server to sanitize error messages.

0 commit comments

Comments
 (0)