Next.js Discord

Discord Forum

Rewrites behave different between App Dir and Pages Dir when using localization?

Unanswered
Masai Lion posted this in #help-forum
Open in Discord
Avatar
Masai LionOP
I've middleware to rewrite pathname:
import { NextRequest, NextResponse } from "next/server";

export async function middleware(request: NextRequest) {
  if (request.nextUrl.pathname.includes("mano-puslapis")) { // hardcode path for simplicity
    const url = request.nextUrl.clone();
    
    url.pathname = `/static${request.nextUrl.pathname}`; // static is hardcoded for simplicity - in real scenario, it comes from another middleware using full pathname
    
    console.log("rewrite to", url.pathname, url, request.nextUrl.locale)
    return NextResponse.rewrite(url)
  }

  return NextResponse.next();
}


Next config:
const nextConfig: NextConfig = {
  i18n: {
    locales: ["fr", "lt"],
    defaultLocale: "lt",
    localeDetection: false,
  },
};


Structure ( I switch _static and static between app and pages dirs when testing different dirs):
app/
  _static/
    [slug]/
      page.tsx
pages/
  static/
    [slug].tsx


With App Dir, when I request http://localhost:3001/fr/mano-puslapis I get 404:
rewrite to /static/mano-puslapis {
  href: 'http://localhost:3001/fr/static/mano-puslapis',
  origin: 'http://localhost:3001',
  protocol: 'http:',
  username: '',
  password: '',
  host: 'localhost:3001',
  hostname: 'localhost',
  port: '3001',
  pathname: '/static/mano-puslapis',
  search: '',
  searchParams: URLSearchParams {  },
  hash: ''
} fr
 ✓ Compiled /_not-found/page in 70ms
 GET /mano-puslapis 404 in 309ms


However, with App Dir, when I request http://localhost:3001/mano-puslapis I get 200:
rewrite to /static/mano-puslapis {
  href: 'http://localhost:3001/static/mano-puslapis',
  origin: 'http://localhost:3001',
  protocol: 'http:',
  username: '',
  password: '',
  host: 'localhost:3001',
  hostname: 'localhost',
  port: '3001',
  pathname: '/static/mano-puslapis',
  search: '',
  searchParams: URLSearchParams {  },
  hash: ''
} lt
GET /mano-puslapis 200 in 540ms


However, it's 200 for both URLs when in Pages Dir.


Repro rep: https://github.com/muningis/locale-test

0 Replies