Skip to content
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

Feat locale detection path without force redirect #1

Draft
wants to merge 4 commits into
base: canary
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,25 @@ export function getUtils({
}
}

// @ts-ignore
const localeDetectPaths = i18n.localeDetectionPaths as string[]
const denormalizedPagePath = denormalizePagePath(pathname)
const denormalizedPagePathWithoutQuery = denormalizedPagePath
.split('?')
.shift()
?.split('#')
.shift() as string
const detectedDefaultLocale =
!detectedLocale ||
detectedLocale.toLowerCase() === defaultLocale.toLowerCase()
const shouldStripDefaultLocale = false
// detectedDefaultLocale &&
// denormalizedPagePath.toLowerCase() === \`/\${i18n.defaultLocale.toLowerCase()}\`

const shouldAddLocalePrefix =
!detectedDefaultLocale && denormalizedPagePath === '/'
const matchUrl = localeDetectPaths.some((path) =>
new RegExp(path).test(denormalizedPagePathWithoutQuery)
)
const shouldAddLocalePrefix = !detectedDefaultLocale && matchUrl

detectedLocale = detectedLocale || i18n.defaultLocale

Expand Down Expand Up @@ -449,8 +458,8 @@ export function getUtils({
pathname: localeDomainRedirect
? localeDomainRedirect
: shouldStripDefaultLocale
? basePath || '/'
: `${basePath}/${detectedLocale}`,
? basePath || `/`
: `${basePath || ''}/${detectedLocale}${denormalizedPagePath}`,
})
)
res.statusCode = TEMPORARY_REDIRECT_STATUS
Expand Down
12 changes: 9 additions & 3 deletions packages/next/next-server/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ export async function imageOptimizer(
const contentType = getContentType(extension)
const fsPath = join(hashDir, file)
if (now < expireAt) {
res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate')
res.setHeader(
'Cache-Control',
'public, max-age=86400000, s-maxage=86400000, stale-while-revalidate=86400000, immutable'
)
if (sendEtagResponse(req, res, etag)) {
return { finished: true }
}
Expand Down Expand Up @@ -369,7 +372,10 @@ function sendResponse(
buffer: Buffer
) {
const etag = getHash([buffer])
res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate')
res.setHeader(
'Cache-Control',
'public, max-age=86400000, s-maxage=86400000, stale-while-revalidate=86400000, immutable'
)
if (sendEtagResponse(req, res, etag)) {
return
}
Expand Down Expand Up @@ -413,7 +419,7 @@ function parseCacheControl(str: string | null): Map<string, string> {
}

export function getMaxAge(str: string | null): number {
const minimum = 60
const minimum = 86400000
const map = parseCacheControl(str)
if (map) {
let age = map.get('s-maxage') || map.get('max-age') || ''
Expand Down
19 changes: 15 additions & 4 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,14 @@ export default class Server {
}
}

const denormalizedPagePath = denormalizePagePath(pathname || '/')
// @ts-ignore
const localeDetectPaths = i18n.localeDetectionPaths as string[]
const denormalizedPagePath = denormalizePagePath(pathname)
const denormalizedPagePathWithoutQuery = denormalizedPagePath
.split('?')
.shift()
?.split('#')
.shift() as string
const detectedDefaultLocale =
!detectedLocale ||
detectedLocale.toLowerCase() === defaultLocale.toLowerCase()
Expand All @@ -497,8 +504,10 @@ export default class Server {
// denormalizedPagePath.toLowerCase() ===
// `/${i18n.defaultLocale.toLowerCase()}`

const shouldAddLocalePrefix =
!detectedDefaultLocale && denormalizedPagePath === '/'
const matchUrl = localeDetectPaths.some((path) =>
new RegExp(path).test(denormalizedPagePathWithoutQuery)
)
const shouldAddLocalePrefix = !detectedDefaultLocale && matchUrl

detectedLocale = detectedLocale || i18n.defaultLocale

Expand Down Expand Up @@ -539,7 +548,9 @@ export default class Server {
...parsed,
pathname: shouldStripDefaultLocale
? basePath || `/`
: `${basePath || ''}/${detectedLocale}`,
: `${
basePath || ''
}/${detectedLocale}${denormalizedPagePath}`,
})
)
res.statusCode = TEMPORARY_REDIRECT_STATUS
Expand Down