Skip to content

Commit

Permalink
Merge branch 'main' into feat/defer-js-init
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Sep 1, 2023
2 parents d72cd13 + 3e3d2fe commit 00b3831
Show file tree
Hide file tree
Showing 76 changed files with 2,044 additions and 775 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ public
.hugo_build.lock
_gen
pagefind_ui/default/_dev_files/_pagefind/ui.*
pagefind_ui/default/_dev_files/pagefind/ui.*
pagefind_ui/default/css
pagefind_ui/modular/_dev_files/_pagefind/modular.*
pagefind_ui/modular/_dev_files/pagefind/modular.*

npm_dist

Expand Down
10 changes: 5 additions & 5 deletions docs/content/docs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Since Pagefind indexes your site _after_ it builds, we'll do things slightly out
Pagefind provides a prebuilt search UI out of the box. Add the following snippet to a page of your choice:

```html
<link href="/_pagefind/pagefind-ui.css" rel="stylesheet">
<script src="/_pagefind/pagefind-ui.js"></script>
<link href="/pagefind/pagefind-ui.css" rel="stylesheet">
<script src="/pagefind/pagefind-ui.js"></script>
<div id="search"></div>
<script>
window.addEventListener('DOMContentLoaded', (event) => {
Expand All @@ -23,20 +23,20 @@ Pagefind provides a prebuilt search UI out of the box. Add the following snippet
</script>
```

> The `/_pagefind/pagefind-ui.css` and `/_pagefind/pagefind-ui.js` assets will be created by Pagefind when we index the site.
> The `/pagefind/pagefind-ui.css` and `/pagefind/pagefind-ui.js` assets will be created by Pagefind when we index the site.
Now build your site to an output directory — this guide assumes that you're running `hugo` and that your site is output to the `public/` directory. Pagefind works with any set of static HTML files, so adjust these configurations as needed.

> If you're running a development server (i.e. `hugo serve`) you won't see anything yet, as Pagefind needs to index the _output_ of your build. Let's do that now.
## Indexing your site

The easiest way to run pagefind is through npx, where `--source` points to the output directory of your static site generator. We'll also add `--serve` so that we can view our final site right away.
The easiest way to run pagefind is through npx, where `--site` points to the output directory of your static site generator. We'll also add `--serve` so that we can view our final site right away.

> Note that Pagefind itself does not have any server component — the search integration is fully baked into your static site. The `--serve` flag here is a shortcut for running Pagefind, followed by serving your output site through any static web server.
```bash
npx -y pagefind --source public --serve
npx -y pagefind --site public --serve
```

You should see some output along the lines of:
Expand Down
16 changes: 8 additions & 8 deletions docs/content/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ Pagefind can be accessed as an API directly from JavaScript, for you to build cu
Anywhere on your site, you can initialize Pagefind with:

```js
const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
```

This will load the Pagefind library and the metadata about the site. If your site is on a subpath, this should be included — e.g. in the CloudCannon documentation, we load `/documentation/_pagefind/pagefind.js`.
This will load the Pagefind library and the metadata about the site. If your site is on a subpath, this should be included — e.g. in the CloudCannon documentation, we load `/documentation/pagefind/pagefind.js`.

> If building your own search UI, it is a good idea to only run this import once your search component has received interaction. This saves the user from loading the Pagefind library on every page request.
Expand All @@ -25,7 +25,7 @@ This will load the Pagefind library and the metadata about the site. If your sit
To perform a search, await `pagefind.search`:
{{< diffcode >}}
```js
const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
+const search = await pagefind.search("static");
```
{{< /diffcode >}}
Expand All @@ -50,7 +50,7 @@ To load the data for a result, await the data function:
{{< diffcode >}}
```js
const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
const search = await pagefind.search("static");
+const oneResult = await search.results[0].data();
```
Expand Down Expand Up @@ -80,7 +80,7 @@ To load a "page" of results, you can run something like the following:
{{< diffcode >}}
```js
const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
const search = await pagefind.search("static");
+const fiveResults = await Promise.all(search.results.slice(0, 5).map(r => r.data()));
```
Expand Down Expand Up @@ -215,15 +215,15 @@ const search = await pagefind.search(null, {
The helper function `pagefind.debouncedSearch` is available and can be used in place of `pagefind.search`:
{{< diffcode >}}
```js
const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
+const search = await pagefind.debouncedSearch("static");
```
{{< /diffcode >}}
A custom debounce timeout (default: `300`) can optionally be specified as the third argument:
{{< diffcode >}}
```js
const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
+const search = await pagefind.debouncedSearch("static", {/* options */}, 300);
```
{{< /diffcode >}}
Expand All @@ -247,7 +247,7 @@ If you have a debounced search input, Pagefind won't start loading indexes until
{{< diffcode >}}
```js
const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
+pagefind.preload("s");

// later...
Expand Down
27 changes: 17 additions & 10 deletions docs/content/docs/config-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,35 @@ The Pagefind CLI has the following options:
## Required arguments

### Source
### Site
The location of your built static site.

| CLI Flag | ENV Variable | Config Key |
|---------------------|-------------------|------------|
| `--source <SOURCE>` | `PAGEFIND_SOURCE` | `source` |
| CLI Flag | ENV Variable | Config Key |
|-----------------|-----------------|------------|
| `--site <PATH>` | `PAGEFIND_SITE` | `site` |

## Optional arguments

### Serve
Serve the source directory after creating the search index. Useful for testing search on a local build of your site without having to serve the source directory manually.
Serve the site directory after creating the search index. Useful for testing search on a local build of your site without having to serve the site directory manually.

| CLI Flag | ENV Variable | Config Key |
|-----------|------------------|------------|
| `--serve` | `PAGEFIND_SERVE` | `serve` |

### Bundle directory
The folder to output search files into, relative to source. Defaults to `_pagefind`.
### Output subdirectory
The folder to output the search bundle into, relative to the processed site. Defaults to `pagefind`.

| CLI Flag | ENV Variable | Config Key |
|-------------------------|--------------------------|-----------------|
| `--output-subdir <DIR>` | `PAGEFIND_OUTPUT_SUBDIR` | `output_subdir` |

### Output path
The folder to output the search bundle into, relative to the working directory. Overrides `output-subdir` if supplied.

| CLI Flag | ENV Variable | Config Key |
|----------------------|-----------------------|--------------|
| `--bundle-dir <DIR>` | `PAGEFIND_BUNDLE_DIR` | `bundle_dir` |
| CLI Flag | ENV Variable | Config Key |
|------------------------|------------------------|---------------|
| `--output-path <PATH>` | `PAGEFIND_OUTPUT_PATH` | `output_path` |

### Root selector
The element that Pagefind should treat as the root of the document. Defaults to `html`.
Expand Down
10 changes: 5 additions & 5 deletions docs/content/docs/config-sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Pagefind will look for a `pagefind.toml`, `pagefind.yml`, `pagefind.yaml`, or `p

```yaml
# pagefind.yml
source: public
bundle_dir: _pagefind
site: public
output_subdir: pagefind
```
```bash
npx pagefind
Expand All @@ -26,14 +26,14 @@ npx pagefind
Pagefind will load any values via a `PAGEFIND_*` environment variable.

```bash
export PAGEFIND_BUNDLE_DIR="_pagefind"
PAGEFIND_SOURCE="public" npx pagefind
export PAGEFIND_OUTPUT_SUBDIR="pagefind"
PAGEFIND_SITE="public" npx pagefind
```

## CLI flags

Pagefind can be passed CLI flags directly.

```bash
npx pagefind --source public --bundle-dir _pagefind
npx pagefind --site public --output-subdir pagefind
```
4 changes: 0 additions & 4 deletions docs/content/docs/hosting.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ Pagefind outputs a static bundle directory to your built site, and no hosting co

Pagefind handles compression of the files in the bundle directly, so no server gzip support is required.

## Hosting on GitHub Pages

GitHub Pages won't serve URLs that start with a leading underscore, so the default path of `_pagefind` will 404. To fix this, set a custom [bundle directory](https://pagefind.app/docs/config-options/#bundle-directory) (_e.g. `--bundle-dir mysearch`_), and update your JS and CSS references to match (_e.g. `<script src="/mysearch/pagefind-ui.js" ...`_).

## Content Security Policy (CSP)

If you have a strict content security policy enabled on your site, you may encounter issues with the Pagefind WebAssembly — this isn't specific to Pagefind but is an issue with CSP and WebAssembly.
Expand Down
14 changes: 7 additions & 7 deletions docs/content/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ Pagefind is a static binary with no dynamic dependencies, so in most cases will
## Running via npx

```bash
npx pagefind --source "public"
npx pagefind --site "public"
```

Pagefind publishes a [wrapper package through npm](https://www.npmjs.com/package/pagefind), which is the easiest way to get started. This package will download the correct [binary of the latest release](https://github.com/CloudCannon/pagefind/releases) from GitHub for your platform and run it.

Specific versions can be run by passing a version tag:

```bash
npx pagefind@latest --source "public"
npx pagefind@latest --site "public"

npx [email protected] --source "public"
npx [email protected] --site "public"
```

> Running Pagefind via npx will download the `pagefind_extended` release, which includes specialized support for indexing Chinese and Japanese pages.
Expand All @@ -31,9 +31,9 @@ npx [email protected] --source "public"
If you prefer to install Pagefind yourself, you can download a [precompiled release from GitHub](https://github.com/CloudCannon/pagefind/releases) and run the binary directly:

```bash
./pagefind --source "public"
./pagefind --site "public"
# or
./pagefind_extended --source "public"
./pagefind_extended --site "public"
```

Pagefind publishes two releases, `pagefind` and `pagefind_extended`. The extended release is a larger binary, but includes specialized support for indexing Chinese and Japanese pages.
Expand All @@ -44,12 +44,12 @@ If you have [Rust and Cargo](https://doc.rust-lang.org/cargo/getting-started/ins

```bash
cargo install pagefind
pagefind --source "public"
pagefind --site "public"
```

To build and install the extended version of Pagefind:

```bash
cargo install pagefind --features extended
pagefind --source "public"
pagefind --site "public"
```
32 changes: 16 additions & 16 deletions docs/content/docs/multisite.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ When initializing the Pagefind UI, include a `mergeIndex` option with an array o
new PagefindUI({
element: "#search",
+ mergeIndex: [{
+ bundlePath: "https://docs.example.com/_pagefind"
+ bundlePath: "https://docs.example.com/pagefind"
+ }]
})
```
Expand All @@ -37,7 +37,7 @@ new PagefindUI({
element: "#search",
+ // ... options for the blog.example.com index
mergeIndex: [{
bundlePath: "https://docs.example.com/_pagefind",
bundlePath: "https://docs.example.com/pagefind",
+ // ... options for the docs.example.com index
}]
})
Expand All @@ -52,8 +52,8 @@ Using an initialized instance of Pagefind, await the `mergeIndex` function to ad
```js
// Running on blog.example.com

const pagefind = await import("/_pagefind/pagefind.js");
+await pagefind.mergeIndex("https://docs.example.com/_pagefind");
const pagefind = await import("/pagefind/pagefind.js");
+await pagefind.mergeIndex("https://docs.example.com/pagefind");
```
{{< /diffcode >}}

Expand All @@ -63,10 +63,10 @@ Pagefind options can be passed to the additional index as a second argument:
```js
// Running on blog.example.com

const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
+await pagefind.options({/* ... options for the blog.example.com index */});
await pagefind.mergeIndex(
"https://docs.example.com/_pagefind",
"https://docs.example.com/pagefind",
+ {/* ... options for the docs.example.com index */}
);
```
Expand All @@ -83,15 +83,15 @@ new PagefindUI({
element: "#search",
+ indexWeight: 2,
mergeIndex: [{
bundlePath: "https://docs.example.com/_pagefind",
bundlePath: "https://docs.example.com/pagefind",
+ indexWeight: 0.5
}]
})

// JS API:
const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
+await pagefind.options({ indexWeight: 2 });
await pagefind.mergeIndex("https://docs.example.com/_pagefind", {
await pagefind.mergeIndex("https://docs.example.com/pagefind", {
+ indexWeight: 0.5
});
```
Expand All @@ -110,17 +110,17 @@ new PagefindUI({
+ resource: "Blog"
+ },
mergeIndex: [{
bundlePath: "https://docs.example.com/_pagefind",
bundlePath: "https://docs.example.com/pagefind",
+ mergeFilter: {
+ resource: "Documentation"
+ }
}]
})

// JS API:
const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
+await pagefind.options({ mergeFilter: { resource: "Blog" } });
await pagefind.mergeIndex("https://docs.example.com/_pagefind", {
await pagefind.mergeIndex("https://docs.example.com/pagefind", {
+ mergeFilter: {
+ resource: "Documentation"
+ }
Expand All @@ -138,14 +138,14 @@ Pagefind will attempt to grab a matching language when merging an index, falling
new PagefindUI({
element: "#search",
mergeIndex: [{
bundlePath: "https://docs.example.com/_pagefind",
bundlePath: "https://docs.example.com/pagefind",
+ language: "pt-br"
}]
})

// JS API:
const pagefind = await import("/_pagefind/pagefind.js");
await pagefind.mergeIndex("https://docs.example.com/_pagefind", {
const pagefind = await import("/pagefind/pagefind.js");
await pagefind.mergeIndex("https://docs.example.com/pagefind", {
+ language: "pt-br"
});
```
Expand All @@ -161,7 +161,7 @@ Due to index merging happening in the browser, your additional search indexes mu
{
"headers": [
{
"match": "/_pagefind/.*",
"match": "/pagefind/.*",
"headers": [
{
"name": "Access-Control-Allow-Origin",
Expand Down
4 changes: 2 additions & 2 deletions docs/content/docs/search-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ If interfacing with Pagefind directly, options can be passed via awaiting `pagef

{{< diffcode >}}
```js
const pagefind = await import("/_pagefind/pagefind.js");
const pagefind = await import("/pagefind/pagefind.js");
+await pagefind.options({
+ baseUrl: "/",
+ // ... more search options
Expand All @@ -53,7 +53,7 @@ Defaults to "/". If hosting a site on a subpath, `baseUrl` can be provided, and

```json
{
"bundlePath": "/subpath/_pagefind/"
"bundlePath": "/subpath/pagefind/"
}
```

Expand Down
Loading

0 comments on commit 00b3831

Please sign in to comment.