From 0f276d9a703d26d47ec000b15a4789f3a7ca58c4 Mon Sep 17 00:00:00 2001 From: Aleksandar Petkov Date: Fri, 27 Oct 2023 11:23:59 +0300 Subject: [PATCH] Upgrade next version to 13.5.4 (#1631) * Update next version to 13.5.4 * i18n: Fix translations not working for dynamic paths * i18next.config: Detect user's locale from NEXT_LOCALE cookie --- next-i18next.config.js | 2 +- package.json | 2 +- src/components/client/layout/LocaleButton.tsx | 7 +- src/middleware.ts | 23 +++- yarn.lock | 115 ++++++++---------- 5 files changed, 82 insertions(+), 67 deletions(-) diff --git a/next-i18next.config.js b/next-i18next.config.js index ddd72d5e6..3f314fea9 100644 --- a/next-i18next.config.js +++ b/next-i18next.config.js @@ -1,6 +1,6 @@ module.exports = { i18n: { - localeDetection: false, + localeDetection: true, defaultLocale: 'bg', locales: ['bg', 'en'], }, diff --git a/package.json b/package.json index 9ef2f1e3f..25145a847 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "lodash": "^4.17.21", "mobx": "6.3.2", "mobx-react": "7.2.0", - "next": "13.4.12", + "next": "13.5.4", "next-auth": "^4.20.1", "next-i18next": "^14.0.3", "nookies": "^2.5.2", diff --git a/src/components/client/layout/LocaleButton.tsx b/src/components/client/layout/LocaleButton.tsx index 47ec57ccc..1f5da59ea 100644 --- a/src/components/client/layout/LocaleButton.tsx +++ b/src/components/client/layout/LocaleButton.tsx @@ -12,8 +12,13 @@ export default function LocaleButton() { // Same route different language const { pathname, asPath, query } = router router.push({ pathname, query }, asPath, { locale }) + const date = new Date() + const expireMs = 100 * 365 * 24 * 60 * 60 * 1000 // 100 days + date.setTime(date.getTime() + expireMs) + document.cookie = `NEXT_LOCALE=${locale};expires=${date.toUTCString()};path=/` + router.push({ pathname, query }, asPath, { locale }) }, - [router.asPath], + [router.asPath, router.locale], ) if (!router.locale) { return null diff --git a/src/middleware.ts b/src/middleware.ts index 8631e5034..6bc91d128 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,9 +1,30 @@ import { isAdmin } from 'common/util/roles' import { getToken } from 'next-auth/jwt' import withAuth, { NextAuthMiddlewareOptions } from 'next-auth/middleware' +import { NextRequest, NextResponse } from 'next/server' export const config = { - matcher: ['/admin', '/admin/:path*', '/profile', '/profile/:path*'], + matcher: ['/admin', '/admin/:path*', '/profile', '/profile/:path*', '/campaigns/:path*'], +} + +const PUBLIC_FILE = /\.(.*)$/ + +export async function middleware(req: NextRequest) { + if ( + req.nextUrl.pathname.startsWith('/_next') || + req.nextUrl.pathname.includes('/api/') || + PUBLIC_FILE.test(req.nextUrl.pathname) + ) { + return + } + + if (req.nextUrl.locale === 'default') { + const locale = req.cookies.get('NEXT_LOCALE')?.value || 'bg' + + return NextResponse.redirect( + new URL(`/${locale}${req.nextUrl.pathname}${req.nextUrl.search}`, req.url), + ) + } } export default withAuth({ diff --git a/yarn.lock b/yarn.lock index 36191d148..2fbc64dc6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2296,10 +2296,10 @@ __metadata: languageName: node linkType: hard -"@next/env@npm:13.4.12": - version: 13.4.12 - resolution: "@next/env@npm:13.4.12" - checksum: 2ccb2e271b3c42697c1e807cdf988429fcb563f80fa0ca72512f65f47cbbcc46c44fc53bf055814d4b467f1394de8c1a1ef6aad14d35f9993004faa956466d02 +"@next/env@npm:13.5.4": + version: 13.5.4 + resolution: "@next/env@npm:13.5.4" + checksum: 95ec7108bc88a01fed5389fb33e4b9eb34937908859d9f0aa87930c660f4395d90dafe10e54830faae5bc0a1b799be544c6455a2c8054499569d1e9296369076 languageName: node linkType: hard @@ -2312,65 +2312,65 @@ __metadata: languageName: node linkType: hard -"@next/swc-darwin-arm64@npm:13.4.12": - version: 13.4.12 - resolution: "@next/swc-darwin-arm64@npm:13.4.12" +"@next/swc-darwin-arm64@npm:13.5.4": + version: 13.5.4 + resolution: "@next/swc-darwin-arm64@npm:13.5.4" conditions: os=darwin & cpu=arm64 languageName: node linkType: hard -"@next/swc-darwin-x64@npm:13.4.12": - version: 13.4.12 - resolution: "@next/swc-darwin-x64@npm:13.4.12" +"@next/swc-darwin-x64@npm:13.5.4": + version: 13.5.4 + resolution: "@next/swc-darwin-x64@npm:13.5.4" conditions: os=darwin & cpu=x64 languageName: node linkType: hard -"@next/swc-linux-arm64-gnu@npm:13.4.12": - version: 13.4.12 - resolution: "@next/swc-linux-arm64-gnu@npm:13.4.12" +"@next/swc-linux-arm64-gnu@npm:13.5.4": + version: 13.5.4 + resolution: "@next/swc-linux-arm64-gnu@npm:13.5.4" conditions: os=linux & cpu=arm64 & libc=glibc languageName: node linkType: hard -"@next/swc-linux-arm64-musl@npm:13.4.12": - version: 13.4.12 - resolution: "@next/swc-linux-arm64-musl@npm:13.4.12" +"@next/swc-linux-arm64-musl@npm:13.5.4": + version: 13.5.4 + resolution: "@next/swc-linux-arm64-musl@npm:13.5.4" conditions: os=linux & cpu=arm64 & libc=musl languageName: node linkType: hard -"@next/swc-linux-x64-gnu@npm:13.4.12": - version: 13.4.12 - resolution: "@next/swc-linux-x64-gnu@npm:13.4.12" +"@next/swc-linux-x64-gnu@npm:13.5.4": + version: 13.5.4 + resolution: "@next/swc-linux-x64-gnu@npm:13.5.4" conditions: os=linux & cpu=x64 & libc=glibc languageName: node linkType: hard -"@next/swc-linux-x64-musl@npm:13.4.12": - version: 13.4.12 - resolution: "@next/swc-linux-x64-musl@npm:13.4.12" +"@next/swc-linux-x64-musl@npm:13.5.4": + version: 13.5.4 + resolution: "@next/swc-linux-x64-musl@npm:13.5.4" conditions: os=linux & cpu=x64 & libc=musl languageName: node linkType: hard -"@next/swc-win32-arm64-msvc@npm:13.4.12": - version: 13.4.12 - resolution: "@next/swc-win32-arm64-msvc@npm:13.4.12" +"@next/swc-win32-arm64-msvc@npm:13.5.4": + version: 13.5.4 + resolution: "@next/swc-win32-arm64-msvc@npm:13.5.4" conditions: os=win32 & cpu=arm64 languageName: node linkType: hard -"@next/swc-win32-ia32-msvc@npm:13.4.12": - version: 13.4.12 - resolution: "@next/swc-win32-ia32-msvc@npm:13.4.12" +"@next/swc-win32-ia32-msvc@npm:13.5.4": + version: 13.5.4 + resolution: "@next/swc-win32-ia32-msvc@npm:13.5.4" conditions: os=win32 & cpu=ia32 languageName: node linkType: hard -"@next/swc-win32-x64-msvc@npm:13.4.12": - version: 13.4.12 - resolution: "@next/swc-win32-x64-msvc@npm:13.4.12" +"@next/swc-win32-x64-msvc@npm:13.5.4": + version: 13.5.4 + resolution: "@next/swc-win32-x64-msvc@npm:13.5.4" conditions: os=win32 & cpu=x64 languageName: node linkType: hard @@ -3662,12 +3662,12 @@ __metadata: languageName: node linkType: hard -"@swc/helpers@npm:0.5.1": - version: 0.5.1 - resolution: "@swc/helpers@npm:0.5.1" +"@swc/helpers@npm:0.5.2": + version: 0.5.2 + resolution: "@swc/helpers@npm:0.5.2" dependencies: tslib: ^2.4.0 - checksum: 71e0e27234590435e4c62b97ef5e796f88e786841a38c7116a5e27a3eafa7b9ead7cdec5249b32165902076de78446945311c973e59bddf77c1e24f33a8f272a + checksum: 51d7e3d8bd56818c49d6bfbd715f0dbeedc13cf723af41166e45c03e37f109336bbcb57a1f2020f4015957721aeb21e1a7fff281233d797ff7d3dd1f447fa258 languageName: node linkType: hard @@ -10977,30 +10977,28 @@ __metadata: languageName: node linkType: hard -"next@npm:13.4.12": - version: 13.4.12 - resolution: "next@npm:13.4.12" +"next@npm:13.5.4": + version: 13.5.4 + resolution: "next@npm:13.5.4" dependencies: - "@next/env": 13.4.12 - "@next/swc-darwin-arm64": 13.4.12 - "@next/swc-darwin-x64": 13.4.12 - "@next/swc-linux-arm64-gnu": 13.4.12 - "@next/swc-linux-arm64-musl": 13.4.12 - "@next/swc-linux-x64-gnu": 13.4.12 - "@next/swc-linux-x64-musl": 13.4.12 - "@next/swc-win32-arm64-msvc": 13.4.12 - "@next/swc-win32-ia32-msvc": 13.4.12 - "@next/swc-win32-x64-msvc": 13.4.12 - "@swc/helpers": 0.5.1 + "@next/env": 13.5.4 + "@next/swc-darwin-arm64": 13.5.4 + "@next/swc-darwin-x64": 13.5.4 + "@next/swc-linux-arm64-gnu": 13.5.4 + "@next/swc-linux-arm64-musl": 13.5.4 + "@next/swc-linux-x64-gnu": 13.5.4 + "@next/swc-linux-x64-musl": 13.5.4 + "@next/swc-win32-arm64-msvc": 13.5.4 + "@next/swc-win32-ia32-msvc": 13.5.4 + "@next/swc-win32-x64-msvc": 13.5.4 + "@swc/helpers": 0.5.2 busboy: 1.6.0 caniuse-lite: ^1.0.30001406 - postcss: 8.4.14 + postcss: 8.4.31 styled-jsx: 5.1.1 watchpack: 2.4.0 - zod: 3.21.4 peerDependencies: "@opentelemetry/api": ^1.1.0 - fibers: ">= 3.1.0" react: ^18.2.0 react-dom: ^18.2.0 sass: ^1.3.0 @@ -11026,13 +11024,11 @@ __metadata: peerDependenciesMeta: "@opentelemetry/api": optional: true - fibers: - optional: true sass: optional: true bin: next: dist/bin/next - checksum: 50bd443ffe09424c1a94d6606d41886ccd1d65185e125aa199957ea92c5e4d1c226f1675f3e5ea92e88f43f8355824ba50db52a8aeae225f7a86b6c901d25527 + checksum: f8e964ee9bbabd0303f9d807c9193833fcc47960be029c3721db9a5a35cc4ff690313e30fc6ee497f959a9141048957dddf6eb038b4a23c78c8762b0cd9d0ae0 languageName: node linkType: hard @@ -11704,7 +11700,7 @@ __metadata: lodash: ^4.17.21 mobx: 6.3.2 mobx-react: 7.2.0 - next: 13.4.12 + next: 13.5.4 next-auth: ^4.20.1 next-i18next: ^14.0.3 next-sitemap: ^3.1.52 @@ -15011,13 +15007,6 @@ __metadata: languageName: node linkType: hard -"zod@npm:^3.22.3": - version: 3.22.4 - resolution: "zod@npm:3.22.4" - checksum: 80bfd7f8039b24fddeb0718a2ec7c02aa9856e4838d6aa4864335a047b6b37a3273b191ef335bf0b2002e5c514ef261ffcda5a589fb084a48c336ffc4cdbab7f - languageName: node - linkType: hard - "zwitch@npm:^2.0.0, zwitch@npm:^2.0.4": version: 2.0.4 resolution: "zwitch@npm:2.0.4"