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/clarify SSR body and layout APIs #160

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/pages/docs/pages/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,33 @@ As with Page layouts, App layouts are just HTML:
```

> When an app layout is present, Greenwood will merge the `<head>` and `<body>` tags for both app and page layouts into one HTML document structure for you.

## Server Rendering

Server rendered layouts can also be authored using Web Components:

```js
// src/layouts/app.js
export default class AppLayout extends HTMLElement {
async connectedCallback() {
const year = new Date().getFullYear();

this.innerHTML = `
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
</head>

<body>
<h1>My App</h1>
<page-outlet></page-outlet>
<footer>&copy; ${year}</footer>
</body>
</html>
`;
}
}
```

> ⚠ This layout component will _only run once at build time_. Dynamic "runtime" layouts are [planned](https://github.com/ProjectEvergreen/greenwood/issues/1248).
15 changes: 11 additions & 4 deletions src/pages/docs/pages/server-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ The above would serve content in a browser at the path _/users/_.

## Usage

In your page file, Greenwood supports the following functions that you can `export` for providing server rendered content and [frontmatter](/docs/resources/markdown/):
In your page file, Greenwood supports the following functions that you can `export` for providing server rendered content and [frontmatter](/docs/resources/markdown/) to produce the `<body><body>` content for your page.

- **default** (recommended): Use the custom elements API to render out your page content, aka **Web (Server) Components**
- **default** (recommended): Use the custom elements API to render out your page content, aka **Web (Server) Components**. _This will take precedence over `getBody`_ and will also automatically track your custom element dependencies (in place of having to define frontmatter imports in `getFrontmatter`).
- **getBody**: Return a string of HTML for the contents of the page
- **getLayout**: Return a string of HTML to act as the [page's layout](/docs/pages/layouts/#pages)
- **getFrontmatter**: Provide an object of [frontmatter](/docs/resources/markdown/#frontmatter) properties. Useful in conjunction with [content as data](/docs/content-as-data/), or otherwise setting static configuration / metadata through SSR.
Expand Down Expand Up @@ -81,7 +81,12 @@ export default class UsersPage extends HTMLElement {
})
.join("");

this.innerHTML = html;
this.innerHTML = `
<body>
<h1>Friends List</h1>
${html}
</body>
`;
}
}
```
Expand Down Expand Up @@ -134,7 +139,7 @@ export async function getBody(compilation, page, request) {

### Layouts

For creating a [layout](/docs/pages/layouts/) dynamically, you can provide a `getLayout` function and return the HTML you need.
Although global [layouts](/docs/pages/layouts/) exist, you can provide a `getLayout` function on a per page basis to override or set the layout for the given page.

You can pull in data from Greenwood's [compilation](/docs/reference/appendix/#compilation) object as well as the specific route:

Expand Down Expand Up @@ -165,6 +170,8 @@ export async function getLayout(compilation, route) {
}
```

> ⚠ This function is _only run once at build time_. Dynamic "runtime" layouts are [planned](https://github.com/ProjectEvergreen/greenwood/issues/1248).

### Frontmatter

Any Greenwood supported frontmatter can be returned here. _This is only run once when the server is started_ to populate pages metadata, which is helpful if you want your dynamic route to show up in a collection with other static pages. You can even define a `layout` and reuse all your existing [layouts](/docs/pages/layouts/), even for server routes!
Expand Down