diff --git a/docs/canary/advanced/app-wrapper.md b/docs/canary/advanced/app-wrapper.md new file mode 100644 index 00000000..984de657 --- /dev/null +++ b/docs/canary/advanced/app-wrapper.md @@ -0,0 +1,33 @@ +--- +description: | + Add a global app wrapper to provide common meta tags or context for application routes. +--- + +The app wrapper component is a Preact component that represents the outer +structure of the HTML document, typically up until the `
`-tag. It is only +rendered on the server and never on the client. The passed `Component` value +represents the children of this component. + +```tsx +function AppWrapper({ Component }) { + return ( + + + +404 not found: {url.pathname}
; +} +``` + +#### Manually render 404 pages + +The `_404.tsx` file will be invoked automatically when no route matches the URL. +In some cases, one needs to manually trigger the rendering of the 404 page, for +example when the route did match, but the requested resource does not exist. +This can be achieved with `ctx.renderNotFound`. + +```tsx routes/blog/[slug].tsx +import { Handlers, PageProps } from "$fresh/server.ts"; + +export const handler: Handlers = { + async GET(req, ctx) { + const blogpost = await fetchBlogpost(ctx.params.slug); + if (!blogpost) { + return ctx.renderNotFound({ + custom: "prop", + }); + } + return ctx.render({ blogpost }); + }, +}; + +export default function BlogpostPage({ data }) { + return ( +500 internal error: {(error as Error).message}
; +} +``` diff --git a/docs/canary/advanced/forms.md b/docs/canary/advanced/forms.md new file mode 100644 index 00000000..fd7dcc06 --- /dev/null +++ b/docs/canary/advanced/forms.md @@ -0,0 +1,120 @@ +--- +description: | + Robustly handle user inputs using HTML ` + > + ); +} +``` + +When the user submits the form, Deno will retrieve the `email` value using the +request's `formData()` method, add the email to a list, and redirect the user to +a thank you page. + +## Handling file uploads + +File uploads can be handled in a very similar manner to the example above. Note +that this time, we have to explicitly declare the form's encoding to be +`multipart/form-data`. + +```tsx routes/subscribe.tsx +import { Handlers, type PageProps } from "$fresh/server.ts"; + +interface Props { + message: string | null; +} + +export const handler: Handlers{message}
: null} + > + ); +} +``` + +## A note of caution + +These examples are simplified to demonstrate how Deno and Fresh handle HTTP +requests. In the Real World™, you'll want to validate your data (_especially the +file type_) and protect against cross-site request forgery. Consider yourself +warned. diff --git a/docs/canary/advanced/layouts.md b/docs/canary/advanced/layouts.md new file mode 100644 index 00000000..35831536 --- /dev/null +++ b/docs/canary/advanced/layouts.md @@ -0,0 +1,47 @@ +--- +description: "Create re-usable layouts across routes" +--- + +Layouts are plain Preact components that are inherited based on the matching +pattern. When you have a section on your site where all pages share the same +HTML structure and only the content changes, a layout is a neat way to abstract +this. Layouts only ever render on the server. The passed `Component` value +represents the children of this component. + +```tsx +function PageLayout({ Component }) { + return ( +