-
Notifications
You must be signed in to change notification settings - Fork 89
fix: ensure middleware matchers evaluate default locale #2548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6bd9781
fix: ensure 2nd level middleware match respects locales
orinokai 49fe9ad
fix: ensure adding locale is case sensitive
orinokai 61ff711
test: update expect message
orinokai 1160801
chore: destructure localizedPath
orinokai b010192
feat: allow for locale fallbacks when adding locale prefix
orinokai 74e2502
fix: pass NextConfig in NextRequest (needed even though it's not in t…
orinokai 71ed888
fix: headers need to be Node.js style plain object (even though Next …
orinokai 935e621
test: assert that paths don't match with an invalid prefix
orinokai 8d3972c
Merge branch 'main' into rs/frp-761-imiddleware-locale
orinokai 878f022
test: remove only
orinokai dccecf9
chore: refactor for clarity
orinokai bfd3f54
chore: revert red herring
orinokai e1330fd
Merge branch 'rs/frp-761-imiddleware-locale' of github.com:netlify/ne…
orinokai 1a5886e
test: fix incorrect locale test
orinokai 8e691dc
chore: refactor NetlifyNextRequest type for clarity
orinokai a7620fd
test: fix faulty i18n test logic
orinokai da8215f
chore: refactor to remove normalizeLocalizedTarget
orinokai fb8d9ea
test: update failure messages for clarity
orinokai af173a6
chore: update e2e test command docs
orinokai 2bd7c8f
Merge branch 'main' into rs/frp-761-imiddleware-locale
pieh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import { HTMLRewriter } from '../vendor/deno.land/x/[email protected]/ | |
import { updateModifiedHeaders } from './headers.ts' | ||
import type { StructuredLogger } from './logging.ts' | ||
import { addMiddlewareHeaders, isMiddlewareRequest, isMiddlewareResponse } from './middleware.ts' | ||
import { RequestData } from './next-request.ts' | ||
import { NetlifyNextRequest } from './next-request.ts' | ||
import { | ||
addBasePath, | ||
normalizeDataUrl, | ||
|
@@ -20,20 +20,20 @@ export interface FetchEventResult { | |
|
||
interface BuildResponseOptions { | ||
context: Context | ||
logger: StructuredLogger | ||
nextConfig?: NetlifyNextRequest['nextConfig'] | ||
locale?: string | ||
request: Request | ||
result: FetchEventResult | ||
nextConfig?: RequestData['nextConfig'] | ||
requestLocale?: string | ||
logger: StructuredLogger | ||
} | ||
|
||
export const buildResponse = async ({ | ||
context, | ||
logger, | ||
nextConfig, | ||
locale, | ||
request, | ||
result, | ||
nextConfig, | ||
requestLocale, | ||
logger, | ||
}: BuildResponseOptions): Promise<Response | void> => { | ||
logger | ||
.withFields({ is_nextresponse_next: result.response.headers.has('x-middleware-next') }) | ||
|
@@ -198,8 +198,8 @@ export const buildResponse = async ({ | |
} | ||
|
||
// If we are redirecting a request that had a locale in the URL, we need to add it back in | ||
if (redirect && requestLocale) { | ||
redirect = normalizeLocalizedTarget({ target: redirect, request, nextConfig, requestLocale }) | ||
if (redirect && locale) { | ||
redirect = normalizeLocalizedTarget({ target: redirect, request, nextConfig }) | ||
if (redirect === request.url) { | ||
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url') | ||
return | ||
|
@@ -234,27 +234,27 @@ function normalizeLocalizedTarget({ | |
target, | ||
request, | ||
nextConfig, | ||
requestLocale, | ||
locale, | ||
}: { | ||
target: string | ||
request: Request | ||
nextConfig?: RequestData['nextConfig'] | ||
requestLocale?: string | ||
nextConfig?: NetlifyNextRequest['nextConfig'] | ||
locale?: string | ||
}) { | ||
const targetUrl = new URL(target, request.url) | ||
|
||
const normalizedTarget = normalizeLocalePath(targetUrl.pathname, nextConfig?.i18n?.locales) | ||
const targetPathname = normalizedTarget.pathname | ||
const targetLocale = normalizedTarget.detectedLocale ?? locale | ||
|
||
const locale = normalizedTarget.detectedLocale ?? requestLocale | ||
if ( | ||
locale && | ||
!normalizedTarget.pathname.startsWith(`/api/`) && | ||
!normalizedTarget.pathname.startsWith(`/_next/static/`) | ||
targetLocale && | ||
!targetPathname.startsWith(`/api/`) && | ||
!targetPathname.startsWith(`/_next/static/`) | ||
) { | ||
targetUrl.pathname = | ||
addBasePath(`/${locale}${normalizedTarget.pathname}`, nextConfig?.basePath) || `/` | ||
addBasePath(`/${targetLocale}${targetPathname}`, nextConfig?.basePath) || `/` | ||
} else { | ||
targetUrl.pathname = addBasePath(normalizedTarget.pathname, nextConfig?.basePath) || `/` | ||
targetUrl.pathname = addBasePath(targetPathname, nextConfig?.basePath) || `/` | ||
} | ||
return targetUrl.toString() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,8 @@ | |
|
||
import type { Key } from '../vendor/deno.land/x/[email protected]/index.ts' | ||
|
||
import { compile, pathToRegexp } from '../vendor/deno.land/x/[email protected]/index.ts' | ||
import { getCookies } from '../vendor/deno.land/[email protected]/http/cookie.ts' | ||
import { compile, pathToRegexp } from '../vendor/deno.land/x/[email protected]/index.ts' | ||
|
||
/* | ||
┌─────────────────────────────────────────────────────────────────────────┐ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.