|
1 | 1 | import type { Context } from '@netlify/edge-functions'
|
2 | 2 |
|
3 |
| -import { addBasePath, normalizeDataUrl, normalizeLocalePath, removeBasePath } from './util.ts' |
| 3 | +import { |
| 4 | + addBasePath, |
| 5 | + addTrailingSlash, |
| 6 | + normalizeDataUrl, |
| 7 | + normalizeLocalePath, |
| 8 | + removeBasePath, |
| 9 | +} from './util.ts' |
4 | 10 |
|
5 | 11 | interface I18NConfig {
|
6 | 12 | defaultLocale: string
|
@@ -41,43 +47,25 @@ const normalizeRequestURL = (
|
41 | 47 | ): { url: string; detectedLocale?: string } => {
|
42 | 48 | const url = new URL(originalURL)
|
43 | 49 |
|
44 |
| - url.pathname = removeBasePath(url.pathname, nextConfig?.basePath) |
45 |
| - const didRemoveBasePath = url.toString() !== originalURL |
| 50 | + let pathname = removeBasePath(url.pathname, nextConfig?.basePath) |
46 | 51 |
|
47 |
| - let detectedLocale: string | undefined |
48 |
| - |
49 |
| - if (nextConfig?.i18n) { |
50 |
| - const { pathname, detectedLocale: detected } = normalizeLocalePath( |
51 |
| - url.pathname, |
52 |
| - nextConfig?.i18n?.locales, |
53 |
| - ) |
54 |
| - if (!nextConfig?.skipMiddlewareUrlNormalize) { |
55 |
| - url.pathname = pathname || '/' |
56 |
| - } |
57 |
| - detectedLocale = detected |
58 |
| - } |
| 52 | + // If it exists, remove the locale from the URL and store it |
| 53 | + const { detectedLocale } = normalizeLocalePath(pathname, nextConfig?.i18n?.locales) |
59 | 54 |
|
60 | 55 | if (!nextConfig?.skipMiddlewareUrlNormalize) {
|
61 | 56 | // We want to run middleware for data requests and expose the URL of the
|
62 | 57 | // corresponding pages, so we have to normalize the URLs before running
|
63 | 58 | // the handler.
|
64 |
| - url.pathname = normalizeDataUrl(url.pathname) |
| 59 | + pathname = normalizeDataUrl(pathname) |
65 | 60 |
|
66 | 61 | // Normalizing the trailing slash based on the `trailingSlash` configuration
|
67 | 62 | // property from the Next.js config.
|
68 |
| - if (nextConfig?.trailingSlash && url.pathname !== '/' && !url.pathname.endsWith('/')) { |
69 |
| - url.pathname = `${url.pathname}/` |
| 63 | + if (nextConfig?.trailingSlash) { |
| 64 | + pathname = addTrailingSlash(pathname) |
70 | 65 | }
|
71 | 66 | }
|
72 | 67 |
|
73 |
| - if (didRemoveBasePath) { |
74 |
| - url.pathname = addBasePath(url.pathname, nextConfig?.basePath) |
75 |
| - } |
76 |
| - |
77 |
| - // keep the locale in the url for request.nextUrl object |
78 |
| - if (detectedLocale) { |
79 |
| - url.pathname = `/${detectedLocale}${url.pathname}` |
80 |
| - } |
| 68 | + url.pathname = addBasePath(pathname, nextConfig?.basePath) |
81 | 69 |
|
82 | 70 | return {
|
83 | 71 | url: url.toString(),
|
|
0 commit comments