Fix search on Vercel by serving Pagefind as static files
The previous approach using an API route to serve Pagefind files doesn't work on Vercel's serverless environment because fs.readFile can't reliably access files in the deployed output. Solution: Serve Pagefind files directly from public/_pagefind as static assets, which is the standard Next.js approach and works reliably on all deployment platforms. Changes: - Update search modal to load from /_pagefind/ instead of /pagefind/ - Remove app/pagefind/[...path]/route.ts API route (no longer needed) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user