Skip to content
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

Merged
merged 18 commits into from
Feb 7, 2023
Merged

docs: server functions #2722

merged 18 commits into from
Feb 7, 2023

Conversation

manucorporat
Copy link
Contributor

No description provided.

@stackblitz
Copy link

stackblitz bot commented Jan 26, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@cloudflare-pages
Copy link

cloudflare-pages bot commented Jan 26, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: e50ead3
Status: ✅  Deploy successful!
Preview URL: https://2c517300.qwik-docs.pages.dev
Branch Preview URL: https://server-fn-docs.qwik-docs.pages.dev

View logs


```tsx
export const onGet: RequestHandler = async ({ json }) => {
throw json(200, { hello: 'world' });
Copy link

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?)

Copy link

@VinSpee VinSpee left a 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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> 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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@manucorporat
Copy link
Contributor Author

Thanks for the review! unfortunately i am moving so many things, i cant apply the feedback!
I will get it to a much better spot today


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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The -> They?

@manucorporat manucorporat enabled auto-merge (squash) February 7, 2023 18:02
@manucorporat manucorporat merged commit 5adc8f5 into main Feb 7, 2023
@manucorporat manucorporat deleted the server-fn-docs branch February 7, 2023 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants