Files
blog-nextjs/app/pagefind/[...path]/route.ts
gbanyan a7aa930759 Fix search hanging on production by correcting Pagefind file path
Problem: Search modal hangs on loading in production deployment,
but works fine locally.

Root cause: The Pagefind route handler was reading files from
.next/pagefind, which is not reliably accessible in production
deployments. The build script copies Pagefind files to
public/_pagefind for deployment, but the route wasn't using them.

Solution: Changed the file path in app/pagefind/[...path]/route.ts
from .next/pagefind to public/_pagefind. Files in the public
directory are always accessible and properly deployed.

This ensures search works consistently across both development
and production environments.
2025-11-20 16:27:34 +08:00

41 lines
1.2 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { readFile } from 'fs/promises';
import { join } from 'path';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ path: string[] }> }
) {
try {
const { path } = await params;
const filePath = join(process.cwd(), 'public', '_pagefind', ...path);
// Read the file
const file = await readFile(filePath);
// Determine content type
const ext = path[path.length - 1].split('.').pop();
const contentTypes: Record<string, string> = {
'js': 'application/javascript',
'css': 'text/css',
'json': 'application/json',
'wasm': 'application/wasm',
'pf_meta': 'application/octet-stream',
'pf_index': 'application/octet-stream',
'pf_fragment': 'application/octet-stream',
};
const contentType = contentTypes[ext || ''] || 'application/octet-stream';
return new NextResponse(file, {
headers: {
'Content-Type': contentType,
'Cache-Control': 'public, max-age=31536000, immutable',
},
});
} catch (error) {
console.error('Error serving Pagefind file:', error);
return new NextResponse('File not found', { status: 404 });
}
}