Next.js Discord

Discord Forum

Robots.txt return not found 404 but only on my custom domains

Unanswered
Nile Crocodile posted this in #help-forum
Open in Discord
Nile CrocodileOP
I have aproblem where i can't reach robots.txt on my custom domains.

this is my middleware file:

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { get } from '@vercel/edge-config';

const API_SITEMAP_PATH = "/api/sitemap";
const SITE_MAPREGEX = new RegExp(/([\wd-]sitemap[\wd_-].xml)/);

type RedirectEntry = {
origin: string;
destination: string;
permanent: boolean;
}

async function redirectUsingConfigEntries(request: NextRequest): Promise<NextResponse | null> {
const path = request.nextUrl.pathname.replace(/^/|/$/g, '');
const isInMaintenanceMode = await get('isInMaintenanceMode')
const redirection = await get<RedirectEntry>('redirects');

// If in maintenance mode, point the url pathname to the maintenance page
if (isInMaintenanceMode) {
request.nextUrl.pathname = /maintenance

// Rewrite to the url
return NextResponse.rewrite(request.nextUrl)
}

if (redirection === undefined || !redirection[path]) {
return NextResponse.next();
}

const url = request.nextUrl.clone();
url.pathname = /${redirection[path].destination};
return NextResponse.redirect(url, 308);
}

export default async function handleRequest(request: NextRequest): Promise<NextResponse | null> {
if (request.url.match(SITE_MAP_REGEX)) return rewriteRequestToAPISitemap(request);
return redirectUsingConfigEntries(request);
}

async function rewriteRequestToAPISitemap(request: NextRequest): Promise<NextResponse | null> {
request.nextUrl.pathname = API_SITEMAP_PATH;
return NextResponse.rewrite(request.nextUrl);
}

export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|robots.txt).*)",
],
};

0 Replies