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

Improve Dynamic Slugs documentation. #1489

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
33 changes: 27 additions & 6 deletions src/docs/plugins/serverless.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ Use the new `serverless` option in `pagination` to slice up your paginated data
```yaml
pagination:
data: authors
alias: author
size: 1
serverless: eleventy.serverless.path.id
permalink:
Expand All @@ -355,13 +356,33 @@ permalink:

Eleventy fetches the value stored at `eleventy.serverless.path.id` (using [lodash get](https://lodash.com/docs/4.17.15#get)) and does an additional get on the pagination data in `authors`.

For example:
For example given this `authors` data :

```json
{
"zachleat": {
"name": "Zach Leatherman",
"content": "Content for Zach Leatherman"
},
"brianjon": {
"name": "Brian Jones",
"content": "Content for Brian Jones"
},
"janedanc": {
"name": "Jane Dance",
"content": "Content for Jane Dance"
}
}
```

1. A request is made to `/authors/zachleat/`
1. The dynamic URL slug for the `possum` serverless function `/authors/:id/` matches `zachleat` to `:id`. This sets `"zachleat"` in the `eleventy.serverless.path.id` Global Data.
1. Because `pagination.serverless` has the value `"eleventy.serverless.path.id"`, we use lodash.get to select the key `"zachleat"` from Global Data.
1. An additional `lodash.get(authors, "zachleat")` returns a single chunk of data for one author.
1. Pagination only operates on that one selected page for rendering.
1. The dynamic URL slug for the `possum` serverless function `/authors/:id/` matches `zachleat` to `:id`.
1. This in turn sets `eleventy.serverless.path.id` in your Global Data to `"zachleat"`.
1. As `pagination.serverless` is set to `"eleventy.serverless.path.id"` we can use it as a key via `lodash.get(authors, "zachleat")` to return data a chunk of data from `authors`.
1. Via `pagination.alias`, your template now has access to `author.name` and `author.content` for `"zachleat"`
1. And as `pagination.size` is set to 1, the pagination only operates on that one selected page for rendering.

{% callout "info", "md" %}You have to use an object for your pagination data. If you use an array you will get unexpected results.{% endcallout %}
Copy link
Member

Choose a reason for hiding this comment

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

Is this line true? You can’t use /authors/0/ to fetch [{ name: "", … }]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so.

If your posts object is an array, you can use /authors/0/ to fetch the first post, but that probably isn't very helpful.

You almost certainly want to match a serverless url against a full slug name and not just a numeric value.

Working repo here:
https://github.com/dwkns/eleventy-serverless-demo



### Input via Query Parameters
Expand Down Expand Up @@ -461,4 +482,4 @@ _Documentation in progress_
| <img src="https://packagephobia.com/badge?p=@11ty/eleventy-plugin-vue" alt="Bundle size for @11ty/eleventy-plugin-vue" loading="lazy"> | `@11ty/eleventy-plugin-vue` |
| <img src="https://packagephobia.com/badge?p=@11ty/eleventy-plugin-rss" alt="Bundle size for @11ty/eleventy-plugin-rss" loading="lazy"> | `@11ty/eleventy-plugin-rss` |

</details>
</details>