mirror of
https://github.com/dergigi/boris.git
synced 2025-12-19 07:34:28 +01:00
fix(api): add base tag for proper asset loading
Use named parameter syntax in Vercel rewrite and add <base href="/"> tag to ensure assets load correctly from root when serving index.html through the API.
This commit is contained in:
@@ -214,18 +214,18 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
|
|||||||
|
|
||||||
const userAgent = req.headers['user-agent'] as string | undefined
|
const userAgent = req.headers['user-agent'] as string | undefined
|
||||||
|
|
||||||
// If it's a regular browser (not a bot), serve index.html
|
// If it's a regular browser (not a bot), serve index.html with base tag
|
||||||
// The rewrite preserves the URL, so SPA routing will handle /a/{naddr}
|
// The rewrite preserves the URL, so SPA routing will handle /a/{naddr}
|
||||||
if (!isCrawler(userAgent)) {
|
if (!isCrawler(userAgent)) {
|
||||||
// Import the static index.html and serve it
|
|
||||||
// This preserves the /a/{naddr} URL for client-side routing
|
|
||||||
const fs = await import('fs')
|
const fs = await import('fs')
|
||||||
const path = await import('path')
|
const path = await import('path')
|
||||||
// eslint-disable-next-line no-undef
|
// eslint-disable-next-line no-undef
|
||||||
const indexPath = path.join(process.cwd(), 'dist', 'index.html')
|
const indexPath = path.join(process.cwd(), 'dist', 'index.html')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const indexHtml = fs.readFileSync(indexPath, 'utf-8')
|
let indexHtml = fs.readFileSync(indexPath, 'utf-8')
|
||||||
|
// Add base tag to ensure assets load from root
|
||||||
|
indexHtml = indexHtml.replace('<head>', '<head>\n <base href="/" />')
|
||||||
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||||
res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate')
|
res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate')
|
||||||
return res.status(200).send(indexHtml)
|
return res.status(200).send(indexHtml)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"rewrites": [
|
"rewrites": [
|
||||||
{
|
{
|
||||||
"source": "/a/(.*)",
|
"source": "/a/:naddr",
|
||||||
"destination": "/api/article-og?naddr=$1"
|
"destination": "/api/article-og?naddr=:naddr"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"source": "/(.*)",
|
"source": "/(.*)",
|
||||||
|
|||||||
Reference in New Issue
Block a user