getPages
An instance method that provides data about the pages you’ve created within Makeswift
Options
Number of pages to fetch. Must be between 1 and 100.
Starting id cursor of the last page entry that was fetched.
A filter to only retrieve pages with a pathname that begins with this value.
A valid locale string (ex. "en-US").
The field to sort the result pages by. Can be any one of the following: 'title', 'path', 'description', 'createdAt', 'updatedAt'.
The direction of how the result pages are sorted. Can be either 'asc' or 'desc'.
Used to either retrieve the published version or working version of Makeswift
pages. Can either be Live or Working
Whether the results should include pages that are offline in Makeswift.
Return Type
Concepts
Pagination & Async Iterables
The requests made by getPages are paginated. A paginated request will only
retrieve a portion of the total pages per request. If you have a large number of
pages, you may need to paginate through the results to retrieve all of them.
To make this easier, the returned value of getPages is an extension of a
Promise and an AsyncIterable. This abstraction enables you to consume the
pages in multiple ways, allowing you to easily retrieve all pages without
worrying about the implementation details of pagination:
The custom async iterable returned by getPages includes the following methods:
-
toArray(): Promise<T[]>: Consumes all pages in the iterable and returns them as an array. -
map(callback: (item: T) => U): Maps over each page in the iterable to yield a new data type. Returns another async iterable. -
filter(predicate: (item: T) => Boolean): Selectively yields pages based on a predicate function. Returns another async iterable.
Because .map and .filter return another iterable, you can chain these
methods together:
For more details on async iterables, see the MDN documentation.
The pagination for this method is cursor based, meaning that you can pass an
id of a page to the after option to retrieve the next set of pages after
that page. This can be used to manually implement paginating through all pages:
Site Versions
By default, getPages will return data reflecting the state of published
Makeswift pages. To specify which variant of your pages you want to retrieve,
you can pass a value to the siteVersion option. If your site is using pages
router, use the getSiteVersion method to
retrieve the current site version. If your site is using app router, use the
getSiteVersion function.
Note that the publishedAt field of a page will always be null when
retrieving non-live versions of the site. This is because the page data has not
been published yet.
Similarly, if you need to retrieve information on pages that are not currently
online (even if they have been published), you can pass includeOffline: true
to the getPages method. By default, offline pages are excluded from results.
Usage
Path Prefix Filtering
Path prefix filtering allows you to selectively retrieve your Makeswift pages
based on if they start with a specific path. For example, suppose you have many
pages organized under the path /blog/. You can use the pathPrefix option to
retrieve all pages that start with /blog/:
Static Path Generation
Since Makeswift pages use dynamic routes with Next.js, you may want to
statically generate paths for your pages at build time. In pages router, this
can be done by defining
getStaticPaths
on your catch-all page:
Similarly for app router, static generation is done by defining
generateStaticParams
on your dynamically routed page:
Generating a Sitemap
The data from getPages can be used to generate a sitemap.xml file for your
site, allowing search engines to index your pages.
Here’s an example of how to use getPages with the next-sitemap library to
generate a sitemap:
Next.js app router also has built-in support for generating sitemaps.
We can use getPages to retrieve pages and map them to the format expected by Next.js:
A few things to note:
-
By default,
getPageswill return pages that are excluded from search. You can filter these pages out while constructing your sitemap based on theexcludedFromSearchproperty. -
getPageswill return the path of the page, which is relative to the root of the site. Depending on whether your host is using path-based or domain-based routing for localization, you may need to prepend the domain or locale to the path. -
If you did not explicitly set a
sitemapPriorityorsitemapFrequencyfor a page in the Makeswift builder, these attributes will benullin the response. You can specify default values for these fields in your sitemap generation logic.
Localization
To retrieve pages that you’ve created in one of your registered
locales, you can pass the locale option to the
getPages method. Each returned page will include any of its localized
alternates in the localizedVariants field.
If you do not pass a locale option, pages from your site’s default locale will
be returned. Each page will include any of its localized alternates in the
localizedVariants field.
For an example of how to use getPages with localization to statically generate
paths in Next.js app router, see
here.