-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: server functions #2722
docs: server functions #2722
Conversation
Run & review this pull request in StackBlitz Codeflow. |
Deploying with Cloudflare Pages
|
|
||
```tsx | ||
export const onGet: RequestHandler = async ({ json }) => { | ||
throw json(200, { hello: 'world' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the new syntax to actually throw
each of these (text
, json
, etc?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some typos / tweaks to wording. I’m very excited about these docs / the upcoming release, looking good!
|
||
Loaders allow data to flow from the server to the Qwik Components. For this reason, Qwik Loaders should be always understood in the context of **loading data in the server that is later consumed by a Qwik Component**, if you want to create a RESTful endpoint, please check the [endpoints](/guide/endpoints) guide instead. | ||
|
||
The behave like RPC server-side functions that can be invokated by Qwik Components during rendering. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behave like RPC server-side functions that can be invokated by Qwik Components during rendering. | |
They behave like RPC server-side functions that can be invoked by Qwik Components during rendering. |
|
||
## Using a loader | ||
|
||
Loaders are consumed using the `.use()` method from within a Qwik Component. The `use()` return a `Signal` which value is the data returned by the loader function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loaders are consumed using the `.use()` method from within a Qwik Component. The `use()` return a `Signal` which value is the data returned by the loader function. | |
Loaders are consumed using the `.use()` method from within a Qwik Component. `use()` returns a `Signal` with the data returned by the loader function as it’s value. |
}); | ||
``` | ||
|
||
> The `.use()` retrives the loader data, but it does not execute the loader multiple times. Loaders execute eagarly at the beginning of the request in order to provide low latency. For this reason they are only allowed in the `src/routes` folder, in a `layout.tsx` or `index.tsx` file, and they MUST be exported. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
> The `.use()` retrives the loader data, but it does not execute the loader multiple times. Loaders execute eagarly at the beginning of the request in order to provide low latency. For this reason they are only allowed in the `src/routes` folder, in a `layout.tsx` or `index.tsx` file, and they MUST be exported. | |
> `.use()` retrieves the loader data, but it does not execute the loader multiple times. Loaders execute eagerly, at the beginning of the request, in order to provide low latency. For this reason they are only allowed in the `src/routes` folder, in a `layout.tsx` or `index.tsx` file, and they MUST be exported. |
|
||
## Loader context | ||
|
||
Just like request handlers such as `onRequest` and `onGet`, loaders have access to the `RequestEvent` object, that includes information about the current request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just like request handlers such as `onRequest` and `onGet`, loaders have access to the `RequestEvent` object, that includes information about the current request. | |
Just like request handlers such as `onRequest` and `onGet`, loaders have access to the `RequestEvent` object which includes information about the current request. |
|
||
Just like request handlers such as `onRequest` and `onGet`, loaders have access to the `RequestEvent` object, that includes information about the current request. | ||
|
||
This information comes handly when the loaders needs to conditionally return data based on the request, or it needs to override the response status, headers or body manually. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This information comes handly when the loaders needs to conditionally return data based on the request, or it needs to override the response status, headers or body manually. | |
This information comes in handy when the loader needs to conditionally return data based on the request, or it needs to override the response status, headers or body manually. |
|
||
A loader should be used when you need to provide some server-side data to your Qwik Components. For example, if you need to fetch some data from a database or an API, you can use a loader to do that. | ||
|
||
You should not use a loader to create a REST API, for that you better use a [Endpoint](/qwik-city/), which allows you to have tight control over the response headers and body. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should not use a loader to create a REST API, for that you better use a [Endpoint](/qwik-city/), which allows you to have tight control over the response headers and body. | |
You should not use a loader to create a REST API, for that you’d be better off using an [Endpoint](/qwik-city/), which allows you to have tight control over the response headers and body. |
Thanks for the review! unfortunately i am moving so many things, i cant apply the feedback! |
|
||
Loaders allow data to flow from the server to the Qwik Components. For this reason, Qwik Loaders should be always understood in the context of **loading data in the server that is later consumed by a Qwik Component**, if you want to create a RESTful endpoint, please check the [endpoints](/qwikcity/endpoints/index.mdx) guide instead. | ||
|
||
The behave like RPC server-side functions that can be invokated by Qwik Components during rendering. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The -> They?
No description provided.