createNextMiddleware
API Reference for the createNextMiddleware() method.
Overview
createNextMiddleware is a utility function that creates a middleware function for use with Next.js.
It allows you to add a different route for each locale in your Next.js application.
For example, a french user would be directed to /fr/landing and an english user would be directed to /en/landing.
For more information on how to use this middleware, see the i18n routing guide.
Reference
Props
Prop
Type
Description
| Prop | Description | 
|---|---|
| pathConfig | A nested object that specifies localized paths for your application. | 
| localeRouting | A flag to enable or disable i18n routing. | 
| prefixDefaultLocale | A flag to enable or disable the removal of the locale prefix from the default locale. (e.g. /en/about->/about) | 
Example
Basic usage
Just invoke add this function and the path matcher to your middleware file to enable locale routing.
import { createNextMiddleware } from 'gt-next/middleware'
export default createNextMiddleware();
export const config = {
  matcher: [
    /*
      * Match all request paths except for the ones starting with:
      * - api (API routes)
      * - _next (internal files)
      * - static files
      */
    "/((?!api|static|.*\\..*|_next).*)",
  ],
}Localized paths
You can specify localized paths through the pathConfig option in the middleware file.
export default createNextMiddleware({
  pathConfig: {
    "/about": "/about",
    "/airplanes": {
      "zh": "/飞机",
    }
  },
});See the i18n routing guide for a detailed explanation of how this works.
Remove default locale prefix
You can remove the default locale prefix by setting the prefixDefaultLocale option to false.
export default createNextMiddleware({
  prefixDefaultLocale: true,
});When this is true, then every path must be prefixed with the locale.
If you set this to false (which is the default), then only the default locale will be removed from the path.
Notes
- The createNextMiddlewarefunction is a utility function that creates a middleware function for use with Next.js.
Next steps
How is this guide?

