@netlify/plugin-nextjs
Advanced tools
Comparing version 5.0.0-beta.6 to 5.0.0-beta.7
@@ -15,3 +15,2 @@ export const InternalHeaders = { | ||
const overriddenHeaders = responseHeaders.get('x-middleware-override-headers') | ||
if (!overriddenHeaders) { | ||
@@ -21,4 +20,12 @@ return | ||
const headersToUpdate = overriddenHeaders.split(',').map((header) => header.trim()) | ||
const headersToUpdate = new Set(overriddenHeaders.split(',').map((header) => header.trim())) | ||
// We can't iterate this directly, because we modify the headers in the loop. | ||
// This was causing values to be skipped. By spreading them first we avoid that. | ||
for (const key of [...requestHeaders.keys()]) { | ||
if (!headersToUpdate.has(key)) { | ||
requestHeaders.delete(key) | ||
} | ||
} | ||
for (const header of headersToUpdate) { | ||
@@ -28,7 +35,14 @@ const oldHeaderKey = 'x-middleware-request-' + header | ||
requestHeaders.set(header, headerValue) | ||
const oldValue = requestHeaders.get(header) || '' | ||
if (oldValue !== headerValue) { | ||
if (headerValue) { | ||
requestHeaders.set(header, headerValue) | ||
} else { | ||
requestHeaders.delete(header) | ||
} | ||
} | ||
responseHeaders.delete(oldHeaderKey) | ||
} | ||
responseHeaders.delete('x-middleware-override-headers') | ||
} |
import type { Context } from '@netlify/edge-functions' | ||
import { normalizeDataUrl, removeBasePath, normalizeLocalePath } from './util.ts' | ||
import { normalizeDataUrl, removeBasePath, normalizeLocalePath, addBasePath } from './util.ts' | ||
@@ -44,2 +44,3 @@ interface I18NConfig { | ||
url.pathname = removeBasePath(url.pathname, nextConfig?.basePath) | ||
const didRemoveBasePath = url.toString() !== originalURL | ||
@@ -68,2 +69,6 @@ let detectedLocale: string | undefined | ||
if (didRemoveBasePath) { | ||
url.pathname = addBasePath(url.pathname, nextConfig?.basePath) | ||
} | ||
return { | ||
@@ -70,0 +75,0 @@ url: url.toString(), |
@@ -6,3 +6,9 @@ import type { Context } from '@netlify/edge-functions' | ||
import type { StructuredLogger } from './logging.ts' | ||
import { normalizeDataUrl, normalizeLocalePath, relativizeURL, rewriteDataPath } from './util.ts' | ||
import { | ||
addBasePath, | ||
normalizeDataUrl, | ||
normalizeLocalePath, | ||
relativizeURL, | ||
rewriteDataPath, | ||
} from './util.ts' | ||
import { addMiddlewareHeaders, isMiddlewareRequest, isMiddlewareResponse } from './middleware.ts' | ||
@@ -168,6 +174,17 @@ import { RequestData } from './next-request.ts' | ||
}) | ||
if (rewriteUrl.toString() === request.url) { | ||
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url') | ||
return | ||
} | ||
res.headers.set('x-middleware-rewrite', relativeUrl) | ||
return addMiddlewareHeaders(fetch(new Request(rewriteUrl, request)), res) | ||
} | ||
const target = normalizeLocalizedTarget({ target: rewrite, request, nextConfig }) | ||
if (target === request.url) { | ||
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url') | ||
return | ||
} | ||
res.headers.set('x-middleware-rewrite', relativeUrl) | ||
request.headers.set('x-middleware-rewrite', rewrite) | ||
return addMiddlewareHeaders(fetch(new Request(rewriteUrl, request)), res) | ||
request.headers.set('x-middleware-rewrite', target) | ||
return addMiddlewareHeaders(fetch(new Request(target, request)), res) | ||
} | ||
@@ -179,13 +196,8 @@ | ||
if (redirect && requestLocale) { | ||
const redirectUrl = new URL(redirect, request.url) | ||
const normalizedRedirect = normalizeLocalePath(redirectUrl.pathname, nextConfig?.i18n?.locales) | ||
const locale = normalizedRedirect.detectedLocale ?? requestLocale | ||
// Pages router API routes don't have a locale in the URL | ||
if (locale && !redirectUrl.pathname.startsWith(`/api/`)) { | ||
redirectUrl.pathname = `/${locale}${normalizedRedirect.pathname}` | ||
redirect = redirectUrl.toString() | ||
res.headers.set('location', redirect) | ||
redirect = normalizeLocalizedTarget({ target: redirect, request, nextConfig }) | ||
if (redirect === request.url) { | ||
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url') | ||
return | ||
} | ||
res.headers.set('location', redirect) | ||
} | ||
@@ -212,1 +224,32 @@ | ||
} | ||
/** | ||
* Normalizes the locale in a URL. | ||
*/ | ||
function normalizeLocalizedTarget({ | ||
target, | ||
request, | ||
nextConfig, | ||
requestLocale, | ||
}: { | ||
target: string | ||
request: Request | ||
nextConfig?: RequestData['nextConfig'] | ||
requestLocale?: string | ||
}) { | ||
const targetUrl = new URL(target, request.url) | ||
const normalizedTarget = normalizeLocalePath(targetUrl.pathname, nextConfig?.i18n?.locales) | ||
const locale = normalizedTarget.detectedLocale ?? requestLocale | ||
if ( | ||
locale && | ||
!normalizedTarget.pathname.startsWith(`/api/`) && | ||
!normalizedTarget.pathname.startsWith(`/_next/static/`) | ||
) { | ||
targetUrl.pathname = addBasePath(`/${locale}${normalizedTarget.pathname}`, nextConfig?.basePath) | ||
} else { | ||
targetUrl.pathname = addBasePath(normalizedTarget.pathname, nextConfig?.basePath) | ||
} | ||
return targetUrl.toString() | ||
} |
@@ -27,2 +27,9 @@ import type { RequestData } from './next-request.ts' | ||
export const addBasePath = (path: string, basePath?: string) => { | ||
if (basePath && !path.startsWith(basePath)) { | ||
return `${basePath}${path}` | ||
} | ||
return path | ||
} | ||
// https://github.com/vercel/next.js/blob/canary/packages/next/src/shared/lib/i18n/normalize-locale-path.ts | ||
@@ -95,6 +102,9 @@ | ||
return dataUrl.replace( | ||
normalizeIndex(normalizedDataUrl), | ||
stripTrailingSlash(normalizeIndex(newRoute)), | ||
return addBasePath( | ||
dataUrl.replace( | ||
normalizeIndex(normalizedDataUrl), | ||
stripTrailingSlash(normalizeIndex(newRoute)), | ||
), | ||
basePath, | ||
) | ||
} |
@@ -62,2 +62,3 @@ import type { Context } from '@netlify/edge-functions' | ||
requestLocale: nextRequest.detectedLocale, | ||
nextConfig, | ||
}) | ||
@@ -64,0 +65,0 @@ |
{ | ||
"name": "@netlify/plugin-nextjs", | ||
"version": "5.0.0-beta.6", | ||
"version": "5.0.0-beta.7", | ||
"description": "Run Next.js seamlessly on Netlify", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2036168
64235