-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
193 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
packages/docs/src/routes/qwikcity/endpoint/overview/index.mdx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
packages/docs/src/routes/qwikcity/routing/default-index/index.mdx
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
packages/docs/src/routes/qwikcity/routing/endpoints/index.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: Endpoints | ||
--- | ||
|
||
# Endpoints | ||
|
||
As we discussed previously, an endpoint in Qwik City is `folders + an index`. | ||
|
||
For `index.md` and `index.mdx` files ((more here)[/qwikcity/content/mdx]), these endpoints will just return HTML and nothing else. For this section, we will focus on index.ts and index.tsx endpoints. | ||
|
||
## Page & Data Endpoints | ||
|
||
In Qwik City, "Page" and "Data" endpoints are the same except for one difference: a page exports a default component$ to render html. To learn about defining a page component, you can (read more here)[qwikcity/content/component]. A quick example would look like this: | ||
|
||
```typescript | ||
// File: src/routes/some/path/index.tsx | ||
import { component$ } from '@builder.io/qwik'; | ||
|
||
// Notice the default export | ||
export default component$(() => { | ||
return <h1>Hello World!</h1>; | ||
}); | ||
``` | ||
|
||
In the following sections, we'll look at the other things we can do within endpoint files. |
70 changes: 70 additions & 0 deletions
70
packages/docs/src/routes/qwikcity/routing/error-responses/index.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
title: Qwik City - Non-200 Response | ||
--- | ||
|
||
# Qwik City - Non-200 Response | ||
|
||
At times it is necessary to respond with HTTP status codes other than 200. In such cases, response handler is the place to determine what status code should be returned. | ||
|
||
Assume this file layout. | ||
``` | ||
- src/ | ||
- routes/ | ||
- product/ | ||
- [skuId]/ | ||
- index.tsx # https://example.com/product/1234 | ||
``` | ||
|
||
## 404 - Not Found | ||
|
||
Let's say that a user does a request to an invalid `skuId` such as `https://example.com/product/999`. In this case, we would like to return a 404 HTTP status code and render a 404 page. The place where we determine if the request is valid or not is in the request handler by looking into the database. Even if the response is non-200, the component still gets a chance to render a page (Except in the redirect case.) | ||
|
||
```typescript | ||
// File: src/routes/product/[skuId]/index.tsx | ||
import { component$ } from '@builder.io/qwik'; | ||
|
||
type EndpointData = ProductData | null; | ||
|
||
interface ProductData { | ||
skuId: string; | ||
price: number; | ||
description: string; | ||
} | ||
export const onGet: RequestHandler<EndpointData> = async ({ params, response }) => { | ||
const product = await loadProductFromDatabase(params.skuId); | ||
|
||
if (!product) { | ||
// Product data not found | ||
// but the data is still given to the renderer to decide what to do | ||
response.status = 404 | ||
return null | ||
} else { | ||
// ... | ||
} | ||
}; | ||
|
||
export default component$(() => { | ||
const resource = useEndpoint<typeof onGet>(); //equivalent to useEndpoint<EndpointData> | ||
|
||
if (resource.state=='resolved' && !resource.resolved) { | ||
// Early return for 404 | ||
return <div>404: Product not found!!!</div> | ||
} | ||
|
||
// Normal rendering | ||
return ( | ||
<Resource | ||
resource={resource} | ||
onPending={() => <div>Loading...</div>} | ||
onError={() => <div>Error</div>} | ||
onResolved={() => ( | ||
<> | ||
<h1>Product: {product.productId}</h1> | ||
<p>Price: {product.price}</p> | ||
<p>{product.description}</p> | ||
</> | ||
)} | ||
/> | ||
); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.