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

Help/Question: How to use pagefind in NextJS app/api #4199

Open
gfazioli opened this issue Feb 12, 2025 · 1 comment
Open

Help/Question: How to use pagefind in NextJS app/api #4199

gfazioli opened this issue Feb 12, 2025 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@gfazioli
Copy link

Hello,
I just migrated one of my projects from Nextra 3 to Nextra 4, and now pagefind is used for search functionalities. From the website's perspective, everything works well, but I need to create an API, for example:

Image
import { type NextRequest } from 'next/server';

export async function GET(request: NextRequest) {
  const searchParams = request.nextUrl.searchParams;
  const query = searchParams.get('query');
  // query is "hello" for /api/search?query=hello

  return new Response(`Hello, ${query}!`, {
    headers: { 'content-type': 'text/plain' },
  });
}

How can I use pagefind here? It's not mentioned in the official documentation for use with Next.js...

@dimaMachina
Copy link
Collaborator

Hi, you need import pagefind.js from public folder, take a look at Nextra’s Search component

window.pagefind = await import(
/* webpackIgnore: true */ addBasePath('/_pagefind/pagefind.js')
)

@dimaMachina dimaMachina added the documentation Improvements or additions to documentation label Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants