Skip to content

Commit

Permalink
allow providing pagefind module manually
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee committed Sep 18, 2024
1 parent 3e37ac4 commit 127cc88
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions js/packages/web/src/components/canary-provider-pagefind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@ export class CanaryProviderPagefind extends LitElement {
async connectedCallback() {
super.connectedCallback();

let pagefind: any;
try {
pagefind = await this._importPagefind();
} catch (e) {
console.info(
"Failed to import pagefind. This is totally fine if you are not in production mode.",
e,
);
return;
// @ts-expect-error
let pagefind = window.pagefind;

if (!pagefind) {
try {
pagefind = await import(
/* @vite-ignore */
/* webpackIgnore: true */
this.options?.path ?? "/pagefind/pagefind.js"
);
} catch (e) {
console.info(
"Failed to import pagefind. Try serving app with production build.",
e,
);
return;
}
}

this._initPagefind(pagefind);
Expand All @@ -65,18 +73,6 @@ export class CanaryProviderPagefind extends LitElement {
);
}

private async _importPagefind() {
try {
return import(
/* @vite-ignore */
/* webpackIgnore: true */
this.options?.path ?? "/pagefind/pagefind.js"
);
} catch (e) {
throw new Error(`Failed to import pagefind': ${e}`);
}
}

private async _initPagefind(pagefind: any) {
try {
pagefind.init();
Expand Down Expand Up @@ -111,7 +107,7 @@ export class CanaryProviderPagefind extends LitElement {
signal.throwIfAborted();

return {
search: [
sources: [
{
name: LOCAL_SOURCE_NAME,
type: "webpage",
Expand Down

0 comments on commit 127cc88

Please sign in to comment.